C automatically assigns a port
I just wanted to know how to use C to automatically assign a free port (and see what was used) if a specific port number is not provided.
For example, I use this:
struct sockaddr_in address; address->sin_family = AF_INET; address->sin_addr.s_addr = INADDR_ANY; address->sin_port = htons( port );
But how can I replace the sin_port assignment and let C automatically assign for me?
Thanks!
+2
a source to share
2 answers
Matt has already answered the question "how to assign a free port" (use sin_port = 0).
To answer the second part, which is "how to see what was used", use getsockname ()
+4
a source to share