1
votes

I have a server sending unicast UDP packets to 192.168.1.101, port 55555.

My Android device has IP 192.168.1.101. My Android device has a multicast socket bound on port 55555 joined on multicast group 230.1.1.111.

I am receiving datagrams on my Android's multicast socket from the server.

Does this make sense? Can a multicast socket receive datagrams which are not addressed to the multicast group it's joined?

2

2 Answers

2
votes

Can a multicast socket receive datagrams which are not addressed to the multicast group it's joined?

Yes it can. It can join zero or more multicast groups. That doesn't affect its unicast capabilities.

1
votes

Adding the below code for reference.. We are able to receive both Multicast and Unicast messages in the same port.

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MulticastReceiver {

        public static void main(String[] args) throws Exception {
            int mcPort = 1800;
            String mcIPStr = "239.255.255.250";
            MulticastSocket mcSocket = null;
            InetAddress mcIPAddress = null;
            mcIPAddress = InetAddress.getByName(mcIPStr);
            mcSocket = new MulticastSocket(mcPort);
            System.out.println("Multicast Receiver running at:"
                    + mcSocket.getLocalSocketAddress());
            mcSocket.joinGroup(mcIPAddress);


        boolean var=true;           
        while(var){ 
            DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
            System.out.println("Waiting for a  multicast message...");
            mcSocket.receive(packet);

        System.out.println("packet length is " +packet.getLength());


            String msg = new String(packet.getData(),0,1024);
            System.out.println("[Multicast  Receiver] Received:" + msg);
        }
            mcSocket.leaveGroup(mcIPAddress);
            mcSocket.close();


        }
    }