Java HTTP Port Find findUnusedPort()

Here you can find the source of findUnusedPort()

Description

find Unused Port

License

Apache License

Declaration

static int findUnusedPort() 

Method Source Code


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

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

public class Main {
    static int findUnusedPort() {
        try (final ServerSocket serverSocket = new ServerSocket(0)) {
            return serverSocket.getLocalPort();
        } catch (IOException e) {
            throw new RuntimeException("Cannot find available port", e);
        }/*from w  w w.  ja  v  a2 s.  c  o  m*/
    }
}

Related

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