Java HTTP Port Find isPortAvailable(int port)

Here you can find the source of isPortAvailable(int port)

Description

is Port Available

License

Apache License

Declaration

public static boolean isPortAvailable(int port) 

Method Source Code


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

import java.io.IOException;
import java.net.DatagramSocket;

import java.net.ServerSocket;

public class Main {
    public static boolean isPortAvailable(int port) {
        if (port < 1 || port > 65535) {
            throw new IllegalArgumentException("Invalid start port: " + port);
        }/* w  w w.j av a2 s  .com*/

        try (ServerSocket ss = new ServerSocket(port); DatagramSocket ds = new DatagramSocket(port)) {
            ss.setReuseAddress(true);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException ignored) {
            /* should not be thrown */
            return false;
        }
    }
}

Related

  1. isPortAvailable(int port)
  2. isPortAvailable(int port)
  3. isPortAvailable(int port)
  4. isPortAvailable(int port)
  5. isPortAvailable(int port)
  6. isPortAvailable(int port)
  7. isPortAvailable(String host, int port)
  8. isPortAvailable(String host, int port)
  9. isPortAvailable(String host, int port)