3

In my network I have several devices (linux, c++) and one android smartphone.
Now, in order to recognize the devices in the network I am sending out a multicast udp packet from the android smartphone with the following code:

public class SSDPSocket {
    SocketAddress mSSDPMulticastGroup;
    MulticastSocket mSSDPSocket;
    InetAddress broadcastAddress;

    public SSDPSocket() throws IOException {
        mSSDPSocket = new MulticastSocket(55325);
        broadcastAddress = InetAddress.getByName(SSDPConstants.ADDRESS);
        mSSDPSocket.joinGroup(broadcastAddress);
    }

    public void send(String data) throws IOException {
        DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(), broadcastAddress, SSDPConstants.PORT);
        mSSDPSocket.setTimeToLive(2);
        mSSDPSocket.send(dp);
    }

    public void receive() {
        // TODO: Implement!
    }
}

The address to which I am sending the udp packet is 239.255.255.250, port is 1900. This works fine, and I am able to receive the packets on my clients.
Now I have problems to implement the receive method. In this method I want to receive respones from all my special devices in the network. First the clients have to respond to the sender address (the ip of my smartphone) right? Then I would have some kind of loop in the receive()method where I can receive incomming udp respones right?

Do I have to open a new DatagramSocket for this? Or can I use the already opened MulticastSocket for responses? In the examples I found everybody is using the opened MulticastSocket for response, but how should this work since the MulticastSocket is bound to 239.255.255.250?

akuzma
  • 1,592
  • 6
  • 22
  • 49
Moonlit
  • 5,171
  • 14
  • 57
  • 95

1 Answers1

1

First looks like Android may require you to aquire a lock, to not ignore the multicast packets. So this might be something to look into.

http://www.whizzosoftware.com/forums/blog/1/entry-40-android-jmdns-and-wi-fi-multicast-packets/

Second your receive needs to be stuck in a while loop.

This could probably help you with both: Problem with MulticastSocket on Java-Android The original poster has their receive in a while loop.

Community
  • 1
  • 1
Aaron Ogle
  • 558
  • 3
  • 14