Here you can find the source of isPortAvailable(int port)
public static boolean isPortAvailable(int port)
//package com.java2s; import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; public class Main { public static boolean isPortAvailable(int port) { ServerSocket ss = null;/*ww w. ja va2 s . c o m*/ DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; } }