Java HTTP Port Find isPortFreeClient(String hostName, int portNumber)

Here you can find the source of isPortFreeClient(String hostName, int portNumber)

Description

is Port Free Client

License

Apache License

Declaration

private static boolean isPortFreeClient(String hostName, int portNumber) 

Method Source Code


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

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;

import java.net.Socket;

public class Main {
    private static boolean isPortFreeClient(String hostName, int portNumber) {
        try {/*from  w  w w  . j av a 2 s.  c o  m*/
            // If the host name is null, assume localhost
            if (hostName == null) {
                hostName = getHostName();
            }
            Socket socket = new Socket(hostName, portNumber);
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            os.close();
            os = null;
            is.close();
            is = null;
            socket.close();
            socket = null;
        } catch (Exception e) {
            // Nobody is listening on this port
            return true;
        }

        return false;
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. isPortBound(int port)
  2. isPortFree(int port)
  3. isPortFree(int port)
  4. isPortFree(int portNumber)
  5. isPortFree(String hostName, String port)
  6. isPortInUse(final String host, final Integer port)
  7. isPortInUse(int port)
  8. isPortOnUse(int port)
  9. isPortOpen(int port)