Finding an available port for a socket connection?

A

andreas.baus

Hi. I am trying to set up a TcpListener to listen for incoming TCP
connections, and one thing I have been wondering is whether there's a
good way for finding an available port number, say, from a given range
of ports.

Of course I could just loop over all ports, try to open the listener on
it, and catch the SocketException that gets thrown when the port is
already in use, before attempting the next port number and so on until
the Listener creation succeeds (or the entire range is exhausted). But
that hardly strikes me as efficient or elegant.

If anyone has any ideas for a better way, I'd love to hear them. Thanks
in advance.
 
M

Mike Lowery

Hi. I am trying to set up a TcpListener to listen for incoming TCP
connections, and one thing I have been wondering is whether there's a
good way for finding an available port number, say, from a given range
of ports.

Of course I could just loop over all ports, try to open the listener on
it, and catch the SocketException that gets thrown when the port is
already in use, before attempting the next port number and so on until
the Listener creation succeeds (or the entire range is exhausted). But
that hardly strikes me as efficient or elegant.

If anyone has any ideas for a better way, I'd love to hear them. Thanks
in advance.

Common practice is to use ports between 49152 and 65535. It's unlikely you'll
have a conflict in that range.
http://www.iana.org/assignments/port-numbers
 
A

andreas.baus

Mike said:
Common practice is to use ports between 49152 and 65535. It's unlikely you'll
have a conflict in that range.
http://www.iana.org/assignments/port-numbers

It is acutally fairly likely there will be collisions if there are
several instances of my code running on the same machine under the same
- or at least very similar - configurations (this application is
supposed to run in a terminal server environment) which somehow need to
get along with each other.
 
D

Damien

It is acutally fairly likely there will be collisions if there are
several instances of my code running on the same machine under the same
- or at least very similar - configurations (this application is
supposed to run in a terminal server environment) which somehow need to
get along with each other.

I found this snippet of C#, that sounds like it does what you need.
Didn't write it myself, and I'm more of a VB guy myself, but it doesn't
look like it loops:

static int FindFreePort()
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
return ((IPEndPoint)socket.LocalEndPoint).Port;
}
finally
{
socket.Close();
}
}

Don't see any way you could limit it to a particular range though
(plus, as is, it closes the socket, whereas you might want to keep the
socket open, to prevent race conditions with other instances of your
app)

Damien
 
C

Chris Mullins

Hi. I am trying to set up a TcpListener to listen for incoming TCP
connections, and one thing I have been wondering is whether there's a
good way for finding an available port number, say, from a given range
of ports.

I'm pretty sure WMI offers the features your need - it'll have a list of all
in-use sockets that you can iterate over and then make a smart decision
about the port you want to use.

I just spent 15 minutes looking for the WMI class so I could paste an
example in here, but I can't seem to find the right one. I'm sure it's in
there somewhere, but I'm afraid I'm not sure where.

WMI comes with baggage though - it has all sorts of permissions restrictions
that I've never quite gotten a handle on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top