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));
有帮助吗?

解决方案

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));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top