Java – Set keep-alive for Java ServerSocket

client-serverjavaserver-sidetcp

In a multi client server application, I need to know when each client connection is dead on the server side.
After set keep-alive for client (and change default time from 2 hours to 10 second) each client could know when disconnected from server, but on the server side, Java doesn't have any method to set keep-alive like client socket.

Best Answer

Java's ServerSocket returns a new Socket object for each client that connects. This Socket object is the same type as is used in a client, so supports exactly the same options. You can simply set the keepalive option there.