This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Socket Timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Timeout" Watch "Socket Timeout" New topic
Author

Socket Timeout

Alex Givant
Greenhorn

Joined: Nov 12, 2001
Posts: 23
Hi, guys.
Here is my question related to socket creation timeout. We want
to control process of socket creation and if socket hasn't been
created in timeout period just continue with the program flow.
Here I've wrote some simple program and it's shows me different
results on different platforms:


Here I'm using "10.0.10.146" as IP address of non-exist computer
When I run it on Windows 2000 - it gives me ~ 21.5 second. But
Sun Solaris - 219.5 second (x10 from Windows time).
So my question: Is any way to control that (beside creating
thread which will wait for n seconds and if socket not exists
yet will send some kind of exception)?
Any answer appriciated.
Alex.

[This message has been edited by Alex Givant (edited November 27, 2001).]
James Johnson
Greenhorn

Joined: Nov 20, 2001
Posts: 8
Originally posted by Alex Givant:
[B]Hi, guys.
Here is my question related to socket creation timeout. We want
to control process of socket creation and if socket hasn't been
created in timeout period just continue with the program flow.
Here I've wrote some simple program and it's shows me different
results on different platforms:


Here I'm using "10.0.10.146" as IP address of non-exist computer
When I run it on Windows 2000 - it gives me ~ 21.5 second. But
Sun Solaris - 219.5 second (x10 from Windows time).
So my question: Is any way to control that (beside creating
thread which will wait for n seconds and if socket not exists
yet will send some kind of exception)?
Any answer appriciated.
Alex.
[This message has been edited by Alex Givant (edited November 27, 2001).][/B]

Im not absolutly sure but, it looks like you can specify the time with the settimeout() method of the Socket class take a look:
http://java.sun.com/j2se/1.4/docs/api/java/net/Socket.html#setSoTimeout(int)
James Johnson
Alex Givant
Greenhorn

Joined: Nov 12, 2001
Posts: 23
Hi, James!
Thank you for answer, but setSoTimeout works for socket that you
already opened. What I need to do it to try to open a socket and
if the socket cann't be open in n seconds, just exit from method.
I know that it's possible to do with thread that wait n second
and than throws some kind of exception.
I was only wondering if there is standard remedy to achive the
same goal.
Thank you.
Alex Givant.
James Johnson
Greenhorn

Joined: Nov 20, 2001
Posts: 8
Ah okay, I wasnt compeletly sure about all the details. In that case Im thinking that if there is not a specific API then you'll probably have to use a thread. If you find an answer let me know, I kind of would like to implement the same thing in a program i am writing.
James
Joel Peach
Greenhorn

Joined: Oct 12, 2001
Posts: 19
Alex,
SO_TIMEOUT also applies to serversockets when you want to set the max time for an accept() call. I couldn't find any additional information on native support for what you're trying to do in the JDK docs.
Best of luck!
-J
Lou Caudell
Ranch Hand

Joined: Oct 06, 2001
Posts: 32
Be sure to throw the SocketException, or you won't know what happened. One wouldn't want to go to the trouble of reconstructing data for each failure, so it's prudent to catch the SocketException after the time out and retry with the same data.
James Redpath
Greenhorn

Joined: Aug 02, 2003
Posts: 13
I certainly would like to do that as well. The problem is all the options are for after the socket is created and most have to be Java 1.3 or higher. When you create a socket and the location does not exist (wrong IP or port), then you wait for a much longer time on Sun Solaris than XP. The default time for Solaris is 3 minutes (190 seconds), but this can be adjusted using ndd command all the way down to 10 seconds. As I recall Sun does not recommend making it lower than 30 seconds. The wait cannot be adjusted for XP. The only way I see is a thread which waits a specific time and throws an exception. I am considering this but its messy. I would like to see your thread if you have done this. Meanwhile, looks like I�m going to do it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Much of this thread was news to me. Things I thought I knew were wrong. Thanks!

Did you try the no argument constructor and the connect( address, timeout ) method?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
James Redpath
Greenhorn

Joined: Aug 02, 2003
Posts: 13
Well looks like I had to do it after all. What you need to do is create the Socket without opening it (connecting). Then pass it to a thread testing its status after a sleep (specified). Meanwhile returning back open the socket. The thread then awakes and tests to see if it not null, if so then closes which causes an exception caught by the parent opening socket. Here it is:


[ April 07, 2005: Message edited by: James Redpath ]
Arnaud Delorme
Greenhorn

Joined: Jun 08, 2011
Posts: 1
The connect idea works as well.

clientSocket = new Socket();
SocketAddress socketAddress = new InetSocketAddress("localhost", 12345);
try
{
clientSocket.connect(socketAddress, 5000); // 5 second timeout
}
catch (IOException e) { System.out.println(e.getMessage()); }

Arno
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Socket Timeout
 
Similar Threads
Difference Between 2 Dates-Contributed by Hari Krishna Kumar @ Chennai
How to get past date ?
Current Date Comparison
How to Compare two Calendar Objects.
timeout