Re: Finding an available port for a socket connection?
- From: "Damien" <Damien_The_Unbeliever@xxxxxxxxxxx>
- Date: 8 Sep 2006 05:55:19 -0700
andreas.baus@xxxxxxxxxxxxx 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
.
- References:
- Finding an available port for a socket connection?
- From: andreas . baus
- Re: Finding an available port for a socket connection?
- From: Mike Lowery
- Re: Finding an available port for a socket connection?
- From: andreas . baus
- Finding an available port for a socket connection?
- Prev by Date: Re: Finding an available port for a socket connection?
- Next by Date: Implement a Time-out in the waitQueue of the ThreadPool
- Previous by thread: Re: Finding an available port for a socket connection?
- Next by thread: Re: Finding an available port for a socket connection?
- Index(es):
Relevant Pages
|