• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Ports in Use

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic