Java HTTP Port Find getFreePort()

Here you can find the source of getFreePort()

Description

get Free Port

License

Apache License

Declaration

public static int getFreePort() 

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 getFreePort() {
        try {/*from www. j a v  a  2  s . co m*/
            try (ServerSocket socket = new ServerSocket(0)) {
                socket.setReuseAddress(true);
                return socket.getLocalPort();
            }
        } catch (IOException e) {
            return -1;
        }
    }
}

Related

  1. getFreePort()
  2. getFreePort()
  3. getFreePort()
  4. getFreePort()
  5. getFreePort()
  6. getFreePort()
  7. getFreePort()
  8. getFreePort()
  9. getFreePort()