Refine search
public static int getAvailablePort() { try (ServerSocket ss = new ServerSocket()) { ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { return getRandomPort(); } }
public static int findRandomAvailablePortOnAllLocalInterfaces() throws IOException { int port; try (ServerSocket socket = new ServerSocket(0);) { port = socket.getLocalPort(); socket.close(); } return port; }
/** This method will close the socket upon first initialization. */ protected InetSocketAddress getWebAddressFromBindSocket() throws IOException { Preconditions.checkNotNull(mWebBindSocket, "mWebBindSocket"); InetSocketAddress socketAddr = new InetSocketAddress(mWebBindSocket.getInetAddress(), mWebBindSocket.getLocalPort()); mWebBindSocket.close(); return socketAddr; }
/** * Check whether the port is available to bind * * @param port port * @return -1 means unavailable, otherwise available * @throws IOException */ public static int tryPort(int port) throws IOException { ServerSocket socket = new ServerSocket(port); int rtn = socket.getLocalPort(); socket.close(); return rtn; }
public static int getAvailablePort() { try (ServerSocket ss = new ServerSocket()) { ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { return getRandomPort(); } }
/** * binds the server socket and gets threads going */ private void startAcceptor() throws ConnectionException { int localPort; int p = this.port; createServerSocket(); try { localPort = socket.getLocalPort(); id = new InetSocketAddress(socket.getInetAddress(), localPort); stopped = false; thread = new LoggingThread("P2P Listener Thread " + id, this); try { thread.setPriority(Thread.MAX_PRIORITY); } catch (Exception e) { logger.info("unable to set listener priority: {}", e.getMessage()); } if (!Boolean.getBoolean("p2p.test.inhibitAcceptor")) { thread.start(); } else { logger.fatal( "p2p.test.inhibitAcceptor was found to be set, inhibiting incoming tcp/ip connections"); socket.close(); } } catch (IOException io) { String s = "While creating ServerSocket on port " + p; throw new ConnectionException(s, io); } this.port = localPort; }
/** * Finds a free port on the machine. * * @return * @throws IOException */ public static int findFreePort() throws IOException { ServerSocket socket= new ServerSocket(0); int port = socket.getLocalPort(); socket.close(); return port; }
ServerSocket s = new ServerSocket(0); System.out.println("listening on port: " + s.getLocalPort());
public static int findFreePort() throws IOException { ServerSocket socket= new ServerSocket(0); int port = socket.getLocalPort(); socket.close(); return port; } }
/** * Binds to a free ephemeral port, and then releases it. The returned port is quite likely * to be free for use after this, but is not entirely guaranteed to be. * * @return a (probably) free ephemeral port * @throws IOException if an error occurs while binding to a port */ public static int chooseFreePort() throws IOException { try (ServerSocket socket = new ServerSocket(0, 0)) { return socket.getLocalPort(); } }
/** * ԴãжǷض˿ * * @param port * @return * @throws IOException */ public static int try_port(int port) throws IOException { ServerSocket socket = new ServerSocket(port); int rtn = socket.getLocalPort(); socket.close(); return rtn; }
int getAvailable(InetAddress address) { try (ServerSocket socket = new ServerSocket(0, 50, address)) { return socket.getLocalPort(); } catch (IOException e) { throw new IllegalStateException("Fail to find an available port on " + address, e); } } }
private static int findFreePort() { ServerSocket server = null; int port = 0; try { server = new ServerSocket(0); port = server.getLocalPort(); } catch (IOException e) { port = DEFAULT_PORT; } finally { try { server.close(); } catch (Exception e) { // ignore } } return port; } }
@Override public int random() throws Exception { try (ServerSocket serverSocket = new ServerSocket(0)) { return serverSocket.getLocalPort(); } }
public static int findFreePort() { int port; try { ServerSocket server = new ServerSocket(0); port = server.getLocalPort(); server.close(); // allow time for the socket to be released TimeUnit.MILLISECONDS.sleep(250); } catch (Exception e) { throw new RuntimeException("Exception while trying to find a free port", e); } return port; } }
@Override public int specific(int port) throws Exception { try (ServerSocket serverSocket = new ServerSocket(port)) { return serverSocket.getLocalPort(); } } }
/** * Finds a free port on the machine, but allow the * ability to specify a port number to not use, no matter what. */ public static int findFreePortExcepting(int portToExclude) throws IOException { ServerSocket socket1 = null; ServerSocket socket2 = null; try { socket1 = new ServerSocket(0); socket2 = new ServerSocket(0); if (socket1.getLocalPort() != portToExclude) { return socket1.getLocalPort(); } // If we're here, then socket1.getLocalPort was the port to exclude // Since both sockets were open together at a point in time, we're // guaranteed that socket2.getLocalPort() is not the same. return socket2.getLocalPort(); } finally { if (socket1 != null){ socket1.close(); } if (socket2 != null){ socket2.close(); } } }
private static int findFreePort() { try(ServerSocket socket = new ServerSocket(0)) { socket.setReuseAddress(true); return socket.getLocalPort(); } catch (Exception ignore) { } throw new IllegalStateException("Could not find a free TCP/IP port to start dlv"); } }
/** * 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 */ 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; }
/** * Find a non-occupied port. * * @return A non-occupied port. */ public static int getAvailablePort() { for (int i = 0; i < 50; i++) { try (ServerSocket serverSocket = new ServerSocket(0)) { int port = serverSocket.getLocalPort(); if (port != 0) { return port; } } catch (IOException ignored) {} } throw new RuntimeException("Could not find a free permitted port on the machine."); }