//********* Non-blocking client *************************************** import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.net.InetSocketAddress; public class NBC { // NonBlockingClient public static void main(String[ ] args) throws Exception { if (args.length < 1) { System.err.println("Usage: NBC "); return; } // Create a ByteBuffer to receive data. ByteBuffer buff = ByteBuffer.allocate(buff_size); // Open a socket channel and configure it as blocking. SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); // Attempt to connect: as blocking is off, the connect // call returns at once sc.connect(new InetSocketAddress(args[0], port)); // Now wait until the connection is finished. // (There's a better way.) while (!sc.finishConnect()) Thread.sleep(500); // Read from the channel when data are available. // (There's a better way.) while (sc.read(buff) != -1) // -1 marks end of stream Thread.sleep(500); sc.close(); System.out.println(new String(buff.array())); } private static final int port = 2233; private static final int buff_size = 512; // in bytes }