Java Reference
In-Depth Information
if (numKeys > 0)
{
//Extract event(s) that have been
//triggered …
Set eventKeys =
selector.selectedKeys();
//Set up Iterator to cycle though set
//of events…
Iterator keyCycler =
eventKeys.iterator();
while (keyCycler.hasNext())
{
SelectionKey key =
(SelectionKey)keyCycler.next();
//Retrieve set of ready ops for
//this key (as a bit pattern)…
int keyOps = key.readyOps();
if (
(keyOps & SelectionKey.OP_ACCEPT)
== SelectionKey.OP_ACCEPT)
{//New connection.
acceptConnection(key);
continue;
}
if (
(keyOps & SelectionKey.OP_READ)
== SelectionKey.OP_READ)
{//Data from existing client.
acceptData(key);
}
}
}
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
System.exit(1);
}
}while (true);
}
private static void acceptConnection(
SelectionKey key) throws IOException
{//Accept incoming connection and add to list.
Search WWH ::




Custom Search