1040N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1040N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1040N/A *
1040N/A * This code is free software; you can redistribute it and/or modify it
1040N/A * under the terms of the GNU General Public License version 2 only, as
1040N/A * published by the Free Software Foundation.
1040N/A *
1040N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1040N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1040N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1040N/A * version 2 for more details (a copy is included in the LICENSE file that
1040N/A * accompanied this code).
1040N/A *
1040N/A * You should have received a copy of the GNU General Public License version
1040N/A * 2 along with this work; if not, write to the Free Software Foundation,
1040N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1040N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1040N/A */
1040N/A
1040N/Aimport java.net.*;
1040N/Aimport java.io.*;
1040N/Aimport java.nio.channels.*;
1040N/Aimport java.util.Enumeration;
1040N/A
1040N/A/**
1040N/A * Sanity check Socket/ServerSocket and each of the stream-oriented channels
1040N/A * on each IP address plumbed to the network adapters.
1040N/A */
1040N/A
1040N/Apublic class Sanity {
1040N/A public static void main(String[] args) throws Exception {
1040N/A Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
1040N/A while (nifs.hasMoreElements()) {
1040N/A NetworkInterface ni = nifs.nextElement();
1040N/A Enumeration<InetAddress> addrs = ni.getInetAddresses();
1040N/A while (addrs.hasMoreElements()) {
1040N/A InetAddress addr = addrs.nextElement();
1040N/A test(addr);
1040N/A }
1040N/A }
1040N/A }
1040N/A
1040N/A static void test(InetAddress addr) throws Exception {
1040N/A System.out.println(addr.getHostAddress());
1040N/A
1040N/A // ServerSocketChannel.bind
1040N/A ServerSocketChannel ssc = ServerSocketChannel.open();
1040N/A try {
1040N/A ssc.bind(new InetSocketAddress(addr, 0));
1040N/A int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
1040N/A
1040N/A // SocketChannel.connect (implicit bind)
1040N/A SocketChannel client = SocketChannel.open();
1040N/A try {
1040N/A client.connect(new InetSocketAddress(addr, port));
1040N/A SocketChannel peer = ssc.accept();
1040N/A try {
1040N/A testConnection(Channels.newOutputStream(client),
1040N/A Channels.newInputStream(peer));
1040N/A } finally {
1040N/A peer.close();
1040N/A }
1040N/A } finally {
1040N/A client.close();
1040N/A }
1040N/A
1040N/A // SocketChannel.connect (explicit bind)
1040N/A client = SocketChannel.open();
1040N/A try {
1040N/A client.bind(new InetSocketAddress(addr, 0))
1040N/A .connect(new InetSocketAddress(addr, port));
1040N/A ssc.accept().close();
1040N/A } finally {
1040N/A client.close();
1040N/A }
1040N/A } finally {
1040N/A ssc.close();
1040N/A }
1040N/A
1040N/A // AsynchronousServerSocketChannel.bind
1040N/A AsynchronousServerSocketChannel server =
1040N/A AsynchronousServerSocketChannel.open();
1040N/A try {
1040N/A server.bind(new InetSocketAddress(addr, 0));
1040N/A int port = ((InetSocketAddress)(server.getLocalAddress())).getPort();
1040N/A
1040N/A // AsynchronousSocketChannel.connect (implicit bind)
1040N/A AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
1040N/A try {
1040N/A client.connect(new InetSocketAddress(addr, port)).get();
1040N/A AsynchronousSocketChannel peer = server.accept().get();
1040N/A try {
1040N/A testConnection(Channels.newOutputStream(client),
1040N/A Channels.newInputStream(peer));
1040N/A } finally {
1040N/A peer.close();
1040N/A }
1040N/A } finally {
1040N/A client.close();
1040N/A }
1040N/A
1040N/A // AsynchronousSocketChannel.connect (explicit bind)
1040N/A client = AsynchronousSocketChannel.open();
1040N/A try {
1040N/A client.bind(new InetSocketAddress(addr, 0))
1040N/A .connect(new InetSocketAddress(addr, port)).get();
1040N/A server.accept().get().close();
1040N/A } finally {
1040N/A client.close();
1040N/A }
1040N/A } finally {
1040N/A server.close();
1040N/A }
1040N/A
1040N/A // ServerSocket.bind
1040N/A ServerSocket ss = new ServerSocket();
1040N/A try {
1040N/A ss.bind(new InetSocketAddress(addr, 0));
1040N/A int port = ss.getLocalPort();
1040N/A
1040N/A // Socket.connect (implicit bind)
1040N/A Socket s = new Socket();
1040N/A try {
1040N/A s.connect(new InetSocketAddress(addr, port));
1040N/A Socket peer = ss.accept();
1040N/A try {
1040N/A testConnection(s.getOutputStream(), peer.getInputStream());
1040N/A } finally {
1040N/A peer.close();
1040N/A }
1040N/A } finally {
1040N/A s.close();
1040N/A }
1040N/A
1040N/A // Socket.connect (explicit bind)
1040N/A s = new Socket();
1040N/A try {
1040N/A s.bind(new InetSocketAddress(addr, 0));
1040N/A s.connect(new InetSocketAddress(addr, port));
1040N/A ss.accept().close();
1040N/A } finally {
1040N/A s.close();
1040N/A }
1040N/A } finally {
1040N/A ss.close();
1040N/A }
1040N/A }
1040N/A
1040N/A static void testConnection(OutputStream out, InputStream in)
1040N/A throws IOException
1040N/A {
1040N/A byte[] msg = "hello".getBytes();
1040N/A out.write(msg);
1040N/A
1040N/A byte[] ba = new byte[100];
1040N/A int nread = 0;
1040N/A while (nread < msg.length) {
1040N/A int n = in.read(ba);
1040N/A if (n < 0)
1040N/A throw new IOException("EOF not expected!");
1040N/A nread += n;
1040N/A }
1040N/A }
1040N/A}