while (true) { // selector blocks until at least one channel is selected selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
while (keyIterator.hasNext()) { SelectionKeyselectionKey= keyIterator.next();
if (selectionKey.isAcceptable()) { System.out.println("[Server] selected acceptable socket"); ServerSocketChannelserverSocketChannel= (ServerSocketChannel) selectionKey.channel();
// create a SocketChannel for accepted request socket SocketChannelsocketChannel= serverSocketChannel.accept(); socketChannel.configureBlocking(false); // configure as non-blocking
shutdownInputpublic void shutdownInput() throws IOException Places the
input stream for this socket at "end of stream". Any data sent to the
input stream side of the socket is acknowledged and then silently
discarded. If you read from a socket input stream after invoking this
method on the socket, the stream's available method will return 0, and
its read methods will return -1 (end of stream).
Throws:IOException - if an I/O error
occurs when shutting down this socket. Since: 1.3
See Also:shutdownOutput(),close(),
setSoLinger(boolean, int),
isInputShutdown()
Socket的shutdownOutput文档说明为:
shutdownOutputpublic void shutdownOutput() throws IOException Disables
the output stream for this socket. For a TCP socket, any previously
written data will be sent followed by TCP's normal connection
termination sequence. If you write to a socket output stream after
invoking shutdownOutput() on the socket, the stream will throw an
IOException. Throws:IOException - if an
I/O error occurs when shutting down this socket. Since:
1.3 See Also:shutdownInput(),
close(), setSoLinger(boolean, int),
isOutputShutdown()