Question

I'm using the below code in my application to bind an IP endpoint to a socket, I have a quick question is "7777" setting the port of the local endpoint? If so then how can I automatically generate a port instead of having to set it myself.

sockconn.lowest_layer().bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.1"), 7777));
Was it helpful?

Solution

Bind to 0. A port will be chosen for you.

sockconn.lowest_layer().bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.1"), 0));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top