Java HTTP Port Find getFreePort()

Here you can find the source of getFreePort()

Description

Get available port.

License

Open Source License

Return

available port. -1 if no available ports exist

Declaration

public static int getFreePort() 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**/*from www . j a va2s . co  m*/
     * Get available port.
     *
     * @return available port. -1 if no available ports exist
     */
    public static int getFreePort() {
        try (ServerSocket socket = new ServerSocket(0)) {
            return socket.getLocalPort();
        } catch (IOException ioe) {
            return -1;
        }
    }
}

Related

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