raj man
raj man

Reputation: 21

Socket Idle timeout Exception In java

Basically am a newbie to socket programing. i would like to know about how to close a socket if stays idle for a specified time interval. i searched on net about this ,i found that function which is used to close the socket after the specified interval. but here in my case , i would like to close the socket only when it is stays Idle for more than the specified interval

Upvotes: 0

Views: 2526

Answers (1)

user207421
user207421

Reputation: 310850

I searched on net about this

Why? The Javadoc exists. No searching necessary.

I found that function which is used to close the socket after the specified interval

There is no such method.

I saw about setSoTimeOut(2000) function which closes the socket after the specifed time interval

No it doesn't. It doesn't close the socket at all, and it causes read methods to throw a SocketTimeoutException if no data arrives within the timeout period.

but I would like to close only if the socket remains idle for the specified interval

Socket.setSoTimeout() is exactly what you need.

the client establishes the connection with the server and then later after sometime it the client close the socket connection at its side after performing the required task and creates a new connection the next time when it pings, where as my server does not close the connection and it keeps on listening to that client

In other words your server is ignoring end of stream on the socket. Don't do that. Close the socket if you get end of stream from a read method.

Upvotes: 1

Related Questions