Here you can find the source of isPortAvailable(int port)
public static boolean isPortAvailable(int port)
//package com.java2s; import java.net.Socket; public class Main { public static boolean isPortAvailable(int port) { try {/*from w w w . j a v a2 s . c om*/ Socket s = new Socket("127.0.0.1", port); s.close(); return false; } catch (Exception ex) { return true; } } }