(ServerSocketChannel) key.channel(); SocketChannel channel = server.accept(); registerChannel (selector,
{ SocketChannel socketChannel = (SocketChannel) key.channel(); int count; buffer.clear(); // Empty buffer // Loop while data is available; channel is nonblockingwhile ((count = socketChannel.read (buffer)) > 0) { buffer.flip(); // Make buffer readable // Send the data; don’t assume it goes all at once while (buffer.hasRemaining()) { socketChannel.write (buffer); } // WARNING: the above loop is evil. Because // it’s writing back to the same nonblocking // channel it read the data from, this code can // potentially spin in a busy loop. In real life // you’d do something more useful than this. buffer.clear(); // Empty buffer } if (count < 0) { // Close channel on EOF, invalidates the key socketChannel.close(); } } // ---------------------------------------------------------- /** * Spew a greeting to the incoming client connection. * @param channel The newly connected SocketChannel to say hello to. */ private void sayHello (SocketChannel channel) throws Exception { buffer.clear(); buffer.put ("Hi there!rn".getBytes()); buffer.flip(); channel.write (buffer); } } Example 4-1 implements a simple server. It creates ServerSocketChannel and Selector objects and registers the channel with the selector. We don't bother saving a reference to the registration key for the server socket because it will never be deregistered. The infinite loop calls select() at the top, which may block indefinitely. When selection is complete, the selected key set is iterated to check for ready channels. If a key indicates that its channel is ready to do an accept(), we obtain the channel associated with the key and cast it to a ServerSocketChannel object. We know it's safe to 150
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services