I'm writing a client/server app that opens two sockets between the client and server. The server app is pretty typical, it opens a ServerSocket on a specific port. When the client connects it spawns a client handler thread to communicate with the new client. The first thing the client handler thread does is create a ServerSocket to allow the client to create an additional socket connection to the server.
Here is the problem. I'd like to restrict the range of ports so that I can program my router to forward that specific range of ports to the server.
Now, if I call new ServerSocket(0) I'll get an available port but not necessarily in the range of ports that I want.
Is there some way to find an available port or better, specify the range of ports to the ServerSocket constructor?