HOWTO: Determine if port is available
-
Hello all, I need to determine if a given port number is available for a socket. Please correct me if I'm wrong, but I think the reserved port numbers are output by "netstat /a". I'm guessing there's something in Winsock that lets me test for a given port's availability, but I'm not sure. Can anyone suggest how I can do this? Thanks.
-
Hello all, I need to determine if a given port number is available for a socket. Please correct me if I'm wrong, but I think the reserved port numbers are output by "netstat /a". I'm guessing there's something in Winsock that lets me test for a given port's availability, but I'm not sure. Can anyone suggest how I can do this? Thanks.
Its all a little fuzzy, but I think a call to
bind
should fail if the sockets local address + local port specified in the SOCKADDR is already in use. Also, winsock has the ability to bind to a port generated for you (specify a 0 for port), which you can query withgetsockname
.