DatagramSocket能否接收多播数据包

Can DatagramSocket Receive multicast Packets

我有一个在线程中运行的代码,我用它发送 DatagramPacket 到计算机中每个 NetworkInterface 的广播地址,也发送到多播组。具体如下:

try {
    String decl="Mymessage";
    DatagramPacket ackdp;
    while(true)
        {
        Thread.sleep(3000);
        //First sending multicast (not broadcast) packet to a multicast group
        //231.26.179.75:37486
        ackdp=new DatagramPacket(s.getBytes(),s.length(),multicastGroup,port);
        BroadcastSocket.send(ackdp);
        //Now sending same message to broadcast address of each of the n/w interfaces
        Enumeration nwInterfaces = NetworkInterface.getNetworkInterfaces();
        while(nwInterfaces.hasMoreElements())
                {
                NetworkInterface ni=(NetworkInterface) nwInterfaces.nextElement();
                if(ni.isLoopback()||!ni.isUp())
                        continue;
                for(InterfaceAddress ifa:ni.getInterfaceAddresses())
                        {
                        InetAddress broadcastIP=ifa.getBroadcast();
                        if(broadcastIP==null)
                                continue;
                        ackdp=new DatagramPacket(s.getBytes(),s.length(),broadcastIP,port);
                        BroadcastSocket.send(ackdp);
                        //port is same here :37486 (ip varies with network interface)
                        }
                }
        }
    } catch (Exception ex) {ex.printStackTrace();}

我的问题是: 我可以使用相同的 DatagramSocket 接收两个(多播和广播)数据包吗?请注意,两者都发送到同一端口。我应该在端口 37486 上打开 MulticastSocket 还是 DatagramSocket 来接收这两个数据包?

(数据包从 PC 发送但在 Android 上接收)

Can DatagramSocket receive multicast Packets

不行,因为不能加入组播组。

你没有问,但为了完整起见:

Can DatagramSocket send multicast Packets

是的。

Can MulticastSocket receive datagram (non-multicast) packets

是的。