Applet socket timeout exception

A

Ashwin Kambli

Hi all,
I am writing a simple applet to check for certain network properties.
In one of the tests, I call a webservice on my server
(SocketListener.asmx on the codebase of the applet). This webservice
opens a socket for listening on a pre-determined port and remains open
until a connection is establised or timesout (60 seconds). Once this
is done asynchronously, I open up a socket in the applet and try to
connect to the server machine on the pre-determined port#. This applet
works perfectly on my local machine (server and client both on same
machine). But when I try it from another machine on the intranet, the
applet socket throws a "Connection timed out" exception. Eventually
(after 60 seconds), the server socket too times out and sends back an
appropriate message.
Now, I copied the actual class file that tries to make the connection
to the server(PingServer.class), to the intranet machine that failed.
When I ran this class from the command prompt (java PingServer
serverip 9000), it worked perfectly.
Any ideas as to what the problem might be?
Ashwin
 
A

Ashwin Kambli

I realised what the problem was. I was trying to bind the socket to a
local port. This threw an IOException, since Sockets in an applet are
not allowed to bind to a pre-determined port.
But, this does not solve my problem, since I have to bind my socket to
a pre-determined port (port 6020). Any idea how to go about doing it?
I have a hunch that I need to grant some permissions in the
java.policy file. I tried:
permission java.net.SocketPermission "localhost:1024-",
"listen,connect,resolve";
permission java.io.FilePermission "*", "read,write";
But, it didnt work.
Any clues?
Ashwin Kambli

Snippet of my code:

try {

Socket s = new Socket();
//Bind the socket to the local port 6020
SocketAddress localaddress = new
InetSocketAddress(InetAddress.getLocalHost(),6020);
SocketAddress serveraddress = new
InetSocketAddress(getCodeBase().getHost(),9000);
s.bind(localaddress);
s.connect(serveraddress);
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top