Forums Register Login

Socket Timeout

+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 7751 times.
Similar Threads
Current Date Comparison
timeout
Difference Between 2 Dates-Contributed by Hari Krishna Kumar @ Chennai
How to get past date ?
How to Compare two Calendar Objects.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:11:10.