5

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.

2 Answers2

3

Of course it closes the socket. What else would it be for? The only exception is when the channel is registered with a Selector, when the actual close is deferred to the next select operation.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

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

xagyg
  • 9,562
  • 2
  • 32
  • 29
  • The bug you now cite shows 'Resolved date: 2005-04-16'. If you've been having difficulties in this area, it is almost certainly because of the delayed close referred to in my answer. – user207421 May 21 '13 at 02:12