Here you can find the source of available(int port)
static boolean available(int port)
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.Socket; public class Main { static boolean available(int port) { Socket s = null;//from w ww. j a v a2s . c o m try { s = new Socket("localhost", port); // If the code makes it this far without an exception it means // something is using the port and has responded. return false; } catch (IOException e) { return true; } finally { if (s != null) { try { s.close(); } catch (IOException e) { throw new RuntimeException("You should handle this error.", e); } } } } }