Search code examples
javaserversocket

Java: interrupt accept method without close the server socket


Is there any way to interrupt a while(true) accepting clients on a server socket, without calling close the server socket and throwing an exception?

Is there, for example, a timeout that the accept method can have?


Solution

  • You can set server socket timeout with setSoTimeout. Once expired a SocketTimeoutException is raised.