01: //********* Blocking client *************************************** 02: import java.nio.ByteBuffer; 03: import java.nio.channels.SocketChannel; 04: import java.net.InetSocketAddress; 05: 06: public class BC { // BlockingClient 07: public static void main(String[ ] args) throws Exception { 08: if (args.length < 1) { 09: System.err.println("Usage: BC <server's IP addr>"); 10: return; 11: } 12: // Create a ByteBuffer to receive data. 13: ByteBuffer buff = ByteBuffer.allocate(buff_size); 14: 15: // Open a socket channel and configure it as blocking. 16: SocketChannel sc = SocketChannel.open(); 17: sc.configureBlocking(true); 18: 19: // Attempt to connect: as blocking is on, the call 20: // blocks until successful or an exception is thrown. 21: sc.connect(new InetSocketAddress(args[0], port)); 22: sc.read(buff); // also blocks until data are available 23: sc.close(); 24: System.out.println(new String(buff.array())); 25: } 26: private static final int port = 2233; 27: private static final int buff_size = 512; // in bytes 28: }