Java HTTP Port Find isBound(int port)

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

Description

Returns true if port is already bound by a process; otherwise returns false.

License

Open Source License

Declaration

public static boolean isBound(int port) throws Exception 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Main {
    /**//from w  w w.j  av a2  s  .  c o m
     * Returns true if port is already bound by
     * a process; otherwise returns false.
     */
    public static boolean isBound(int port) throws Exception {
        InetSocketAddress address = new InetSocketAddress(port);
        return isBound(address);
    }

    /**
     * Returns true if ip+port are already bound by
     * a process; otherwise returns false.
     */
    public static boolean isBound(String ip, int port) throws Exception {
        InetSocketAddress address = new InetSocketAddress(ip, port);
        return isBound(address);
    }

    /**
     */
    private static boolean isBound(InetSocketAddress address) throws Exception {
        Socket socket = new Socket();
        try {
            socket.bind(address);
            return false;
        } catch (IOException e) {
            return true;
        } finally {
            socket.close();
        }
    }
}

Related

  1. isActivePort(int port)
  2. isAlive(String hostname, int port, int retries)
  3. isAvailablePort(int port)
  4. isBindable(int port)
  5. isBindedPort(String host, int port)
  6. isBound(int port)
  7. isConnectionAlive(String protocol, String host, int port)
  8. isFreePort(int portNumber)
  9. isFreeTCPPort(final int port)