Java Reference
In-Depth Information
{
/*
Reset buffer pointer to start of buffer,
prior to reading buffer's contents and
writing them to the SocketChannel…
*/
buffer.fl ip();
while (buffer.remaining()>0)
socketChannel.write(buffer);
}
catch (IOException ioEx)
{
System.out.println("\nClosing socket "
+ socket + "…");
closeSocket(socket);
}
}
//Remove this event, to avoid re-processing it
//as though it were a new one…
selector.selectedKeys().remove(key);
}
private static void closeSocket(Socket socket)
{
try
{
if (socket != null)
socket.close();
}
catch (IOException ioEx)
{
System.out.println(
"*** Unable to close socket! ***");
}
}
}
3.6.3
Further Details
Though the preceding sub-section provides enough information to allow the reader
to implement a basic non-blocking server, there are other methods that are often
required in more sophisticated implementations. Not all of the remaining methods
associated with Java's NIO will be covered in this section, but the reader should fi nd
that those that are covered are the only additional ones that are needed for many
Search WWH ::




Custom Search