Search in sources :

Example 51 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBindError.

@Test
public void testBindError() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);
    try {
        int min = socket.getLocalPort();
        conf.set("TestRange", min + "-" + min);
        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        boolean caught = false;
        try {
            Server.bind(socket2, address2, 10, conf, "TestRange");
        } catch (BindException e) {
            caught = true;
        } finally {
            socket2.close();
        }
        assertTrue("Failed to catch the expected bind exception", caught);
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 52 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class AuthenticatorTestCase method getLocalPort.

protected int getLocalPort() throws Exception {
    ServerSocket ss = new ServerSocket(0);
    int ret = ss.getLocalPort();
    ss.close();
    return ret;
}
Also used : ServerSocket(java.net.ServerSocket)

Example 53 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class ConnectionUtilsTest method testReturnLocalHostAddressUsingHeuristics.

@Test
public void testReturnLocalHostAddressUsingHeuristics() throws Exception {
    try (ServerSocket blocker = new ServerSocket(0, 1, InetAddress.getLocalHost())) {
        // the "blocker" server socket simply does not accept connections
        // this address is consequently "unreachable"
        InetSocketAddress unreachable = new InetSocketAddress("localhost", blocker.getLocalPort());
        final long start = System.nanoTime();
        InetAddress add = ConnectionUtils.findConnectingAddress(unreachable, 2000, 400);
        // check that it did not take forever (max 30 seconds)
        // this check can unfortunately not be too tight, or it will be flaky on some CI infrastructure
        assertTrue(System.nanoTime() - start < 30_000_000_000L);
        // we should have found a heuristic address
        assertNotNull(add);
        // make sure that we returned the InetAddress.getLocalHost as a heuristic
        assertEquals(InetAddress.getLocalHost(), add);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 54 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class ConnectionUtilsTest method testFindConnectingAddressWhenGetLocalHostThrows.

@Test
public void testFindConnectingAddressWhenGetLocalHostThrows() throws Exception {
    PowerMockito.mockStatic(InetAddress.class);
    Mockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()).thenCallRealMethod();
    final InetAddress loopbackAddress = Inet4Address.getByName("127.0.0.1");
    Thread socketServerThread;
    try (ServerSocket socket = new ServerSocket(0, 1, loopbackAddress)) {
        // Make sure that the thread will eventually die even if something else goes wrong
        socket.setSoTimeout(10_000);
        socketServerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    socket.accept();
                } catch (IOException e) {
                // ignore
                }
            }
        });
        socketServerThread.start();
        final InetSocketAddress socketAddress = new InetSocketAddress(loopbackAddress, socket.getLocalPort());
        final InetAddress address = ConnectionUtils.findConnectingAddress(socketAddress, 2000, 400);
        PowerMockito.verifyStatic();
        // Make sure we got an address via alternative means
        assertNotNull(address);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 55 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class IPv6HostnamesITCase method getLocalIPv6Address.

private Inet6Address getLocalIPv6Address() {
    try {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface netInterface = e.nextElement();
            // for each address of the network interface
            Enumeration<InetAddress> ee = netInterface.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress addr = ee.nextElement();
                if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) {
                    // see if it is possible to bind to the address
                    InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
                    try {
                        log.info("Considering address " + addr);
                        // test whether we can bind a socket to that address
                        log.info("Testing whether sockets can bind to " + addr);
                        ServerSocket sock = new ServerSocket();
                        sock.bind(socketAddress);
                        sock.close();
                        // test whether Akka's netty can bind to the address
                        log.info("Testing whether Akka can use " + addr);
                        int port = NetUtils.getAvailablePort();
                        ActorSystem as = AkkaUtils.createActorSystem(new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port)));
                        as.shutdown();
                        log.info("Using address " + addr);
                        return (Inet6Address) addr;
                    } catch (IOException ignored) {
                    // fall through the loop
                    }
                }
            }
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) Configuration(org.apache.flink.configuration.Configuration) InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) IOException(java.io.IOException) Tuple2(org.apache.flink.api.java.tuple.Tuple2) InetAddress(java.net.InetAddress)

Aggregations

ServerSocket (java.net.ServerSocket)736 IOException (java.io.IOException)336 Socket (java.net.Socket)265 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 File (java.io.File)23 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16