Java HTTP Port Find availablePort(int port)

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

Description

From: https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java

License

Open Source License

Declaration

public static boolean availablePort(int port) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.net.ServerSocket;

public class Main {
    /**//ww w  .j  a  va  2s  .  c  om
     * From: https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
     */
    public static boolean availablePort(int port) {
        ServerSocket ss = null;
        DatagramSocket ds = null;

        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);

            return true;
        } catch (Exception e) {
            System.out.println("IPUtils::availablePort: " + e.getMessage());
        } finally {
            if (ds != null)
                ds.close();
            if (ss != null)
                try {
                    ss.close();
                } catch (IOException e) {
                    /* Should not be thrown */
                    System.out.println("IPUtils::availablePort: " + e.getMessage());
                }
        }

        return false;
    }
}

Related

  1. available(int port)
  2. available(int port)
  3. available_port()
  4. availableAndReturn(int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)
  5. availablePort()
  6. availablePort(int pPort)
  7. availablePort(int prefered)
  8. canConnect(String host, int port)
  9. canConnect(String host, int port)