Imagine that you have three Android phones (A, B and C) connected to the same access point. So each phone has a different IP address. For example:
- A is at 192.168.1.50
- B is at 192.168.1.60
- C is at 192.168.1.70
In each one I've the same app installed. In this app there are two UDP sockets:
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(55000);
Since each phone is at a different IP address every phone can send packets to every other phone. For example, to send a packet to A from B:
InetAddress address = InetAddress.getByName("192.168.1.50");
byte[] data = "Hello".getBytes();
DatagramPacket sendPacket = new DatagramPacket(data, data.length, address, 55000);
sendSocket.send(sendPacket);
Now the question is: what is the best approach to develop and debug this application using three emulators on the same machine, such that the code to run it in the emulator is almost the same code to run it on a real setup?
Attempts I've made:
- Using the code provided here I've launched two emulators on the same machine and printed their IP addresses, obtaining the same IP address for both emulators:
fe80::5054:ff:fe12:3456%eth0
. So a dead end. - Launched only two emulators, each one on specific and different ports for the receive and send sockets. This approach works fine, but it isn't usable with more machines as the redirects and deploys start to become rather bothersome and complicated. Not to mention the fact that I've to play with the 10.0.2.15 and 10.0.2.2 IP addresses to get it working.