|
1 /* |
|
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. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @summary Test nonblocking connect and finishConnect |
|
26 * @bug 4457776 |
|
27 * @library .. |
|
28 */ |
|
29 |
|
30 import java.net.*; |
|
31 import java.nio.*; |
|
32 import java.nio.channels.*; |
|
33 import java.nio.channels.spi.SelectorProvider; |
|
34 import java.util.*; |
|
35 |
|
36 |
|
37 /** |
|
38 * Typically there would be more than one channel registered to select |
|
39 * on, this test is just a very simple version with only one channel |
|
40 * registered for the connectSelector. |
|
41 */ |
|
42 |
|
43 public class BasicConnect { |
|
44 |
|
45 public static void main(String[] args) throws Exception { |
|
46 Selector connectSelector = |
|
47 SelectorProvider.provider().openSelector(); |
|
48 try (TestServers.EchoServer echoServer |
|
49 = TestServers.EchoServer.startNewServer(100)) { |
|
50 InetSocketAddress isa |
|
51 = new InetSocketAddress(echoServer.getAddress(), |
|
52 echoServer.getPort()); |
|
53 SocketChannel sc = SocketChannel.open(); |
|
54 sc.configureBlocking(false); |
|
55 boolean result = sc.connect(isa); |
|
56 if (result) { |
|
57 System.out.println("Socket immediately connected on " |
|
58 + System.getProperty("os.name") |
|
59 + ": " + sc); |
|
60 } |
|
61 while (!result) { |
|
62 SelectionKey connectKey = sc.register(connectSelector, |
|
63 SelectionKey.OP_CONNECT); |
|
64 int keysAdded = connectSelector.select(); |
|
65 if (keysAdded > 0) { |
|
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 } |
|
76 } |
|
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); |
|
96 } |
|
97 } |
|
98 } |