Here you can find the source of findFreePort()
public static ServerSocket findFreePort()
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; public class Main { public static ServerSocket findFreePort() { ServerSocket server = null; try {/*from w w w. j a v a2 s .c om*/ server = new ServerSocket(0, 1000, InetAddress.getLocalHost()); server.setReuseAddress(true); } catch (IOException e) { e.printStackTrace(); } return server; } }