Java HTTP Port Find findFreePortForApi()

Here you can find the source of findFreePortForApi()

Description

find Free Port For Api

License

Open Source License

Declaration

public static int findFreePortForApi() 

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 findFreePortForApi() {
        ServerSocket socket = null;
        try {// ww  w .j a  v a2 s  .c o  m
            socket = new ServerSocket(0);
            return socket.getLocalPort();
        } catch (IOException e) {

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
        }
        return -1;
    }
}

Related

  1. findFreePort(int start, int len)
  2. findFreePort(int start, int limit)
  3. findFreePort(int startPort)
  4. findFreePort(int startPort)
  5. findFreePortExcepting(int portToExclude)
  6. findUnusedPort()
  7. findUnusedPort()
  8. findUnusedPort(int preferredPort)
  9. findUnusedPorts(int numPorts)