Here you can find the source of findAvailablePort(int port)
Parameter | Description |
---|---|
IOException | an exception |
public static int findAvailablePort(int port) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.ServerSocket; public class Main { /** // w ww . j a v a 2s . co m * Creates a server socket, bound to the specified port. * A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range. * This port number can then be retrieved by calling getLocalPort. * * @return * @throws IOException */ public static int findAvailablePort(int port) throws IOException { try (ServerSocket s = new ServerSocket(port)) { return s.getLocalPort(); } catch (IOException e) { throw e; } } }