wood burning stoves 2.0*
The moose likes Applets and the fly likes Applets and Sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applets and Sockets " Watch "Applets and Sockets " New topic
Author

Applets and Sockets

Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
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


Raghav.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ulvi Cetin
Ranch Hand

Joined: Mar 03, 2003
Posts: 39
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

Joined: Jan 12, 2001
Posts: 641
TIM .
can you please explain it detail what exactly is to be done .
Thanks a ton
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Mar 14, 2001
Posts: 74
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.


"It is almost always answered in the API" - Murali Nanchala
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jan 12, 2001
Posts: 641
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 .
 
wood burning stoves
 
subject: Applets and Sockets
 
Similar Threads
Animated Applet API
Applets
requested servlet is not available
servelets and applets
Help:(