So, I'm testing my android game on my phone (HTC Legend) in debugging mode. I am trying to communicate with my custom JAVA server. I can receive UDP packages on my server, i can see them, but none of the responses from server ever reaches phone. I am using following code on my server to get client IP:port
datagramPackageIn.getAddress();
datagramPackageIn.getPort();
On the client side I use this code to see my address:
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;
}
In testing I can see that those 2 addresses are different. Maybe there is the problem?
I used standard UDP communication code (one that worked in emulator) in client:
run(){//simplified version
while(true)
packageIN()
}
public packageIN(){
DatagramPacket packet= new DatagramPacket(inBuffer, inBuffer.length);
DatagramSocket socket= new DatagramSocket(receivingPort);
socket.receive();
}
So, any working solutions ( tested one with phone on 3g)?
P.S. I know that HTC Legend has wifi problems, thats why I use 3G.....
If the server shows a different IP address than the mobile device when you connect through 3G, you may be behind a NAT gateway. Some cell carriers do this. A sure sign of NAT is if the IP looks something like
10.x.x.xor192.168.x.xor172.y.x.x(whereyis between 16 and 31). I've also seen carriers assign NAT addresses of the form1.x.x.x.With such a setup it depends on how the gateway handles UDP.
To rule out any errors caused by NAT, try accessing the server through WiFi and a LAN, not going through any networks that you don't control.