Java Reference
In-Depth Information
try {
selector . select ();
} catch ( IOException ex ) {
ex . printStackTrace ();
break ;
}
Set < SelectionKey > readyKeys = selector . selectedKeys ();
Iterator < SelectionKey > iterator = readyKeys . iterator ();
while ( iterator . hasNext ()) {
SelectionKey key = iterator . next ();
iterator . remove ();
try {
if ( key . isAcceptable ()) {
ServerSocketChannel server = ( ServerSocketChannel ) key . channel ();
SocketChannel client = server . accept ();
System . out . println ( "Accepted connection from " + client );
client . configureBlocking ( false );
SelectionKey key2 = client . register ( selector , SelectionKey .
OP_WRITE );
ByteBuffer buffer = ByteBuffer . allocate ( 74 );
buffer . put ( rotation , 0 , 72 );
buffer . put (( byte ) '\r' );
buffer . put (( byte ) '\n' );
buffer . flip ();
key2 . attach ( buffer );
} else if ( key . isWritable ()) {
SocketChannel client = ( SocketChannel ) key . channel ();
ByteBuffer buffer = ( ByteBuffer ) key . attachment ();
if (! buffer . hasRemaining ()) {
// Refill the buffer with the next line
buffer . rewind ();
// Get the old first character
int first = buffer . get ();
// Get ready to change the data in the buffer
buffer . rewind ();
// Find the new first characters position in rotation
int position = first - ' ' + 1 ;
// copy the data from rotation into the buffer
buffer . put ( rotation , position , 72 );
// Store a line break at the end of the buffer
buffer . put (( byte ) '\r' );
buffer . put (( byte ) '\n' );
// Prepare the buffer for writing
buffer . flip ();
}
client . write ( buffer );
}
} catch ( IOException ex ) {
key . cancel ();
try {
Search WWH ::




Custom Search