Search in sources :

Example 96 with ServerSocket

use of java.net.ServerSocket in project neo4j by neo4j.

the class Ports method findFreePort.

public static InetSocketAddress findFreePort(String host, int[] portRange) throws IOException {
    InetSocketAddress address = null;
    IOException ex = null;
    for (int port = portRange[0]; port <= portRange[1]; port++) {
        if (host == null || host.equals(INADDR_ANY)) {
            address = new InetSocketAddress(port);
        } else {
            address = new InetSocketAddress(host, port);
        }
        try {
            new ServerSocket(address.getPort(), 100, address.getAddress()).close();
            ex = null;
            break;
        } catch (IOException e) {
            ex = e;
        }
    }
    if (ex != null) {
        throw ex;
    }
    return address;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 97 with ServerSocket

use of java.net.ServerSocket in project neo4j by neo4j.

the class NeoServerPortConflictIT method shouldComplainIfServerHTTPSPortIsAlreadyTaken.

@Test
public void shouldComplainIfServerHTTPSPortIsAlreadyTaken() throws IOException, InterruptedException {
    ListenSocketAddress unContestedAddress = new ListenSocketAddress("localhost", 8888);
    ListenSocketAddress contestedAddress = new ListenSocketAddress("localhost", 9999);
    try (ServerSocket ignored = new ServerSocket(contestedAddress.getPort(), 0, InetAddress.getByName(contestedAddress.getHostname()))) {
        AssertableLogProvider logProvider = new AssertableLogProvider();
        CommunityNeoServer server = CommunityServerBuilder.server(logProvider).onAddress(unContestedAddress).onHttpsAddress(contestedAddress).withHttpsEnabled().usingDataDir(folder.directory(name.getMethodName()).getAbsolutePath()).build();
        try {
            server.start();
            fail("Should have reported failure to start");
        } catch (ServerStartupException e) {
            assertThat(e.getMessage(), containsString("Starting Neo4j failed"));
        }
        logProvider.assertAtLeastOnce(AssertableLogProvider.inLog(containsString("CommunityNeoServer")).error("Failed to start Neo4j on %s: %s", unContestedAddress, format("At least one of the addresses %s or %s is already in use, cannot bind to it.", unContestedAddress, contestedAddress)));
        server.stop();
    }
}
Also used : ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) ServerSocket(java.net.ServerSocket) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Example 98 with ServerSocket

use of java.net.ServerSocket in project neo4j by neo4j.

the class NeoServerPortConflictIT method shouldComplainIfServerPortIsAlreadyTaken.

@Test
public void shouldComplainIfServerPortIsAlreadyTaken() throws IOException, InterruptedException {
    ListenSocketAddress contestedAddress = new ListenSocketAddress("localhost", 9999);
    try (ServerSocket ignored = new ServerSocket(contestedAddress.getPort(), 0, InetAddress.getByName(contestedAddress.getHostname()))) {
        AssertableLogProvider logProvider = new AssertableLogProvider();
        CommunityNeoServer server = CommunityServerBuilder.server(logProvider).onAddress(contestedAddress).usingDataDir(folder.directory(name.getMethodName()).getAbsolutePath()).build();
        try {
            server.start();
            fail("Should have reported failure to start");
        } catch (ServerStartupException e) {
            assertThat(e.getMessage(), containsString("Starting Neo4j failed"));
        }
        logProvider.assertAtLeastOnce(AssertableLogProvider.inLog(containsString("CommunityNeoServer")).error("Failed to start Neo4j on %s: %s", contestedAddress, format("Address %s is already in use, cannot bind to it.", contestedAddress)));
        server.stop();
    }
}
Also used : ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) ServerSocket(java.net.ServerSocket) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Example 99 with ServerSocket

use of java.net.ServerSocket in project neo4j by neo4j.

the class FreePorts method discoverPort.

private int discoverPort(int minPort, int maxPort) throws IOException {
    int port;
    for (port = minPort; port <= maxPort; port++) {
        if (discoveredPorts.contains(port) || deniedPorts.contains(port)) {
            // This port has already been discovered or denied
            continue;
        }
        try {
            // try binding it at wildcard
            ServerSocket ss = new ServerSocket();
            ss.setReuseAddress(false);
            ss.bind(new InetSocketAddress(port));
            ss.close();
        } catch (IOException e) {
            deniedPorts.add(port);
            continue;
        }
        try {
            // try binding it at loopback
            ServerSocket ss = new ServerSocket();
            ss.setReuseAddress(false);
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
            ss.close();
        } catch (IOException e) {
            deniedPorts.add(port);
            continue;
        }
        try {
            // try connecting to it at loopback
            Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
            socket.close();
            deniedPorts.add(port);
            continue;
        } catch (IOException e) {
            // Port very likely free!
            discoveredPorts.add(port);
            return port;
        }
    }
    throw new IOException("No open port could be found");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 100 with ServerSocket

use of java.net.ServerSocket in project netty by netty.

the class TestUtils method isTcpPortAvailable.

private static boolean isTcpPortAvailable(InetSocketAddress localAddress) {
    ServerSocket ss = null;
    try {
        ss = new ServerSocket();
        ss.setReuseAddress(false);
        ss.bind(localAddress);
        ss.close();
        ss = null;
        return true;
    } catch (Exception ignore) {
    // Unavailable
    } finally {
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException ignore) {
            // Ignore
            }
        }
    }
    return false;
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

ServerSocket (java.net.ServerSocket)655 IOException (java.io.IOException)306 Socket (java.net.Socket)254 InetSocketAddress (java.net.InetSocketAddress)126 Test (org.junit.Test)94 SocketException (java.net.SocketException)54 InputStream (java.io.InputStream)48 SocketTimeoutException (java.net.SocketTimeoutException)42 OutputStream (java.io.OutputStream)40 InetAddress (java.net.InetAddress)39 BindException (java.net.BindException)27 URL (java.net.URL)26 InputStreamReader (java.io.InputStreamReader)23 File (java.io.File)22 UnknownHostException (java.net.UnknownHostException)22 SSLSocket (javax.net.ssl.SSLSocket)21 BufferedReader (java.io.BufferedReader)20 SSLServerSocket (javax.net.ssl.SSLServerSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)15 SocketChannel (java.nio.channels.SocketChannel)14