• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont see where your incrementing ourSock
 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should have been more clear. I defined "x" earlier to be 0; as you'll note, I use it to do the incrementing.
Alex
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i figured that part out but what is ourSock equal to ?
oursock has to equal X
 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awww, shit....
It's a static final int...but it'd work just fine if I was having my connection attempt use (ourSock + x).
I really should have seen that.
Thanks,
Alex
 
reply
    Bookmark Topic Watch Topic
  • New Topic