1 /*
2  * Copyright (c) 2009, 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 import java.net.*;
25 import java.io.*;
26 import java.nio.channels.*;
27 import java.util.Enumeration;
28 
29 /**
30  * Sanity check Socket/ServerSocket and each of the stream-oriented channels
31  * on each IP address plumbed to the network adapters.
32  */
33 
34 public class Sanity {
main(String[] args)35     public static void main(String[] args) throws Exception {
36         Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
37         while (nifs.hasMoreElements()) {
38             NetworkInterface ni = nifs.nextElement();
39             Enumeration<InetAddress> addrs = ni.getInetAddresses();
40             while (addrs.hasMoreElements()) {
41                 InetAddress addr = addrs.nextElement();
42                 test(addr);
43             }
44         }
45     }
46 
test(InetAddress addr)47     static void test(InetAddress addr) throws Exception {
48         System.out.println(addr.getHostAddress());
49 
50         // ServerSocketChannel.bind
51         ServerSocketChannel ssc = ServerSocketChannel.open();
52         try {
53             ssc.bind(new InetSocketAddress(addr, 0));
54             int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
55 
56             // SocketChannel.connect (implicit bind)
57             SocketChannel client = SocketChannel.open();
58             try {
59                 client.connect(new InetSocketAddress(addr, port));
60                 SocketChannel peer = ssc.accept();
61                 try {
62                     testConnection(Channels.newOutputStream(client),
63                                    Channels.newInputStream(peer));
64                 } finally {
65                     peer.close();
66                 }
67             } finally {
68                 client.close();
69             }
70 
71             // SocketChannel.connect (explicit bind)
72             client = SocketChannel.open();
73             try {
74                 client.bind(new InetSocketAddress(addr, 0))
75                   .connect(new InetSocketAddress(addr, port));
76                 ssc.accept().close();
77             } finally {
78                 client.close();
79             }
80         } finally {
81             ssc.close();
82         }
83 
84         // AsynchronousServerSocketChannel.bind
85         AsynchronousServerSocketChannel server =
86             AsynchronousServerSocketChannel.open();
87         try {
88             server.bind(new InetSocketAddress(addr, 0));
89             int port = ((InetSocketAddress)(server.getLocalAddress())).getPort();
90 
91             // AsynchronousSocketChannel.connect (implicit bind)
92             AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
93             try {
94                 client.connect(new InetSocketAddress(addr, port)).get();
95                 AsynchronousSocketChannel peer = server.accept().get();
96                 try {
97                     testConnection(Channels.newOutputStream(client),
98                                    Channels.newInputStream(peer));
99                 } finally {
100                     peer.close();
101                 }
102             } finally {
103                 client.close();
104             }
105 
106             // AsynchronousSocketChannel.connect (explicit bind)
107             client = AsynchronousSocketChannel.open();
108             try {
109                 client.bind(new InetSocketAddress(addr, 0))
110                   .connect(new InetSocketAddress(addr, port)).get();
111                 server.accept().get().close();
112             } finally {
113                 client.close();
114             }
115         } finally {
116             server.close();
117         }
118 
119         // ServerSocket.bind
120         ServerSocket ss = new ServerSocket();
121         try {
122             ss.bind(new InetSocketAddress(addr, 0));
123             int port = ss.getLocalPort();
124 
125             // Socket.connect (implicit bind)
126             Socket s = new Socket();
127             try {
128                 s.connect(new InetSocketAddress(addr, port));
129                 Socket peer = ss.accept();
130                 try {
131                     testConnection(s.getOutputStream(), peer.getInputStream());
132                 } finally {
133                     peer.close();
134                 }
135             } finally {
136                 s.close();
137             }
138 
139             // Socket.connect (explicit bind)
140             s = new Socket();
141             try {
142                 s.bind(new InetSocketAddress(addr, 0));
143                 s.connect(new InetSocketAddress(addr, port));
144                 ss.accept().close();
145             } finally {
146                 s.close();
147             }
148         } finally {
149             ss.close();
150         }
151     }
152 
testConnection(OutputStream out, InputStream in)153     static void testConnection(OutputStream out, InputStream in)
154         throws IOException
155     {
156         byte[] msg = "hello".getBytes();
157         out.write(msg);
158 
159         byte[] ba = new byte[100];
160         int nread = 0;
161         while (nread < msg.length) {
162             int n = in.read(ba);
163             if (n < 0)
164                 throw new IOException("EOF not expected!");
165             nread += n;
166         }
167     }
168 }
169