Failed to get UDP data on Android from PC server via Internet

I am currently studying the transfer of UDP packets in Java to create a multiplayer game on Android. I managed to exchange packets in my Nexus 4 using the usual "127.0.0.1", and I also managed to exchange packets between my PC server and my Android client on my local network. But since I want the game to play on the Internet, I want my Android client to be able to exchange packages with my PC server when they are not on the same local network. I'm scared here.

My setup: A PC server connected to my home Internet connection and a Nexus 4 connected to a 3G network.

Firstly, my PC server starts listening on port 10000, and my Android client opens a socket to receive server packets on port 10001. Then, the Android client sends a packet to the PC server to its current public address "173.246.12.125" on port 10000. Server The PC receives the packet and sends a response to the sender on port 10001. But the Android client never receives a response.

Here is my pc server code:

public class UDPServer { private final static int SERVER_PORT = 10000; private final static int CLIENT_PORT = 10001; public static void main(String[] args) { InetAddress clientAddr = null; DatagramSocket socket = null; try { //Initializing the UDP server System.out.println(String.format("Connecting on %s...", SERVER_PORT)); socket = new DatagramSocket(SERVER_PORT); System.out.println("Connected."); System.out.println("===================="); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } while(true){ try { //Listening byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, buf.length); System.out.println("Listening..."); socket.receive(packet); //Getting client address from the packet we received clientAddr = packet.getAddress(); System.out.println("Received: '" + new String(packet.getData()).trim() + "' from "+clientAddr.toString()); //Sending response byte[] message = ("Hello Android").getBytes(); DatagramPacket response = new DatagramPacket(message, message.length, clientAddr, CLIENT_PORT); DatagramSocket clientSocket = new DatagramSocket(); System.out.println("Sending: '" + new String(message) + "'"); clientSocket.send(response); System.out.println("Response sent."); System.out.println("--------------------"); } catch (Exception e) { e.printStackTrace(); } } } } 

And here are my Android client classes:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Receiver()).start(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new Client()).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } public class Receiver implements Runnable { private final static int LISTENING_PORT = 10001; @Override public void run() { try { //Opening listening socket Log.d("UDP Receiver", "Opening listening socket on port "+LISTENING_PORT+"..."); DatagramSocket socket = new DatagramSocket(LISTENING_PORT); socket.setBroadcast(true); socket.setReuseAddress(true); while(true){ //Listening on socket Log.d("UDP Receiver", "Listening..."); byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); Log.d("UDP", "Received: '" + new String(packet.getData()).trim() + "'"); } } catch (Exception e) { Log.e("UDP", "Receiver error", e); } } } public class Client implements Runnable { private final static String SERVER_ADDRESS = "173.246.12.125";//public ip of my server private final static int SERVER_PORT = 10000; @Override public void run() { try { //Preparing the socket InetAddress serverAddr = InetAddress.getByName(SERVER_ADDRESS); DatagramSocket socket = new DatagramSocket(); //Preparing the packet byte[] buf = ("Hello computer").getBytes(); DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVER_PORT); //Sending the packet Log.d("UDP", String.format("Sending: '%s' to %s:%s", new String(buf), SERVER_ADDRESS, SERVER_PORT)); socket.send(packet); Log.d("UDP", "Packet sent."); } catch (Exception e) { Log.e("UDP", "Client error", e); } } } 

On the server console, the client IP address is displayed:

 Connecting on 192.168.1.126:10000... Connected. ==================== Listening... Received: 'Hello computer' from /204.48.72.68 Sending: 'Hello Android' Response sent. -------------------- Listening... 

The package seems to come from 204.48.72.68, but if I go to whatismyip.com on my Android, it will show me 96.22.246.97 ... I don’t know where 204.48.72.68 comes from ..

I am not sure if the problem is that my listening socket on my Android client is not suitable or if the PC server is not sending a response to the correct address. Can someone tell me what I am doing wrong?

thanks

+4
source share
2 answers

I ran into a similar problem, but instead of UDP I used TCP sockets. My intensity sent files directly to my mobile. On the local network, this worked quite a lot. It seems that it is impossible to send data to listening sockets when your phone is connected to the Internet using a mobile connection. I read on some pages (sry no longer has links) that incoming connections on a mobile phone are blocked by a telecommunications service provider. My workaround was to create outgoing connections to the server and use the bi-directional capabilities of tcp sockets. Perhaps you can use your β€œworking” datagram socket to exchange data with your mobile phone. Here is the example I found: http://itucet.blogspot.de/2011/03/java-bidirectional-data-transfer-using.html

+1
source

The same code works well for me. You have a problem when trying with the emulator, but it works great if you use any Android mobile.

The cause of the problem is the android emulator and your computer is not on the same subnet.

+1
source

All Articles