While writing a network application, I decided to set myself up so that if the port I wanted was already in use, the app would try the port + 1, infinitely until an available port was found.
So I wrote the following code into the constructor of my connectionManager object:
boolean err = false;
do
{
try
{
System.out.println("Attempting to connect socket to port " + (ourSock + x) + "...");
socket = new DatagramSocket(ourSock);
System.out.println("Connected to socket: port " + (ourSock + x));
err = false;
}
catch (SocketException e)
{
System.err.println("Could not connect to port " + (ourSock + x));
System.err.println(e);
x++;
err = true;
}
}
while (err == true);
The code itself seems to work all right. When the port is available, the program connects and is happy; when it's not, it goes up by one numer and attempts to connect again.
The problem is, for some reason
Java (or my system, or whatever) thinks that ports which I know to be open aren't. In fact, when my program encounters the first used port, it goes into an infinite loop, wherein it finds that each subsequent port is already in use. Since I've started with port 7334, I know that, say, 7335-7500 are open.
Why would this happen? I'm running Java 2 on an OpenBSD 2.9, Intel-based machine.
Thanks,
Alex Kirk