0

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

3
  • 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. Commented Nov 28, 2016 at 1:18
  • it is stays Idle for more than the specified interval - what is your logic for this? Commented 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. Commented Nov 28, 2016 at 1:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

am completely new to this can you please say me on how to detect the end of stream from the read method
Have you considered consulting the Javadoc? You seem to be just guessing at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.