author | alanb |
Fri, 10 Sep 2010 18:48:49 +0100 | |
changeset 6538 | cb0da65ba680 |
parent 5970 | d4e98bbfb0be |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @summary Test nonblocking connect and finishConnect |
|
26 |
* @bug 4457776 |
|
27 |
* @library .. |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.net.*; |
|
32 |
import java.nio.*; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.nio.channels.spi.SelectorProvider; |
|
35 |
import java.nio.charset.*; |
|
36 |
import java.util.*; |
|
37 |
||
38 |
||
39 |
/** |
|
40 |
* Typically there would be more than one channel registered to select |
|
41 |
* on, this test is just a very simple version with only one channel |
|
42 |
* registered for the connectSelector. |
|
43 |
*/ |
|
44 |
||
45 |
public class BasicConnect { |
|
46 |
||
47 |
static final int PORT = 7; // echo |
|
48 |
static final String HOST = TestUtil.HOST; |
|
49 |
||
50 |
public static void main(String[] args) throws Exception { |
|
51 |
Selector connectSelector = |
|
52 |
SelectorProvider.provider().openSelector(); |
|
53 |
InetSocketAddress isa |
|
54 |
= new InetSocketAddress(InetAddress.getByName(HOST), PORT); |
|
55 |
SocketChannel sc = SocketChannel.open(); |
|
56 |
sc.configureBlocking(false); |
|
57 |
boolean result = sc.connect(isa); |
|
58 |
while (!result) { |
|
59 |
SelectionKey connectKey = sc.register(connectSelector, |
|
60 |
SelectionKey.OP_CONNECT); |
|
61 |
int keysAdded = connectSelector.select(); |
|
62 |
if (keysAdded > 0) { |
|
63 |
Set readyKeys = connectSelector.selectedKeys(); |
|
64 |
Iterator i = readyKeys.iterator(); |
|
65 |
while (i.hasNext()) { |
|
66 |
SelectionKey sk = (SelectionKey)i.next(); |
|
67 |
i.remove(); |
|
68 |
SocketChannel nextReady = (SocketChannel)sk.channel(); |
|
69 |
result = nextReady.finishConnect(); |
|
70 |
if (result) |
|
71 |
sk.cancel(); |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
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(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
86 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
87 |
sc.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
88 |
connectSelector.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
89 |
|
2 | 90 |
if (!bb.equals(bb2)) |
91 |
throw new Exception("Echoed bytes incorrect: Sent " |
|
92 |
+ bb + ", got " + bb2); |
|
93 |
} |
|
94 |
||
95 |
} |