How to bind boost::asio::ip::tcp::socket to a local endpoint with a sperific local IP address?
Hi, I have searched the documents, but still could not clarify how to bind the socket to a local IP address, which API could I add my local IP address to it? socket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 12345)); ....... boost::asio::ip::tcp::resolver::query query(remoteServerIPAddress, portNumber); Thank you. Kind regards, - jh
On Sun, Sep 22, 2019 at 5:00 PM JH via Boost
socket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 12345));
You are using the right function, but you are passing a default-constructed IPv4 address. Why not just pass the local IP address you want to bind to (instead of 0.0.0.0, which is what a default-constructed address gives you). Look at the type of the endpoint parameter in the call to bind: https://www.boost.org/doc/libs/1_71_0/doc/html/boost_asio/reference/basic_so... Regards
On 9/24/19, Vinnie Falco
On Sun, Sep 22, 2019 at 5:00 PM JH via Boost
wrote: socket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 12345));
Sorry, let me clarify it, it is in client side I want to bind a client local IP address either WiFi or other network interface.
You are using the right function, but you are passing a default-constructed IPv4 address. Why not just pass the local IP address you want to bind to (instead of 0.0.0.0, which is what a default-constructed address gives you). Look at the type of the endpoint parameter in the call to bind:
Did you refer to use boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(wifi_ip_address), portNumber)? I think you were talking about server side of default 0.0.0.0, it was my fault not being clear. Anyway, can it be done to bind client side interface IP address such as WiFi IP address? Is it possible to specify local IP address in boost::asio::ip::tcp::resolver::query query(remoteServerIpAddress, portNumber) ? Thank you Vinnie, Kind Regards, - jh
On 24/09/2019 17:43, JH wrote:
Did you refer to use boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(wifi_ip_address), portNumber)?
Yes. With socket.bind().
I think you were talking about server side of default 0.0.0.0, it was my fault not being clear. Anyway, can it be done to bind client side interface IP address such as WiFi IP address? Is it possible to specify local IP address in boost::asio::ip::tcp::resolver::query query(remoteServerIpAddress, portNumber) ?
No, you need to call bind in the client prior to using the socket for anything. Normally you don't call bind on the client side because you want the default of any address and any port, but if you want a specific address (or specific port) then you have to call it. (This is why you always see it called on the server side too, since you want a specific port there.) You can pass 0 for the port number if you want it to be automatically chosen as usual; it's rare (and generally not recommended, although needed in a few cases) for the client to request a specific port number.
participants (3)
-
Gavin Lambert
-
JH
-
Vinnie Falco