Here you can find the source of isPortOnUse(int port)
public static boolean isPortOnUse(int port)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.*; public class Main { public static boolean isPortOnUse(int port) { boolean isUse = false; ServerSocket serverSocket = null; try {//from w ww .j a v a 2 s . c om serverSocket = new ServerSocket(port); isUse = false; } catch (IOException e) { e.printStackTrace(); isUse = true; } finally { if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } return isUse; } }