This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

timeout

 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic