2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, 2002, 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 Unit test for socket-channel adaptors
|
|
26 |
* @library ..
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.io.*;
|
|
30 |
import java.net.*;
|
|
31 |
import java.nio.*;
|
|
32 |
import java.nio.channels.*;
|
|
33 |
import java.nio.charset.*;
|
|
34 |
|
|
35 |
|
|
36 |
public class AdaptSocket {
|
|
37 |
|
|
38 |
static java.io.PrintStream out = System.out;
|
|
39 |
|
|
40 |
static final int ECHO_PORT = 7;
|
|
41 |
static final int DAYTIME_PORT = 13;
|
|
42 |
static final String REMOTE_HOST = TestUtil.HOST;
|
|
43 |
static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
|
|
44 |
|
|
45 |
static void test(String hn, int timeout, boolean shouldTimeout)
|
|
46 |
throws Exception
|
|
47 |
{
|
|
48 |
out.println();
|
|
49 |
|
|
50 |
InetSocketAddress isa
|
|
51 |
= new InetSocketAddress(InetAddress.getByName(hn),
|
|
52 |
DAYTIME_PORT);
|
|
53 |
SocketChannel sc = SocketChannel.open();
|
|
54 |
Socket so = sc.socket();
|
|
55 |
out.println("opened: " + so);
|
|
56 |
out.println(" " + sc);
|
|
57 |
|
|
58 |
//out.println("opts: " + sc.options());
|
|
59 |
so.setTcpNoDelay(true);
|
|
60 |
//so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
|
|
61 |
so.setKeepAlive(true);
|
|
62 |
so.setSoLinger(true, 42);
|
|
63 |
so.setOOBInline(true);
|
|
64 |
so.setReceiveBufferSize(512);
|
|
65 |
so.setSendBufferSize(512);
|
|
66 |
//out.println(" " + sc.options());
|
|
67 |
|
|
68 |
if (timeout == 0)
|
|
69 |
so.connect(isa);
|
|
70 |
else {
|
|
71 |
try {
|
|
72 |
so.connect(isa, timeout);
|
|
73 |
} catch (SocketTimeoutException x) {
|
|
74 |
if (shouldTimeout) {
|
|
75 |
out.println("Connection timed out, as expected");
|
|
76 |
return;
|
|
77 |
} else {
|
|
78 |
throw x;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
out.println("connected: " + so);
|
|
83 |
out.println(" " + sc);
|
|
84 |
byte[] bb = new byte[100];
|
|
85 |
int n = so.getInputStream().read(bb);
|
|
86 |
String s = new String(bb, 0, n - 2, "US-ASCII");
|
|
87 |
out.println(isa + " says: \"" + s + "\"");
|
|
88 |
so.shutdownInput();
|
|
89 |
out.println("ishut: " + sc);
|
|
90 |
so.shutdownOutput();
|
|
91 |
out.println("oshut: " + sc);
|
|
92 |
so.close();
|
|
93 |
out.println("closed: " + so);
|
|
94 |
out.println(" " + sc);
|
|
95 |
}
|
|
96 |
|
|
97 |
static String dataString = "foo\r\n";
|
|
98 |
|
|
99 |
static void testRead(Socket so, boolean shouldTimeout)
|
|
100 |
throws Exception
|
|
101 |
{
|
|
102 |
String data = "foo\r\n";
|
|
103 |
so.getOutputStream().write(dataString.getBytes("US-ASCII"));
|
|
104 |
InputStream is = so.getInputStream();
|
|
105 |
try {
|
|
106 |
byte[] b = new byte[100];
|
|
107 |
int n = is.read(b);
|
|
108 |
if (n != 5)
|
|
109 |
throw new Exception("Incorrect number of bytes read: " + n);
|
|
110 |
if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
|
|
111 |
throw new Exception("Incorrect data read: " + n);
|
|
112 |
} catch (SocketTimeoutException x) {
|
|
113 |
if (shouldTimeout) {
|
|
114 |
out.println("Read timed out, as expected");
|
|
115 |
return;
|
|
116 |
}
|
|
117 |
throw x;
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
static void testRead(String hn, int timeout, boolean shouldTimeout)
|
|
122 |
throws Exception
|
|
123 |
{
|
|
124 |
out.println();
|
|
125 |
|
|
126 |
InetSocketAddress isa
|
|
127 |
= new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
|
|
128 |
SocketChannel sc = SocketChannel.open();
|
|
129 |
sc.connect(isa);
|
|
130 |
Socket so = sc.socket();
|
|
131 |
out.println("connected: " + so);
|
|
132 |
out.println(" " + sc);
|
|
133 |
|
|
134 |
if (timeout > 0)
|
|
135 |
so.setSoTimeout(timeout);
|
|
136 |
out.println("timeout: " + so.getSoTimeout());
|
|
137 |
|
|
138 |
testRead(so, shouldTimeout);
|
|
139 |
if (!TestUtil.onME())
|
|
140 |
for (int i = 0; i < 4; i++)
|
|
141 |
testRead(so, shouldTimeout);
|
|
142 |
|
|
143 |
sc.close();
|
|
144 |
}
|
|
145 |
|
|
146 |
public static void main(String[] args) throws Exception {
|
|
147 |
|
|
148 |
test(REMOTE_HOST, 0, false);
|
|
149 |
test(REMOTE_HOST, 1000, false);
|
|
150 |
test(VERY_REMOTE_HOST, 10, true);
|
|
151 |
|
|
152 |
testRead(REMOTE_HOST, 0, false);
|
|
153 |
testRead(REMOTE_HOST, 8000, false);
|
|
154 |
testRead(VERY_REMOTE_HOST, 10, true);
|
|
155 |
|
|
156 |
}
|
|
157 |
|
|
158 |
}
|