4

I'm playing a little bit with multicast sockets. I write a server which sends a message to a android client. Until yet the client should only log the received message. I noticed that no multicast packet are received on my device.

Here is the code for the server (runs on the pc):

public class MulticastServer{

private int port;

private boolean running = false;

private MulticastSocket serverSocket;

private InetAddress group;

private String multicastAddress = "230.192.0.11";

public MulticastServer(int port) {
    super();
    this.port = port;
    init();
}

public MusicStreamerServer() {
    this(5500);
}

private void init() {

    try {
        group = InetAddress.getByName(multicastAddress);
        serverSocket = new MulticastSocket(port);
        serverSocket.joinGroup(group);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void start() throws IOException {
    System.out.println("server started");

    if (running)
        return;

    running = true;

    new Thread(new Runnable() {

        @Override
        public void run() {

            byte[] buf = new byte[1024];

            DatagramPacket packet = new DatagramPacket(buf, buf.length,
                    group, port);

            String msg = "msg";


            while (running) {

                                    packet.setData(msg.getBytes(), 0, msg.length());


                try {
                    serverSocket.send(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }).start();

}

public void stop() throws IOException {
    running = false;
} }

Here is the code for the android client:

public class MainActivity extends Activity {

private MulticastSocket socket;

private InetAddress group;

private String multicastAddress = "230.192.0.11";

private int port = 5500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();
}

private void init() {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();

    StrictMode.setThreadPolicy(policy);

    try {
        group = InetAddress.getByName(multicastAddress);
        socket = new MulticastSocket(port);
        socket.joinGroup(group);
        socket.setBroadcast(true);
    } catch (IOException e) {
        e.printStackTrace();
        Log.wtf("init", e.getMessage());
    }

    new Thread(new Runnable() {

        @Override
        public void run() {

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

            multicastLock.acquire();

            byte[] buf = new byte[1024];

            while (true) {

                try {
                    socket.receive(packet);

                    Log.d("receiver","received = " + (new String(packet.getData())));

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }).start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

I've tested the code with 2 different devices. The Nexus4 and the Nexus7 (2013) both running the latest Android.

Could anybody help me?

Thanks

  • In your sample I believe you're missing some code for DatagramPacket creation and handling. I also would like to state for a fact that Android can do mulitcast reception; I've tested it on the Nexus7 and other devices. – Paul Gregoire Jan 27 '14 at 01:51
  • Have you managed to resolve it yet ? – Ahmed May 04 '14 at 19:35
  • What network interface do you use? Wifi? Each interface may have its quirks, in order to optimize battery usage. – dolmen Sep 08 '14 at 21:09

2 Answers2

4

I've seen that the issue is really inconsistent.

Android version: 4.2.x

On Samsung S4 active: Multicast is working as expected.

On Samsung Note 10.1 and Nexus 4.2.3 Multicast is not working as expected.

239.x.x.x is not supported (and sadly it's the one used to multicast television...) 224.0.0.251 is working as expected.

I think they have a bug with the mask.

A multicast address is normally |1 1 1 0| MULTICAST Address | 224.0.0.0 - 239.255.255.255

  11100000.00000000.00000000.00000001 = 224.0.0.1
  11101111.00000000.00000000.00000001 = 239.0.0.1

So the mask should be 224.0.0.0/4 and not 224.0.0.0/8

William
  • 20,150
  • 8
  • 49
  • 91
Gabriel Klein
  • 367
  • 2
  • 6
  • Wow, I would've never guessed that in years (regarding the mask). Was using 239.7.7.* addresses. Not working. Tested 224.0.0.251 as you suggested - worked! Thank you! (My phone: Motorola Atria 4G - Android 2.3.6) – briggsm Dec 08 '14 at 11:42
2

Does your manifest request the proper permissions?

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

Also, you may want to play with the Advanced settings in the WiFi menu on your phone, both Wi-Fi optimizations and Keep Wi-Fi on during sleep may impact your ability to do multicasts.

Android Newbie
  • 711
  • 3
  • 9
  • Add that line to the Android XML but still, it's not working. Can anyone please elaborate on other configurations those are required to enable the wifi multicast on Android?? – MSD Paul Sep 02 '18 at 22:46