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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
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.