List of usage examples for java.net ServerSocket getLocalPort
public int getLocalPort()
From source file:org.codice.ddf.catalog.ui.forms.SearchFormsSymbolsIT.java
private static void tryCloseSocket(@Nullable ServerSocket socket) { try {/* w w w. j av a2 s. co m*/ if (socket != null) { socket.close(); } } catch (IOException e) { throw new AssertionError( "Problem while enumerating ports (specifically, port " + socket.getLocalPort() + ")", e); } }
From source file:test.integ.be.agiv.security.IPSTSTest.java
private static int getFreePort() throws Exception { ServerSocket serverSocket = new ServerSocket(0); int port = serverSocket.getLocalPort(); serverSocket.close();//w ww.j a v a 2s . c o m return port; }
From source file:org.openspaces.test.client.executor.ExecutorUtils.java
/** * Get an anonymous port.//w ww . j a v a 2s. c o m * * @return An anonymous port created by instantiating a <code>java.net.ServerSocket</code> with * a port of 0. */ public static int getAnonymousPort() { // default port int port = 45467; java.net.ServerSocket socket; try { socket = new java.net.ServerSocket(0); port = socket.getLocalPort(); socket.close(); } catch (IOException e) { if (_logger.isWarnEnabled()) _logger.warn("Failed to get anonymous socket port.", e); } return port; }
From source file:org.paxml.selenium.rc.XSelenium.java
private static int getAvailablePort() { ServerSocket s = null; try {//from w ww .ja v a 2 s.c o m s = new ServerSocket(0); return s.getLocalPort(); } catch (IOException e) { throw new PaxmlRuntimeException(e); } finally { try { if (s != null) { s.close(); } } catch (Exception e) { log.warn("Cannot close server socket of port: " + s.getLocalPort(), e); } } }
From source file:com.ngdata.hbaseindexer.mr.HBaseIndexingOptionsTest.java
private static int getFreePort() { ServerSocket socket = null; try {//w ww. ja va2s .c o m socket = new ServerSocket(0); return socket.getLocalPort(); } catch (IOException e) { throw new RuntimeException("Error finding a free port", e); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { throw new RuntimeException("Error closing ServerSocket used to detect a free port.", e); } } } }
From source file:org.apache.smscserver.test.TestUtil.java
/** * Attempts to find a free port/*from w w w .j a va 2 s .c om*/ * * @param initPort * The first port to try, before resolving to brute force searching * @throws IOException * * @throws IOException */ public static int findFreePort(int initPort) throws IOException { int port = -1; ServerSocket tmpSocket = null; // first try the default port try { tmpSocket = new ServerSocket(initPort); port = initPort; System.out.println("Using default port: " + port); } catch (IOException e) { System.out.println("Failed to use specified port"); // didn't work, try to find one dynamically try { int attempts = 0; while ((port < 1024) && (attempts < 2000)) { attempts++; tmpSocket = new ServerSocket(0); port = tmpSocket.getLocalPort(); } } catch (IOException e1) { throw new IOException("Failed to find a port to use for testing: " + e1.getMessage()); } } finally { if (tmpSocket != null) { try { tmpSocket.close(); } catch (IOException e) { // ignore } tmpSocket = null; } } return port; }
From source file:org.apache.helix.TestHelper.java
public static int getRandomPort() throws IOException { ServerSocket sock = new ServerSocket(); sock.bind(null);//from ww w . j av a 2 s.co m int port = sock.getLocalPort(); sock.close(); return port; }
From source file:org.apache.ftpserver.test.TestUtil.java
/** * Attempts to find a free port/*from ww w . java2s . co m*/ * @param initPort The first port to try, before resolving to * brute force searching * @throws IOException * * @throws IOException */ public static int findFreePort(int initPort) throws IOException { int port = -1; ServerSocket tmpSocket = null; // first try the default port try { tmpSocket = new ServerSocket(initPort); port = initPort; System.out.println("Using default port: " + port); } catch (IOException e) { System.out.println("Failed to use specified port"); // didn't work, try to find one dynamically try { int attempts = 0; while (port < 1024 && attempts < 2000) { attempts++; tmpSocket = new ServerSocket(0); port = tmpSocket.getLocalPort(); } } catch (IOException e1) { throw new IOException("Failed to find a port to use for testing: " + e1.getMessage()); } } finally { if (tmpSocket != null) { try { tmpSocket.close(); } catch (IOException e) { // ignore } tmpSocket = null; } } return port; }
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Return a free port number. There is no guarantee it will remain free, so it should be used immediately. * * @returns A free port for binding a local socket *//* www. ja v a2s . c o m*/ public static int getFreeSocketPort() { int port = 0; try { ServerSocket s = new ServerSocket(0); port = s.getLocalPort(); s.close(); return port; } catch (IOException e) { // Could not get a free port. Return default port 0. } return port; }
From source file:org.apache.flink.runtime.clusterframework.BootstrapTools.java
/** * Starts an ActorSystem with the given configuration listening at the address/ports. * @param configuration The Flink configuration * @param listeningAddress The address to listen at. * @param portRangeDefinition The port range to choose a port from. * @param logger The logger to output log information. * @return The ActorSystem which has been started * @throws Exception/* www . j a v a 2 s . c o m*/ */ public static ActorSystem startActorSystem(Configuration configuration, String listeningAddress, String portRangeDefinition, Logger logger) throws Exception { // parse port range definition and create port iterator Iterator<Integer> portsIterator; try { portsIterator = NetUtils.getPortRangeFromString(portRangeDefinition); } catch (Exception e) { throw new IllegalArgumentException("Invalid port range definition: " + portRangeDefinition); } while (portsIterator.hasNext()) { // first, we check if the port is available by opening a socket // if the actor system fails to start on the port, we try further ServerSocket availableSocket = NetUtils.createSocketFromPorts(portsIterator, new NetUtils.SocketFactory() { @Override public ServerSocket createSocket(int port) throws IOException { return new ServerSocket(port); } }); int port; if (availableSocket == null) { throw new BindException("Unable to allocate further port in port range: " + portRangeDefinition); } else { port = availableSocket.getLocalPort(); try { availableSocket.close(); } catch (IOException ignored) { } } try { return startActorSystem(configuration, listeningAddress, port, logger); } catch (Exception e) { // we can continue to try if this contains a netty channel exception Throwable cause = e.getCause(); if (!(cause instanceof org.jboss.netty.channel.ChannelException || cause instanceof java.net.BindException)) { throw e; } // else fall through the loop and try the next port } } // if we come here, we have exhausted the port range throw new BindException("Could not start actor system on any port in port range " + portRangeDefinition); }