Here you can find the source of isPortFreeClient(String hostName, int portNumber)
private static boolean isPortFreeClient(String hostName, int portNumber)
//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; } } }