2

I've got a very simple draw application on my PC that stores points and draws an oval for each point.

I'm trying to send this to an android application that will receive coordinates for each point and then draw the ovals.

Right now I'm using Multicast sockets, but it doesn't seem to work. I'm afraid I'm missing something.

The InetAddress and port are the same in both applications: 234.235.236.237 and 9876

In my PC application the MultiCastSocket initialization code looks like this:

private void InitiateSocket() throws IOException
{
    //  Create a socket and start the communication
socket = new MulticastSocket(port);
iAddr = InetAddress.getByName("234.235.236.237");

    //  Joins the multicastSocket group
socket.joinGroup(iAddr);
} 

And when I want to send a coordinate I call this method:

public void SendMessage(int x, int y)
{

    //Translate message to bytes
byte[] _data = (x+ " " + y).getBytes();

    //Create and send a packet
DatagramPacket _packet = new DatagramPacket(_data, _data.length, iAddr, port);

    try {
        socket.send(_packet);
        textArea.append(x + " " + y + " ");
    } catch (IOException e) {
        System.exit(0);
    }
} 

Then my Android application looks like this:

WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.acquire();


//Create a socket and start the communication
_socket = new MulticastSocket(_port);

//    Joins the multicastSocket group
_socket.joinGroup(_iadr);

//    Add a new Client to the socket
new Client(_socket, this);

When I want to send a message I call this, which is very similar to the one in the pc applikacation

    private void SendMessage(String _s)
{
    //    Translate message to bytes
    byte[] _data = (_name + ": "+ _s).getBytes();

    //    Create and send a packet
    DatagramPacket _packet = new DatagramPacket(_data, _data.length, _iadr, _port);

    try {
        _socket.send(_packet);
    } catch (IOException e) {
        _field.append(e.toString());
    }
}

Then the Client class listens to the socket with this code:

@Override
public void run() {

    byte[] _data = new byte[1024];
    while(true)
    {
        try
        {
            //Datagram for receiving
            DatagramPacket packet = new DatagramPacket(_data, _data.length);

            //Receive the packet, convert it and send it to the Activity class
            _socket.receive(packet);
            String _medd = new String(_data, 0 , packet.getLength());
            _csta.input = _medd;
        }
        catch(IOException e)
        {
            //Will break when the socket is closed
            break;
        } 
    }
}

Thing is that I receive my own messages sent from the android application, but I do not receive messages sent from the PC. So I'm guessing it has something to do with the connection.

I have set android.permission.CHANGE_WIFI_MULTICAST_STATE and android.permission.INTERNET in the AndroidManifest.

I hope the information I have given is enough.

I'm using the emulator and android 2.2.

Off topic: I don't really understand how MulticastSockets work, how can I just pick a class D IP and port?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Orujimaru
  • 835
  • 2
  • 13
  • 18
  • I found a code.google thread reporting that android doesn't support multicastSocket, the thread is old though, can anyone confirm that this is the issue? – Orujimaru Sep 18 '11 at 10:32

1 Answers1

0

It seems that the emulator does not have support for multicast:

https://stackoverflow.com/a/3179939/1686442

Community
  • 1
  • 1
Scott Wardlaw
  • 652
  • 1
  • 8
  • 13