List of usage examples for java.net ServerSocket setReuseAddress
public void setReuseAddress(boolean on) throws SocketException
From source file:org.apache.marmotta.platform.core.test.base.JettyMarmotta.java
public static boolean isPortAvailable(final int port) { ServerSocket ss = null; try {/*from www. j a v a 2 s.c o m*/ ss = new ServerSocket(port); ss.setReuseAddress(true); return true; } catch (final IOException e) { } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { } } } return false; }
From source file:it.geosolutions.geostore.services.rest.SecurityTest.java
/** * Checks if a network host / port is already occupied. * //from ww w . ja v a 2s . c o m * @param host * @param port * @return */ private static boolean portIsBusy(String host, int port) { ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return false; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return true; }
From source file:org.exoplatform.addons.es.integration.BaseIntegrationTest.java
/** * Get a random available port/*from w w w. jav a 2 s .c o m*/ * @return * @throws IOException */ private static int getAvailablePort() throws IOException { ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(0); ss.setReuseAddress(true); ds = new DatagramSocket(0); ds.setReuseAddress(true); return ss.getLocalPort(); } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } }
From source file:org.apache.tajo.rpc.NettyServerBase.java
private static boolean available(int port) throws IOException { if (port < 1024 || port > 65535) { throw new IllegalArgumentException("Port Number Out of Bound: " + port); }/*from ww w. ja va 2 s . c o m*/ ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { return false; } finally { if (ss != null) { ss.close(); } if (ds != null) { ds.close(); } } }
From source file:org.kiji.schema.cassandra.TestingCassandraFactory.java
/** * Find an available port./* w w w . j a va 2 s. c om*/ * * @return an open port number. * @throws IllegalArgumentException if it can't find an open port. */ private static int findOpenPort() { try { ServerSocket serverSocket = new ServerSocket(0); int portNumber = serverSocket.getLocalPort(); serverSocket.setReuseAddress(true); serverSocket.close(); LOG.debug("Found usable port {}", portNumber); return portNumber; } catch (IOException ioe) { throw new RuntimeException("Could not find open port."); } }
From source file:ubicrypt.core.TestUtils.java
/** * Returns a free port number on localhost. * * <p>Heavily inspired from org.eclipse.jdt.launching.SocketUtil (to avoid a dependency to JDT * just because of this). Slightly improved with stop() missing in JDT. And throws exception * instead of returning -1./*from w w w . j a v a2 s.c o m*/ * * @return a free port number on localhost * @throws IllegalStateException if unable to find a free port */ public static int findFreePort() { ServerSocket socket = null; do { try { final int port = ThreadLocalRandom.current().nextInt(49152, 65535 + 1); socket = new ServerSocket(port); socket.setReuseAddress(false); try { socket.close(); } catch (final IOException e) { // Ignore IOException on stop() } return port; } catch (final IOException e) { } finally { if (socket != null) { try { socket.close(); } catch (final IOException e) { } } } } while (true); }
From source file:fr.fastconnect.factory.tibco.bw.maven.bwengine.AbstractServiceEngineMojo.java
public static boolean available(int port, int minPort, int maxPort) { if (port < minPort || port > maxPort || port > 65535) { throw new IllegalArgumentException("Invalid start port: " + port); }//w ww .j av a 2s . co m ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; }
From source file:gridool.communication.transport.tcp.GridNioServer.java
private static void startListening(final Selector connectSelector, final int port) throws IOException { ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); ServerSocket servSocket = serverChannel.socket(); servSocket.setReuseAddress(true); servSocket.bind(new InetSocketAddress(port)); serverChannel.register(connectSelector, SelectionKey.OP_ACCEPT); if (LOG.isInfoEnabled()) { LOG.info("GridNioServer is started at port: " + port); }//from w ww . j av a 2 s . co m }
From source file:io.github.kitarek.elasthttpd.server.networking.HttpConfiguredServerSocket.java
private static void setSocketReuseAddress(final SocketConfiguration socketConfiguration, final ServerSocket serverSocket) { socketConfiguration.getAddressAndPortReusePolicy().map(new OptionalMapper<AddressAndPortReusePolicy>() { public void present(AddressAndPortReusePolicy addressAndPortReusePolicy) { try { serverSocket.setReuseAddress( addressAndPortReusePolicy == REUSE_PORT_AND_IP_ADDRESS_RISKING_DELIVERY_OF_OLD_PACKETS); } catch (SocketException e) { throw new IllegalStateException("Cannot set socket receive buffer size", e); }/*from w w w . j a v a2 s . c om*/ } }); }
From source file:org.jumpmind.util.AppUtils.java
/** * Checks to see if a specific port is available. * * @param port/*w w w .j ava2 s . c o m*/ * the port to check for availability */ public static boolean isPortAvailable(int port) { if (port < 1 || port > 65535) { throw new IllegalArgumentException("Invalid start port: " + port); } ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; }