problem with closing connection to client socket
Hello
I am new to java networking domain.
I am having an issue with my code.My problem is
I created a server that can accept messages from multiple clients using multithreading.
I want to close a client when it is not sending any data to the server for 2 minutes. That means when the client is idle for 2 minutes.
Please help me if there is any way.
Thanks in advance.
Sunitha
Re: problem with closing connection to client socket
On your server side class when the clients socket is created/connected you can call setSOTimeout(timeout in milliseconds) on the clients socket.
When you call the read method on that socket's InputStream if no data is read/received with in that SO Timeout then the
read method for that stream will throw a SocketTimeoutException , but the Socket is not closed.
If you catch that exception then you can do something such as close that client's connection.
The Java API reads....
Quote:
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.