This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "timeout" Watch "timeout" New topic
Author

timeout

Ariffin Ahmad
Ranch Hand

Joined: Aug 16, 2001
Posts: 84
i made a client to make a socket connection to some server.
this client will send some parameter and will waiting for some reponse after that.
the problem is, how can i put a timeout to that client, so that the client don't have to wait forever for the response...
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Is Socket.setSoTimeout() what you're looking for? With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid.
- Peter
Ariffin Ahmad
Ranch Hand

Joined: Aug 16, 2001
Posts: 84
thanx mate...
one more problem....
how about, while the client waiting for servlet response, ie: waiting in read() statement, how can i interrupt the read(), so that i don't have to wait any response from the server to in order to end my process...
Originally posted by Peter den Haan:
Is Socket.setSoTimeout() what you're looking for? With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid.
- Peter

Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ariffin Ahmad:
how about, while the client waiting for servlet response, ie: waiting in read() statement, how can i interrupt the read(), so that i don't have to wait any response from the server to in order to end my process...
You can't, except by either (a) setting a timeout as indicated above, or (b) close()ing the Socket from another thread. The latter is an undocumented, but widely used trick. Of course after (b) your socket will be unusuable.
- Peter
Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
To do what you are talking about you need to have a separate Thread running which monitors how long it takes to get the InputStream back from the socket connection. If the time required to get the InputStream exceeds the timeout you have defined then you would want to throw some sort of exception.
There is a class called com.jscape.inet.util.TimedSocket which will do this for you in the iNet Factory distribution available at http://www.jscape.com

------------------
JScape - Java Network Components www.jscape.com
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: timeout
 
Similar Threads
Duplicate JMS message processing with multiple MessageListener threads
Setting time out for web service call
Transaction Timeouts
write() method of DataOutputStream not throwing exception
Interview question asked to me which I could not answer