[boost] How to bind boost::asio::ip::tcp::socket to a local endpoint with a sperific local IP address?

1,227 views
Skip to first unread message

JH via Boost

unread,
Sep 22, 2019, 8:00:22 PM9/22/19
to boost, JH
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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Vinnie Falco via Boost

unread,
Sep 23, 2019, 10:13:54 AM9/23/19
to boost@lists.boost.org List, Vinnie Falco
On Sun, Sep 22, 2019 at 5:00 PM JH via Boost <bo...@lists.boost.org> wrote:
> 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_socket_acceptor/bind/overload2.html>

Regards

JH via Boost

unread,
Sep 24, 2019, 1:44:07 AM9/24/19
to Vinnie Falco, JH, boost@lists.boost.org List
On 9/24/19, Vinnie Falco <vinnie...@gmail.com> wrote:
> On Sun, Sep 22, 2019 at 5:00 PM JH via Boost <bo...@lists.boost.org> 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

Gavin Lambert via Boost

unread,
Sep 24, 2019, 3:53:21 AM9/24/19
to bo...@lists.boost.org, Gavin Lambert
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.
Reply all
Reply to author
Forward
0 new messages