Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

timeout

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic