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;
}
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();
}
}
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();
}
}
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");
}
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;
}
Aggregations