Here you can find the source of getPort(final int suggestedPort)
protected static int getPort(final int suggestedPort) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.BindException; import java.net.ServerSocket; public class Main { /**/* w w w . java2s . c o m*/ * Return an open port on current machine. Try the suggested port first. If * suggestedPort is zero, just select a random port */ protected static int getPort(final int suggestedPort) throws IOException { ServerSocket openSocket; try { openSocket = new ServerSocket(suggestedPort); } catch (BindException ex) { // the port is busy, so look for a random open port openSocket = new ServerSocket(0); } final int port = openSocket.getLocalPort(); openSocket.close(); return port; } }