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 {
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