Java Reference
In-Depth Information
serverSocketChannel =
ServerSocketChannel.open();
serverSocketChannel.confi gureBlocking(false);
serverSocket = serverSocketChannel.socket();
/*
ServerSocketChannel created before
ServerSocket largely in order to confi gure
latter as a non-blocking socket by calling
the confi gureBlocking method of the
ServerSocketChannel with argument of 'false'.
(ServerSocket will have a ServerSocketChannel
only if latter is created fi rst.)
*/
InetSocketAddress netAddress =
new InetSocketAddress(PORT);
//Bind socket to port…
serverSocket.bind(netAddress);
//Create a new Selector object for detecting
//input from channels…
selector = Selector.open();
//Register ServerSocketChannel with Selector
//for receiving incoming connections…
serverSocketChannel.register(selector,
SelectionKey.OP_ACCEPT);
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
System.exit(1);
}
processConnections();
}
private static void processConnections()
{
do
{
try
{
//Get number of events (new connection(s)
//and/or data transmissions from existing
//connection(s))…
int numKeys = selector.select();
Search WWH ::




Custom Search