35 public class SelectAfterRead { |
35 public class SelectAfterRead { |
36 |
36 |
37 final static int TIMEOUT = 1000; |
37 final static int TIMEOUT = 1000; |
38 |
38 |
39 public static void main(String[] argv) throws Exception { |
39 public static void main(String[] argv) throws Exception { |
|
40 InetAddress lh = InetAddress.getByName(ByteServer.LOCALHOST); |
|
41 |
40 // server: accept connection and write one byte |
42 // server: accept connection and write one byte |
41 ByteServer server = new ByteServer(1); |
43 ByteServer server = new ByteServer(1); |
42 server.start(); |
44 server.start(); |
43 InetSocketAddress isa = new InetSocketAddress( |
|
44 InetAddress.getByName(ByteServer.LOCALHOST), ByteServer.PORT); |
|
45 Selector sel = Selector.open(); |
45 Selector sel = Selector.open(); |
46 SocketChannel sc = SocketChannel.open(); |
46 SocketChannel sc = SocketChannel.open(); |
47 sc.connect(isa); |
47 sc.connect(new InetSocketAddress(lh, server.port())); |
48 sc.read(ByteBuffer.allocate(1)); |
48 sc.read(ByteBuffer.allocate(1)); |
49 sc.configureBlocking(false); |
49 sc.configureBlocking(false); |
50 sc.register(sel, SelectionKey.OP_READ); |
50 sc.register(sel, SelectionKey.OP_READ); |
51 // previously on Windows select would select channel here, although there was |
51 // previously on Windows select would select channel here, although there was |
52 // nothing to read |
52 // nothing to read |
59 // Now we will test a two reads combination |
59 // Now we will test a two reads combination |
60 // server: accept connection and write two bytes |
60 // server: accept connection and write two bytes |
61 server = new ByteServer(2); |
61 server = new ByteServer(2); |
62 server.start(); |
62 server.start(); |
63 sc = SocketChannel.open(); |
63 sc = SocketChannel.open(); |
64 sc.connect(isa); |
64 sc.connect(new InetSocketAddress(lh, server.port())); |
65 sc.configureBlocking(false); |
65 sc.configureBlocking(false); |
66 sel = Selector.open(); |
66 sel = Selector.open(); |
67 sc.register(sel, SelectionKey.OP_READ); |
67 sc.register(sel, SelectionKey.OP_READ); |
68 if (sel.select(TIMEOUT) != 1) |
68 if (sel.select(TIMEOUT) != 1) |
69 throw new Exception("One selected key expected"); |
69 throw new Exception("One selected key expected"); |