Socket Timeout

Alex Givant

Greenhorn

Posts: 23

posted 16 years ago

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 creatingthread 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

Posts: 8

posted 16 years ago

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]

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

Posts: 8

posted 16 years ago

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

Posts: 19

posted 16 years ago

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

Posts: 32

posted 15 years ago

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

Posts: 13

posted 12 years ago

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

Posts: 8791

posted 12 years ago

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

Posts: 13

posted 12 years ago

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: