Here you can find the source of checkIfPortAvailable(int port)
private static boolean checkIfPortAvailable(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 { private static boolean checkIfPortAvailable(int port) { ServerSocket tcpSocket = null; DatagramSocket udpSocket = null; try {//from w w w. ja v a 2s. c o m tcpSocket = new ServerSocket(port); tcpSocket.setReuseAddress(true); udpSocket = new DatagramSocket(port); udpSocket.setReuseAddress(true); return true; } catch (IOException ex) { // denotes the port is in use } finally { if (tcpSocket != null) { try { tcpSocket.close(); } catch (IOException e) { /* not to be thrown */ } } if (udpSocket != null) { udpSocket.close(); } } return false; } }