Java HTTP Port Find getRandomAvailablePort()

Here you can find the source of getRandomAvailablePort()

Description

get Random Available Port

License

Open Source License

Declaration

public static int getRandomAvailablePort() 

Method Source Code

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

import java.net.DatagramSocket;
import java.net.ServerSocket;

public class Main {
    public static int getRandomAvailablePort() {
        int port = 2048;
        boolean br = false;
        while (port < 49151 && !br) {
            try {
                new ServerSocket(port).close();
                new DatagramSocket(port).close();
                br = true;//from  w w w . ja va  2  s  . c  o m
            } catch (Exception ex) {
                port++;
            }
        }
        return port;
    }
}

Related

  1. getPort(String hostAndPort)
  2. getPortInRange(int minPort, int maxPort)
  3. getPorts(int count)
  4. getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)
  5. getPrometheusMetrics(int metricsPort)
  6. getRandomFreePort()
  7. getRandomFreePort()
  8. getRandomOpenPort()
  9. getRandomPort()