Java HTTP Port Find findFreePort()

Here you can find the source of findFreePort()

Description

find Free Port

License

Apache License

Declaration

public static int findFreePort() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.net.ServerSocket;

public class Main {
    public static int findFreePort() {
        try (ServerSocket serverSocket = new ServerSocket(0)) {
            return serverSocket.getLocalPort();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }/*from  w  w  w.j  a  v  a 2  s.com*/
    }
}

Related

  1. findAvailablePort(int minPort, int maxPort)
  2. findAvailablePort(int port)
  3. findAvailablePort(int port)
  4. findAvailablePort(String hostname, int startPort, int endPort)
  5. findAvailablePorts(int n)
  6. findFreePort()
  7. findFreePort()
  8. findFreePort()
  9. findFreePort()