Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I close a ServerSocket, will the Sockets accepted by the ServerSocket be also closed?

ServerSocket serverSocket = new ServerSocket(some_port);
Socket socket = serverSocket.accept();
serverSocket.close();

Will socket be closed as well?

like image 960
hebothu Avatar asked Apr 03 '16 00:04

hebothu


People also ask

What happens when the close () method of a ServerSocket is called?

The Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. Upon closing, the Connected property is set to false . For connection-oriented protocols, it is recommended that you call Shutdown before calling the Close method.

How do I close ServerSocket?

The close() method of ServerSocket class is used to close this socket. Any thread currently blocked in accept() will throw a SocketException. The associated channel is also closed by it if it has any.

What happens if ServerSocket?

What happens if ServerSocket is not able to listen on the specified port? Explanation: public ServerSocket() creates an unbound server socket. It throws IOException if specified port is busy when opening the socket.

Do you need to close the server socket?

Yes. While destroying references to the socket may cause the garbage collector to finalize it, that does not specify that it will be closed. This is implementation-specific in many cases, and can sometimes derail relative to design due to performance or even minute bugs that are hard to track.


1 Answers

No. Accepted Sockets are completely independent of the ServerSocket they were accepted from.

like image 126
user207421 Avatar answered Sep 22 '22 23:09

user207421