Hi guys
I am creating p2p chat app. i want to get the peer ip addresses if it's available. I manage to get local ip address on Fig 1, However i dont know how to get other devices ip address. Could anyone help me what's wrong with Fig 2. Or any idea how to do it?
Thanks
Fig 1
Fig 2
I am creating p2p chat app. i want to get the peer ip addresses if it's available. I manage to get local ip address on Fig 1, However i dont know how to get other devices ip address. Could anyone help me what's wrong with Fig 2. Or any idea how to do it?
Thanks
Fig 1
Code:
public String getLocalIpAddress() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(android.content.Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
try {
return InetAddress.getByName(String.format("%d.%d.%d.%d",
(ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff))).toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
Fig 2
Code:
public String getClientIpAddress() {
WifiManager wifiManager = (WifiManager) connectionSocket.getRemoteSocketAddress(android.content.Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
try {
return InetAddress.getByName(String.format("%d.%d.%d.%d",
(ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff))).toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
Last edited by a moderator:
