Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Ports in Use

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I dont see where your incrementing ourSock
 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic