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
-
i saw about setSoTimeOut(2000) functionwhich closes the socket after the specifed time interval, but i would like to close only if the socket remains idle for the specified interval.raj man– raj man2016-11-28 01:18:47 +00:00Commented Nov 28, 2016 at 1:18
-
it is stays Idle for more than the specified interval - what is your logic for this?Scary Wombat– Scary Wombat2016-11-28 01:19:59 +00:00Commented Nov 28, 2016 at 1:19
-
in my case, 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 and hence too many connection remains open which leads to various problems in my case. so i would like to close the socket if it stays idle for the specified interval.raj man– raj man2016-11-28 01:25:38 +00:00Commented Nov 28, 2016 at 1:25
1 Answer
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.