Here you can find the source of availablePort(int port)
From: https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
public static boolean availablePort(int port)
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; public class Main { /**//ww w .j a va 2s . c om * From: https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java */ public static boolean availablePort(int port) { ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (Exception e) { System.out.println("IPUtils::availablePort: " + e.getMessage()); } finally { if (ds != null) ds.close(); if (ss != null) try { ss.close(); } catch (IOException e) { /* Should not be thrown */ System.out.println("IPUtils::availablePort: " + e.getMessage()); } } return false; } }