Java HTTP Port Find findFreePort()

Here you can find the source of findFreePort()

Description

find Free Port

License

Open Source License

Declaration

public static int findFreePort() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.ServerSocket;

public class Main {
    public static int findFreePort() {
        try {/*w w  w.ja  v  a 2 s . co  m*/
            ServerSocket server = new ServerSocket(0);
            int port = server.getLocalPort();
            server.close();
            return port;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

Related

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