Search code examples
javasocketsraspberry-pimulticastsocket

Java Soket cannot send multicast packet on Raspberry Pi


I try to run this code in Raspberry Pi. The socket can receive data from the multicast group, however, it shows the following error when it tries to send data:

pi@raspberrypi:~ $ java  Protocol
java.net.MulticastSocket@647e05
java.io.IOException: Cannot assign requested address
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:693)
    at Protocol.getSensorData(Protocol.java:201)
    at Protocol.main(Protocol.java:305)

Here is the code:

import java.net.*;
import java.util.*;

public class Protocol {

    private MulticastSocket socket = null;
    private MulticastSocket socket_switchPanel = null;
    String MULTICAST_ADDRESS = "";
    int port = -1;

    public Protocol(String NIC, boolean isByIP, String multcastAddress, int port) {
        this.MULTICAST_ADDRESS = multcastAddress;
        this.port = port;
        try {
            InetAddress dstAddr = InetAddress.getByName(MULTICAST_ADDRESS);
            socket = new MulticastSocket(port);
            InetSocketAddress socketAddress = new InetSocketAddress(MULTICAST_ADDRESS, port);

            NetworkInterface ni = NetworkInterface.getByName(NIC);
            socket.setReuseAddress(true);
            socket.joinGroup(socketAddress, ni);
            System.out.println(ni);

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            if (socket != null) {
                socket.close();
            }
        }
    }

    public void close_socket() {
        if (socket != null) {
            socket.close();
        }
    }

    public Integer getSensorData() {

        byte[] msg = new byte[]{
                (byte) 0xAB, 0x04, (byte) 0x82, (byte) 0xCD, 0x00, (byte) 0x01};
        try {
            // get the IP address
            InetAddress dstAddr = InetAddress.getByName(MULTICAST_ADDRESS);

            final DatagramPacket out = new DatagramPacket(msg, msg.length, dstAddr, port);
            socket.send(out);
            // receive data until timeout or stop prematurely on users' request
            try {
                // process multi cast response(s)
                final byte[] inputBuffer = new byte[30];
                final DatagramPacket in = new DatagramPacket(inputBuffer, inputBuffer.length);
                socket.setSoTimeout(500);
                socket.receive(in);
                byte[] data = in.getData();
                return 1;

            } catch (Exception ste) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }


    public static void main(String[] args) {
//        tring NIC, boolean isByIP, String multcastAddress, int port
        Protocol dc = new Protocol("wlan0",
                false,
                "ff12:00:00:00:4479:00:00:00",
                50000);
        int ab = dc.getSensorData();
        System.out.println(ab);
        return;
    }
}

Solution

  • I find the solution. You have to disable other interface. For example, if you are using WIFI to send multicast message. You can disable eth0 by using ifconfig eth0 down. Note that after the PI reboots, the eth0 goes live again.