0

I, i'm developing a mobile application (Android) that works with a server application on PC side. I need to use multicast UDP datagrams to sending information at smartphone connected to WIFI area. I have two module: The first module is a UDP multicast server.

private void connection() {
    System.setProperty("java.net.preferIPv4Stack", "true");
    String msg = "Hello";
    InetAddress group = null;
    try {
        group = InetAddress.getByName("224.0.2.0");
    } catch (UnknownHostException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
    while (true) {
        MulticastSocket s = null;
        try {
            s = new MulticastSocket(6789);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            s.joinGroup(group);
            s.setTimeToLive(200);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        DatagramPacket hi = new DatagramPacket(msg.getBytes(),
                msg.length(), group, 6789);
        try {

            s.send(hi);
            System.out.println(hi.toString());
            s.leaveGroup(group);
            s.close();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This function (up) create a MulticastSocket and sending information at multicast address 224.0.2.0:6789.

The second module is a java receiver for the UDP packets sent by the first program.

byte[] b = new byte[1024]; 
            DatagramPacket dgram = new DatagramPacket(b, b.length); 
            MulticastSocket socket = null;
            try {
                socket = new MulticastSocket(6789);
            } catch (IOException e) {
                Log.e("WIFI_E", e.getMessage());
            } // must bind receive side
            try {
                socket.joinGroup(InetAddress.getByName("224.0.2.0"));
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            //while(true) { 
             try {
                socket.receive(dgram);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // blocks until a datagram is received
             Toast.makeText(getApplicationContext(), "Received " + dgram.getLength() + 
                        " bytes from " + dgram.getAddress(), Toast.LENGTH_LONG); 
             dgram.setLength(b.length); // must reset length field!
            //} 

This is my code. Now the problem. When i'm starting the server (PC side), UDP packets are visible only on localhost machine (tested with Wireshark) and the smarthpone or other PC cannot receive their. I try to turn off Windows firewall and antivirus but not working. I have no idea why packets are not redirected correctly on the network. Maybe some errors in my code? Thanks you.

Alessio Melani
  • 413
  • 3
  • 6
  • 7
  • how are the different machines connected? Is it possible your router is configured not to send multicast? – xaxxon Aug 21 '13 at 20:37
  • Have you already found a solution? I'm struggling with a similar project, which does nearly the same and can't get it working. Note: Using *nmap* I found out that the specified port stays closed when running the program. – s3lph Jan 17 '14 at 21:08

1 Answers1

0

On the Android device you need to acquire a MulticastLock. You also need the following permissions:

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

Having said that, it appears that Multicast support in Android is not as solid as some of use might hope. See http://codeisland.org/2012/udp-multicast-on-android/

Ie whether it actually works out or may be device dependent. It is not working on my Nexus5

William
  • 20,150
  • 8
  • 49
  • 91
  • I am in the same case. How we could implement another way to multicast UDP packet. Maybe, if we could code in low level. But i can not find another solution. I saw one library, cling, that make able to implement comunication for PnP devices. And it is working in most of the devices that I could test where the Multicast didn't work. Any suggestion about it? – Juan Pedro Martinez Feb 21 '14 at 15:36
  • I can't comment on Cling. But I found I could make it work but I needed to keep within particular multicast addresses. – William Feb 22 '14 at 06:34