0

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;
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Sean
  • 4,267
  • 10
  • 36
  • 50

2 Answers2

0

Your java may be picking up the IP V4 instead of IP V6 (like your multicast address seems to point to). Try starting it with

-Djava.net.preferIPv6Addresses=true

Usually this happens with java preferring v6 over v4, but you might have a special case here. Alternatively, you're just using an invalid multicast address (looking at https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#ipv6-scope it doesn't appear that addresses starting with ff12 are valid for ipv6 multicast addresses).

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I tried to use IPV6 setting. It does not work. This address is correct and it is our intern server address. I can run the above code in Windows machine. But not sure why it does not work in Pi. Do you have any idea? – Sean Apr 07 '17 at 13:24
0

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.

Sean
  • 4,267
  • 10
  • 36
  • 50