Search code examples
javasocketsnetwork-programmingchannelsocketchannel

Does SocketChannel.close() close the socket also?


I have a server program that needs to close any connections that remain when the server is shutting down. I know I need to call SocketChannel.close(). My question is if I also need to close the Socket associated with the SocketChannel or if SocketChannel.close() does that automatically.


Solution

  • SocketChannel.close() should close the Socket. However, there have been some bugs around this issue (should have been solved a long time ago). https://bugs.java.com/bugdatabase/view_bug?bug_id=4960962. Another situation that can cause problems: https://bugs.java.com/bugdatabase/view_bug?bug_id=6179351