Another option is to specify port 0 to bind()
. That will allow you to bind to a specific IP address (in case you have multiple installed) while still binding to a random port. If you need to know which port was picked, you can use getsockname()
after the binding has been performed.