How do I obtain my local udp ip address on Android using Boost Asio?
I never tried to code in C++ for Android, but rather in ReactJS or directly in Java where a way of obtaining the IP would be e.g.
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
What I am saying is that you should most probably be obtaining the IP from the WifiManager as authoritative source under Android - that is what my I keep hearing from Android gurus. Given that you want to stick to boost::asio
, let's have a closer look to your library.
If you look into the source code of Boost-Asio, e.g. at boost/asio/ip/host_name.hpp
I see a single call of boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec)
which is located
within asio/detail/socket_ops.hpp
. I was expecting a loop over all possible sockets since there can be multiple interfaces. It is not obvious to me which order the sockets/ interfaces have under Android.
In other words: Maybe under Android your library is just (un)lucky to get the wrong socket which happens to be the loopback interface with 127.0.0.1
as IP.
You could now write your own asio::ip::gethostname()
function which tries to query the underlying asio::detail::socket_ops::gethostname()
more than once.
References
- How to get IP address of the device from code? and its duplicate How to get the ip address of an android mobile programatically ....?
- https://answers.unrealengine.com/questions/885603/view.html talks about a similar issue of the Unreal Engine. Different system, same effect.
- Official documentation for Android networking: https://developer.android.com/ndk/reference/group/networking
- https://android.stackexchange.com/questions/3715/is-there-a-command-or-application-similar-to-ipconfig
boost asio iostream - how to get local IP address
Assuming Linux, Use getifaddrs(3)
to get a list of interfaces for the local system.
Find server IP using Boost Asio
As far as getting IP is concerned the socket has a function that will retrieve the remote endpoint. I'd give this chain of commands, they should retrieve the string representation of the remote end IP address:
asio::ip::tcp::socket socket(io_service);
// Do all your accepting and other stuff here.
asio::ip::tcp::endpoint remote_ep = socket.remote_endpoint();
asio::ip::address remote_ad = remote_ep.address();
std::string s = remote_ad.to_string();
For the connectivity perspective never seen such functionality in asio.
How to get IP address of boost::asio::ip::tcp::socket?
The socket has a function that will retrieve the remote endpoint. I'd give this (long-ish) chain of commands a go, they should retrieve the string representation of the remote end IP address:
asio::ip::tcp::socket socket(io_service);
// Do all your accepting and other stuff here.
asio::ip::tcp::endpoint remote_ep = socket.remote_endpoint();
asio::ip::address remote_ad = remote_ep.address();
std::string s = remote_ad.to_string();
or the one-liner version:
asio::ip::tcp::socket socket(io_service);
// Do all your accepting and other stuff here.
std::string s = socket.remote_endpoint().address().to_string();
how to handle local and public ip using boost::asio::ip::tcp::resolver
Set address to "0.0.0.0" to accept all IPV4 addresses. Or you can simply replace:
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
with:
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
Please note that in the line above, port is an unsigned short
not a string
.
boost asio - connect using an ip address
It is possible to use:
socket.connect(endpoint);
Boost asio socket: how to get IP, port address of connection?
You can get the IP and port like this:
std::string sClientIp = socket().remote_endpoint().address().to_string();
unsigned short uiClientPort = socket().remote_endpoint().port();
Related Topics
How to Define a Move Constructor
How to Get a List of Files in a Folder in Which the Files Are Sorted with Modified Date Time
When Do You Prefer Using Std::List<T> Instead of Std::Vector<T>
Is a Member of an Rvalue Structure an Rvalue or Lvalue
Visual Studio Project Out of Date
Passing Member Function Pointer to Member Object in C++
Is Using a Vector of Boolean Values Slower Than a Dynamic Bitset
Vector <Unsigned Char> VS String for Binary Data
Implementing Component System from Unity in C++
Print MACro Values Without Knowing the Amount of MACros
How to Access a Global Variable Within a Local Scope
"Winapifamily.H: No Such File or Directory" When Compiling Sdl in Code::Blocks