Java HTTP Port Find isLocalPortUsed(int port)

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

Description

Check if aport is being used in the local host

License

Apache License

Parameter

Parameter Description
port a parameter

Declaration

public static boolean isLocalPortUsed(int port) 

Method Source Code


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

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Main {
    /**//  w w w. java 2 s.  c o  m
     * Check if  aport is being used in the local host
     *
     * @param port
     * @return
     */
    public static boolean isLocalPortUsed(int port) {
        boolean flag = true;
        try {
            flag = isPortUsing("127.0.0.1", port);
        } catch (Exception e) {
        }
        return flag;
    }

    /**
     * Check if a port is being used in the specified host
     *
     * @param host
     * @param port
     * @return
     */
    public static boolean isPortUsing(String host, int port) {
        boolean flag = false;
        try {

            InetAddress theAddress = InetAddress.getByName(host);
            Socket socket = new Socket(theAddress, port);
            flag = true;
        } catch (IOException e) {
            //do nothing
        }
        return flag;
    }
}

Related

  1. isListening(String host, int port)
  2. isLivereloadAvailable(int port)
  3. isLocalPortAvailable(final int port)
  4. isLocalPortFree(final int port)
  5. isLocalPortOccupied(int portNum)
  6. isLoclePortUsing(int port)
  7. isMulticastSupported(NetworkInterface pNif)
  8. isOpen(final int port)
  9. isPortActive(String host, int port)