I'm developing one socket app.
Background :
- There is two android device.
- Both device is on different network (i.e. one is on mobilenetwork and other is on different mobile network or on wifi).
- App is working good when on same wifi network.
- App is not working on different network.
- I've read about port forwarding but this all ended up with router port forwarding, so this has nothing to do with my app, as my app is like to work on different network.
What I've tried:
- Use Socket-communication over different networks [ Still unable to find the answer]
I got server ip address using following method and it gives me ip address:
public String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "Server running at : "
+ inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
text.setText(ip);
return ip;
}
I'm using static port 8080 [May be this is a problem,please suggest me a option if so].
I'm using this lines at client side to connect but soket object returns null on different network:
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
So,Please suggest me what should i use for communicating over different real android device with different network [app runs on same network perfectly]?