Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find a port that is free.

Status
Not open for further replies.

bhunter

Programmer
Jul 27, 2000
36
US
I am testing an application that uses java sockets, and a lot of times the application doesn't free up a port because it crashes (it's still being developed) before it hits the code to do that. If I want to run the program again, the port that was previously used is not available. I then have to go into the code, change the port number and recompile every time. Is there a way to look for a free port at run-time so that each time I start the program I can find an available port? That way I won't need to constantly change a port number to make things work.
 
bhunter,

When you create a Socket object you don't tell it what port to use, only what port to connect to on the target system. The TCP/IP stack picks an available port for you.

I'm confused as to what you are trying to do and what the problem is.

-pete
 
i assume he means with the ServerSocket object.

with that you designate a port for it to bind to.

 
i meant to add that i have no idea how to look for available ports.
 
> i assume he means with the ServerSocket object.

Well how does that work? If it changes the listening port dynamically, how could any clients connect to it? X-) This is very confusing.

-pete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top