I am using 0 in the sin_port parameter of a listening socket.
As said in the Windows API, if you use 0 for the port, Windows will use a port between 1024 and 5000.
How can I know which port Windows selected after a bind or listen call?
I tried getsockname, but it doesn't seem to work; I always get 0 in the sin_port parameter.
Posted on 2003-04-19 02:37:23 by dilau
For TCP/IP, if the port is specified as zero, the service provider assigns a unique port to the application with a value between 1024 and 5000. The application can use getsockname after calling bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname cannot necessarily supply the address until the socket is connected, since several addresses can be valid if the host is multihomed. Binding to a specific port number other than port 0 is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.

Posted on 2003-04-19 04:08:45 by Thomas