1 /*
2  * Copyright (c) 2000, 2016, 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  * @bug 4286936 8143100
26  * @summary Unit test for server-socket channels
27  * @library ..
28  */
29 
30 import java.io.*;
31 import java.net.*;
32 import java.nio.*;
33 import java.nio.channels.*;
34 
35 
36 public class Basic {
37 
38     static PrintStream log = System.err;
39 
40     static class Server
41         extends TestThread
42     {
43         ServerSocketChannel ssc;
44         boolean block;
45 
Server(ServerSocketChannel ssc, boolean block)46         Server(ServerSocketChannel ssc, boolean block) {
47             super("Server", Basic.log);
48             this.ssc = ssc;
49             this.block = block;
50         }
51 
go()52         void go() throws Exception {
53             log.println("Server: Listening "
54                         + (block ? "(blocking)" : "(non-blocking)"));
55             if (!block)
56                 ssc.configureBlocking(false);
57             log.println("  " + ssc);
58             //log.println("  " + ssc.options());
59             SocketChannel sc = null;
60             for (;;) {
61                 sc = ssc.accept();
62                 if (sc != null) {
63                     break;
64                 }
65                 log.println("Server: Sleeping...");
66                 Thread.sleep(50);
67             }
68             log.println("Server: Accepted " + sc);
69             ByteBuffer bb = ByteBuffer.allocateDirect(100);
70             if (sc.read(bb) != 1)
71                 throw new Exception("Read failed");
72             bb.flip();
73             byte b = bb.get();
74             log.println("Server: Read " + b + ", writing " + (b + 1));
75             bb.clear();
76             bb.put((byte)43);
77             bb.flip();
78             if (sc.write(bb) != 1)
79                 throw new Exception("Write failed");
80             sc.close();
81             ssc.close();
82             log.println("Server: Finished");
83         }
84 
85     }
86 
87     static class Client
88         extends TestThread
89     {
90         int port;
91         boolean dally;
92 
Client(int port, boolean block)93         Client(int port, boolean block) {
94             super("Client", Basic.log);
95             this.port = port;
96             this.dally = !block;
97         }
98 
go()99         public void go() throws Exception {
100             if (dally)
101                 Thread.sleep(200);
102             InetSocketAddress isa
103                 = new InetSocketAddress(InetAddress.getLocalHost(), port);
104             log.println("Client: Connecting to " + isa);
105             SocketChannel sc = SocketChannel.open();
106             sc.connect(isa);
107             log.println("Client: Connected");
108             ByteBuffer bb = ByteBuffer.allocateDirect(512);
109             bb.put((byte)42).flip();
110             log.println("Client: Writing " + bb.get(0));
111             if (sc.write(bb) != 1)
112                 throw new Exception("Write failed");
113             bb.clear();
114             if (sc.read(bb) != 1)
115                 throw new Exception("Read failed");
116             bb.flip();
117             if (bb.get() != 43)
118                 throw new Exception("Read " + bb.get(bb.position() - 1));
119             log.println("Client: Read " + bb.get(0));
120             sc.close();
121             log.println("Client: Finished");
122         }
123 
124     }
125 
test(boolean block)126     static void test(boolean block) throws Exception {
127         ServerSocketChannel ssc = ServerSocketChannel.open();
128         ssc.socket().setReuseAddress(true);
129         int port = TestUtil.bind(ssc);
130         Server server = new Server(ssc, block);
131         Client client = new Client(port, block);
132         server.start();
133         client.start();
134         if ((server.finish(0) & client.finish(0)) == 0)
135             throw new Exception("Failure");
136         log.println();
137     }
138 
main(String[] args)139     public static void main(String[] args) throws Exception {
140         log.println();
141         test(true);
142         test(false);
143     }
144 
145 }
146