Timeouts on Socket Creation

I am trying to open multiple sockets at differing IP addresses. Is there a way to make it so that the open socket call will wait for only two seconds?

There is no supported way of defining a timeoutfor a socket connection attempt. There is noreason this should be so, and is a flaw in thecore Java API. Unfortunately, this has not changed in Java 1.2. You can see my August 1998 Java Pro column, entitled “Socket to Me,” for a discussion of this dilemma. Perhaps the only workaround is to launch the socket connetion attempt in a separate thread and then wait in the main program for the timeout period. If the socket connection succeeds before the timeout, the thread should wake up the main parent thread. If the socket connection is still in progress, the main thread should kill the connection thread. It’s ugly, but it works.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: