Java HTTP Port Find available(int port)

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

Description

available

License

Apache License

Declaration

public static boolean available(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 final int MIN_PORT_NUMBER = 1100;
    public static final int MAX_PORT_NUMBER = 49151;

    public static boolean available(int port) {
        if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
            throw new IllegalArgumentException("Invalid start port: "
                    + port);//  ww w .j av a  2  s .co  m
        }

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    /* should not be thrown */
                }
            }
        }

        return false;
    }
}

Related

  1. available(final int port)
  2. available(int port)
  3. available(int port)
  4. available(int port)
  5. available_port()