Here you can find the source of isPortAvailable(String host, int port)
public static boolean isPortAvailable(String host, int port)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.Socket; public class Main { /**//from ww w.jav a 2 s . c om * Is the specified port available? * * Note that this will be true at the time the method run, * but may be false a couple of milliseconds after... */ public static boolean isPortAvailable(String host, int port) { try { (new Socket(host, port)).close(); return true; } catch (IOException e) { return false; } } }