Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check port availability

0 views
Skip to first unread message

Ronny

unread,
Nov 24, 2009, 5:12:12 AM11/24/09
to
I need to know programmatically if some port is available for using in a new
socket.
Is that possible and how?
Regards
Ronny


Bert Hyman

unread,
Nov 24, 2009, 9:57:12 AM11/24/09
to
In news:OQ#Xj6ObK...@TK2MSFTNGP05.phx.gbl "Ronny" <ro...@mako.com>
wrote:

> I need to know programmatically if some port is available for using in
> a new socket.
> Is that possible and how?

If you bind to a port that's already in use, you should get a
WSAEADDRINUSE error.

Is that too crude?

--
Bert Hyman St. Paul, MN be...@iphouse.com

Ronny

unread,
Nov 25, 2009, 1:40:13 AM11/25/09
to
Thanks Mr. Hyman
Does that work olso in DotNet?
Regards
Ronny

"Bert Hyman" <be...@iphouse.com> wrote in message
news:Xns9CCD5B1431B...@207.46.248.16...

Tom Handal

unread,
Nov 25, 2009, 2:31:34 AM11/25/09
to

I would use the GetTcpTable or GetUdpTable functions from the IP
Helper API library in Windows. Using those, you can see all of the
active TCP connections, and TCP and UDP listening ports. You can check
these tables to see which ports are currently being used and which are
not. That way you can choose what port you want to use if you are in
a dynamic port allocation situation.

If there is a very specific port you want to use, then you can do what
Bert said. Just catch the WSAEADDRINUSE error as he stated.

Tom Handal

Remy Lebeau

unread,
Nov 25, 2009, 12:50:07 PM11/25/09
to

"Ronny" <ro...@mako.com> wrote in message news:OSD2woZb...@TK2MSFTNGP05.phx.gbl...

> Does that work olso in DotNet?

The Socket.Bind() method will raise a SocketException if the chosen ip/port pair cannot be used.

--
Remy Lebeau (TeamB)

0 new messages