简体   繁体   中英

Java multicast socket not received out of localhost

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM