File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification needed on setSoTimeout()

 
jaibhim max
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am opening one channel and reading some streams through a socket. If i use setSoTimeout(5000), it is expected that socket will be closed after 5 seconds.
Consider, I have established SSH connection with a server and reading data through socket, I want to know -
1. If my operation through that socket is taking 20 seconds, will that socket will be timed out after 5 seconds even if the operation is in progress?
2. If my connection breaks with the server when it is reading data through that socket, will my socket be timed out after 5 seconds?


Thanks,
Akash Mahakode
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaibhim max wrote:If i use setSoTimeout(5000), it is expected that socket will be closed after 5 seconds.

So, what exactly do you expect: if you call this, then after 5 seconds the socket connection is automatically closed?

That's not what setSoTimeout does. In fact, it does not have anything to do with closing the socket. The API documentation describes it in detail:
Socket.setSoTimeout wrote:Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. 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.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

In other words: if you call read() on the socket's input stream and nothing is received within 5 seconds, you will get a SocketTimeoutException. That doesn't mean that the connection is closed; it only means that no data was received on the socket within 5 seconds. The connection will still be open if you get the exception.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic