views:

313

answers:

1

Hi all

I want to communicate two emulators via DatagramSocket in Android. Each of them is a Node in a P2P system. Thus each of them has a server Thread and client Thread (created per GUI event). This is how I create server

public static final String SERVERIP = "10.0.2.15";
    //...
    run() {
                InetAddress serverAddr = InetAddress.getByName(SERVERIP);
                DatagramSocket socket = new DatagramSocket(SERVERPORT,serverAddr);
                while(true) {
                    byte[] buf = new byte[29];
                    DatagramPacket packet = new DatagramPacket(buf, buf.length);
                    socket.receive(packet);
                //... 
                }
     }

The port is given by the user during initializing application. The client part (requesting some data)

InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
//...
Log.i("Requester", "Trying to connect to device port = "+target);
DatagramSocket socketJ = new DatagramSocket();
byte[] bufJ = Adaptor.createStringMsg(Adaptor.createJoingMsg(id, Location.getX(), Location.getY())).getBytes();             
DatagramPacket packetJ = new DatagramPacket(bufJ, bufJ.length, serverAddr, target);
Log.i("Requester", "Sending: '" + new String(bufJ) + "'");
socketJ.send(packetJ);
Log.i("Requester", "Done.");

Some additional info. The Node1 (emulatorA) has a server on port 8000 and Node2 (emulatorB) has a server on port 8001. The target port for "client part" is read properly. What tried to do is to set the redirection as such:

//emulatorA
redir add tcp:8000:8000
//emulatorB
redir add tcp:8001:8001

However I can not get any communication beetwen those 2 emulators. As far as I understood the android tutorial about it should work like this redir add tcp:localhostPort:emulatorPort. I'm stuck with it :/. Can anyone point me the mistake or give some good advice. For the record while I was testing communication on a single device (client faking other node) everything worked, so I don't think there is a bug in the code.

Btw does any one knows how can I get 2 set of logs for those 2 emulators (logA, logB)? It would help me a lot.

@Fred Grott Yes I have conected through telnet to each emulator. Thx for the tip with logs.

A: 

Datagram Sockets use UDP. The port redirects you have stated that you setup are for TCP. If you try:

redir add udp:localhostPort:emulatorPort

You may have better luck. However if you change your implementation to use Sockets and communicate via streams that will work too.

Greg