Here you can find the source of available(int port)
public static boolean available(int port)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; public class Main { public static final int MIN_PORT_NUMBER = 1100; public static final int MAX_PORT_NUMBER = 49151; public static boolean available(int port) { if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { throw new IllegalArgumentException("Invalid start port: " + port);// ww w .j av a 2 s .co m } ServerSocket ss = null; 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; } }