import java.util.*; import java.net.*; import java.nio.*; import java.nio.channels.*; import java.io.*; public class NonblockingServer { public static void main(String[] args) { try { // set up server sockets for the given ports ServerSocketChannel[] ss = new ServerSocketChannel[args.length]; Selector sel = Selector.open(); for (int i=0; i0 && buf.get(p-1)==0) { eos = true; // we have reached the terminating 0 buf.position(p-1); } buf.flip(); byte[] bb = new byte[buf.remaining()]; buf.get(bb); msg.append(new String(bb)); } // write output Selector writesel = Selector.open(); c.register(writesel, SelectionKey.OP_WRITE); buf.clear(); buf.put((new String("Rumors at port "+ c.socket().getLocalPort()+": "+msg)) .getBytes()); buf.flip(); while (buf.remaining()>0) { writesel.select(); // wait for client to be ready c.write(buf); } c.socket().shutdownOutput(); } catch (Exception e) { // presumably lost connection continue; } c.close(); } } } } catch (IOException e) { System.err.println(e); } } }