How to get the Android device IP to be used in Socket() in java?

Asked

Viewed 786 times

0

I’m trying to implement a point-to-point communication (device to device) and I need to get the real IP of the server device to then insert it into the client but I can’t. I’m using:

ServerSocket server = new ServerSocket(8000);
Log.v("TAG", server.getInetAddress().getHostAddress().toString());

and just returns to me "::".

1 answer

1

You will need to make a request to an external server to know the IP. Something like this:

https://github.com/kost/external-ip

================

In the case of IP Wi-Fi:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Linking in the manifest:

ACCESS_WIFI_STATE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.