• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applets and Sockets

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
What can i do to make my Applet create a socket to a a ServerSocket ?
I've done it using Frames but applets don't seem to work . Are there any security issues regarding applets ?
What is the solution ?
Thanks in advance
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets can open and use sockets, but only if the socket is connected to the server from which the applet was loaded.
If you want to connect to anywhere else, you'd have to security-sign the applet.
It's actually quite common for applets to talk to their servers using a process called HTTP tunnelling. This allows the applet to get/set data from a database in a 3-tier applicaiton, for example.
 
Ulvi Cetin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a java policy file and put these in it:
grant {
permission java.net.SocketPermission "host.com ortno","connect,accept,listen,resolve";
};
where host.com is the host you are connecting and portno is the port number.
The file name should be .java.policy (starts with a dot). And don't forget the semi-column at the end of grant statement closing curly braces.
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TIM .
can you please explain it detail what exactly is to be done .
Thanks a ton
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Tim corrctly pointed out, applets can only open socket connection with the server from where they are downloaded.
You'd typically construct a URL of the server (and when you are testing, ensure that applet is run with HTTP://whatever/ and not file:///etc/), get a URLConnection of that server, get input stream of the URRLCOnnection and read from it as you'd in case of normal IO operation.
Also have a look at this -
http://www.geocities.com/technofundo/tech/java/readurl.html
and see if it is of any help.
HTH,
- Manish
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish ... i want to make use of sockets instaed of th URl class .
I'll explain what i,am trying to achieve
I have a client ( Frame at present) which makes a socket connection to xx.xxx.xx.xxx ip address at port 5000 . The Serversocket object is created for the pirticular client and the client is able to send text messages to the server .
All this now has to be don with the help of applets . But with applets i,am not able to make a connection( create a socket ) to xx.xxx.xx.xxx at port 5000.
I hope i,am clear enough
Thanks in advance
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With sockets also, your code will be similar. However if you're connecting to the IP say - a.b.c.d make sure that your applet is also referred from there using the IP itself. If you're having problems connecting to specifi port (say 5000), it may be due to the firewall. What sort of error/exception you're getting?
- Manish
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO Exception . Just not able to estabish a connection from an applet to the server running on 1234 ip port 5000.
 
Murali Nanchala
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test it locally first to rule out the firewall 'policing' of the non standard 5000 port.
If it works, then we'll go further.
Like it is said before in this thread, just make sure the xx.xxx.xxx.xx ip you are trying to make a socket connection on is the same one serving the applet.
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to see if the server is indeed running on that specific IP on that specific port. Trace using -
netstat -ap tcp
from command prompt or something similar.
- Manish
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish
i've tested the ip and port and i,am sure that the server is working fine on the this ip on the specified port. I can confirm this because my similar application written using frames works fine.

Like it is said before in this thread, just make sure the xx.xxx.xxx.xx ip you are trying to make a socket connection on is the same one serving the applet.

murali ... i,am not sure what do you mean by this .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic