Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Jan 8, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 timeout for a socket connection attempt. There is no reason this should be so, and is a flaw in the core 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.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date