Home All Groups Group Topic Archive Search About

Finding an available port for a socket connection?

Author
7 Sep 2006 1:18 PM
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.

Author
7 Sep 2006 4:11 PM
Mike Lowery
<andreas.b***@meta-level.de> wrote in message
Show quote
news:1157635090.938081.117470@i42g2000cwa.googlegroups.com...
> 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
Author
8 Sep 2006 12:42 PM
andreas.baus
Mike Lowery schrieb:

> 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.
Author
8 Sep 2006 12:55 PM
Damien
andreas.b***@meta-level.de wrote:
> Mike Lowery schrieb:
>
> > 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.

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
Author
8 Sep 2006 4:29 PM
Chris Mullins
<andreas.b***@meta-level.de> wrote
> 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.

--
Chris Mullins

AddThis Social Bookmark Button