I'm working on a simple communication client in-between Windows/Android using Boost-Asio and UDP. When resolving the hostname and ip address on Windows I get my computer name and a valid ip-address. However, the equivalent code on android returns localhost
and 127.0.0.1
.
How can I obtain an ip-address usable from other clients?
Snippet for obtaining local ip address
auto io_service = asio::io_service();
auto resolver = asio::ip::udp::resolver(io_service);
const auto host_name = asio::ip::host_name(); // "localhost" on android
const auto query = asio::ip::udp::resolver::query(host_name, "");
const auto begin = resolver.resolve(query);
const auto end = asio::ip::udp::resolver::iterator{};
for (
auto it = resolver.resolve(query);
it != end;
++it
) { auto ip = it->endpoint().address(); } // only 127.0.0.1 on android
Android Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Notes
I do find the correct ip-address via the Java WifiManager. However, I would like to obtain it directly from the C++/boost::asio
side of my application where the rest of the network code is.