ServerSocket serverSocket = new ServerSocket(some_port);
Socket socket = serverSocket.accept();
serverSocket.close();
Will socket
be closed as well?
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.
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 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.
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.
No. Accepted Sockets
are completely independent of the ServerSocket
they were accepted from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With