Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining public IP address rather than localhost (Boost Asio)

I have the following connection set up, this works correctly. This is part of a larger piece of code which listens (at a free port), for incoming messages. What I am trying to do is publish the uri so that other clients can connect to this. However I cannot figure out a way for the endpoint.address() to appear as the actual IP address on the interface being used rather than "localhost". Any ideas?

 tcp::resolver::query query(address, "");
 tcp:: endpoint endpoint = *resolver.resolve(query);
 acc.open(endp.protocol());
 acc.set_option(reuse_address(true));
 acc.bind(endp);
 acc.listen();

 tcp::endpoint endpoint =  acc.local_endpoint() ; 
 string uri = "tcp://" + endpoint.address().to_string() + ":" + lexical_cast<string>(endpoint.port()) ; 
like image 606
user1018513 Avatar asked Nov 19 '25 23:11

user1018513


1 Answers

Boost ASIO has no way to enum all the interfaces of your computer. resolver query your DNS for your IP, witch is not the same as it can return whatever you have configured in it (even inacurrate information can be retrieved).

If you want to bind to the default interface. you don't need to make a resolve. Just create the socket with the following endpoint :

 boost::asio::ip::tcp::endpoint endpoint = 
      boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),port);
like image 145
geekpp Avatar answered Nov 22 '25 12:11

geekpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!