Here you can find the source of getAvailablePort(int port)
public static Integer getAvailablePort(int port)
//package com.java2s; import java.net.ServerSocket; public class Main { public static Integer getAvailablePort(int port) { ServerSocket socket = null; for (int i = 0; i < 2048; i++) { try { socket = new ServerSocket(port + i); } catch (Exception e) { } finally { if (null != socket) { try { socket.close();/*from w w w .j a v a2s .co m*/ return port + i; } catch (Exception e2) { } } } } return null; } }