1 /* |
1 /* |
2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
42 * registered for the connectSelector. |
40 * registered for the connectSelector. |
43 */ |
41 */ |
44 |
42 |
45 public class BasicConnect { |
43 public class BasicConnect { |
46 |
44 |
47 static final int PORT = 7; // echo |
|
48 static final String HOST = TestUtil.HOST; |
|
49 |
|
50 public static void main(String[] args) throws Exception { |
45 public static void main(String[] args) throws Exception { |
51 Selector connectSelector = |
46 Selector connectSelector = |
52 SelectorProvider.provider().openSelector(); |
47 SelectorProvider.provider().openSelector(); |
53 InetSocketAddress isa |
48 try (TestServers.EchoServer echoServer |
54 = new InetSocketAddress(InetAddress.getByName(HOST), PORT); |
49 = TestServers.EchoServer.startNewServer(100)) { |
55 SocketChannel sc = SocketChannel.open(); |
50 InetSocketAddress isa |
56 sc.configureBlocking(false); |
51 = new InetSocketAddress(echoServer.getAddress(), |
57 boolean result = sc.connect(isa); |
52 echoServer.getPort()); |
58 while (!result) { |
53 SocketChannel sc = SocketChannel.open(); |
59 SelectionKey connectKey = sc.register(connectSelector, |
54 sc.configureBlocking(false); |
60 SelectionKey.OP_CONNECT); |
55 boolean result = sc.connect(isa); |
61 int keysAdded = connectSelector.select(); |
56 if (result) { |
62 if (keysAdded > 0) { |
57 System.out.println("Socket immediately connected on " |
63 Set readyKeys = connectSelector.selectedKeys(); |
58 + System.getProperty("os.name") |
64 Iterator i = readyKeys.iterator(); |
59 + ": " + sc); |
65 while (i.hasNext()) { |
60 } |
66 SelectionKey sk = (SelectionKey)i.next(); |
61 while (!result) { |
67 i.remove(); |
62 SelectionKey connectKey = sc.register(connectSelector, |
68 SocketChannel nextReady = (SocketChannel)sk.channel(); |
63 SelectionKey.OP_CONNECT); |
69 result = nextReady.finishConnect(); |
64 int keysAdded = connectSelector.select(); |
70 if (result) |
65 if (keysAdded > 0) { |
71 sk.cancel(); |
66 Set readyKeys = connectSelector.selectedKeys(); |
|
67 Iterator i = readyKeys.iterator(); |
|
68 while (i.hasNext()) { |
|
69 SelectionKey sk = (SelectionKey)i.next(); |
|
70 i.remove(); |
|
71 SocketChannel nextReady = (SocketChannel)sk.channel(); |
|
72 result = nextReady.finishConnect(); |
|
73 if (result) |
|
74 sk.cancel(); |
|
75 } |
72 } |
76 } |
73 } |
77 } |
|
78 |
|
79 byte[] bs = new byte[] { (byte)0xca, (byte)0xfe, |
|
80 (byte)0xba, (byte)0xbe }; |
|
81 ByteBuffer bb = ByteBuffer.wrap(bs); |
|
82 sc.configureBlocking(true); |
|
83 sc.write(bb); |
|
84 bb.rewind(); |
|
85 |
|
86 ByteBuffer bb2 = ByteBuffer.allocateDirect(100); |
|
87 int n = sc.read(bb2); |
|
88 bb2.flip(); |
|
89 |
|
90 sc.close(); |
|
91 connectSelector.close(); |
|
92 |
|
93 if (!bb.equals(bb2)) |
|
94 throw new Exception("Echoed bytes incorrect: Sent " |
|
95 + bb + ", got " + bb2); |
74 } |
96 } |
75 |
|
76 byte[] bs = new byte[] { (byte)0xca, (byte)0xfe, |
|
77 (byte)0xba, (byte)0xbe }; |
|
78 ByteBuffer bb = ByteBuffer.wrap(bs); |
|
79 sc.configureBlocking(true); |
|
80 sc.write(bb); |
|
81 bb.rewind(); |
|
82 |
|
83 ByteBuffer bb2 = ByteBuffer.allocateDirect(100); |
|
84 int n = sc.read(bb2); |
|
85 bb2.flip(); |
|
86 |
|
87 sc.close(); |
|
88 connectSelector.close(); |
|
89 |
|
90 if (!bb.equals(bb2)) |
|
91 throw new Exception("Echoed bytes incorrect: Sent " |
|
92 + bb + ", got " + bb2); |
|
93 } |
97 } |
94 |
|
95 } |
98 } |