• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Socket Timeout

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
James Redpath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic