Android Multicast is only working using 255.255.255.255 address

浪尽此生 提交于 2019-12-06 06:31:40

问题


I tried setting the Multicast host as 230.0.0.1 using the port 5500. Then, on the other side I said to join group 230.0.0.1 at port 5500. It joined and it received packets for a few seconds. Then it stops all of a sudden. If I use 255.255.255.255 it receives packets normally. Why is this happening? The code for the Multicast sender is below:

private class StatusBroadcasterThread extends Thread
{

    private static final boolean DEBUG = App.DEBUG;
    private static final String TAG = "StatusBroadcasterThread";

    private DatagramSocket broadcastSocket;

    public StatusBroadcasterThread(int port) throws SocketException {

        broadcastSocket = new DatagramSocket(port);

        this.start();
    }

    @Override
    public void run() {

        while (!this.isInterrupted()) {

            try {

                byte[] buffer = status.toString().getBytes(); 

                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(App.Config.multicastAddress),
                        App.Config.multicastPort);

                broadcastSocket.send(packet);

                if (DEBUG)                      
                    Log.d(TAG, "Sent: " + new String(packet.getData()));

            } catch (IOException e) {

                Log.e(TAG, "Error: " + e.getMessage());
            }

            try {
                sleep(App.Config.broadcastInterval);
            } catch (InterruptedException ex) {
            }
        }
    }
}

Receiver thread:

private class ReceiverThread extends Thread
{

    private static final String TAG = ComMessageReceiver.TAG + "Thread";

    private WifiManager wifiManager;
    private MulticastSocket multicastSocket;
    private InetSocketAddress groupInetSocketAddress;
    private boolean joinedGroup = false;

    public ReceiverThread(String group, int port, int timeout) throws IOException {

        super();

        wifiManager = (WifiManager) App.context.getSystemService(Context.WIFI_SERVICE);
        groupInetSocketAddress = new InetSocketAddress(InetAddress.getByName(group), port);
        multicastSocket = new MulticastSocket(port);
        multicastSocket.setSoTimeout(timeout);

    }

    public ReceiverThread() throws IOException {

        this(Config.multicastAddress, Config.multicastPort, DEFAULT_TIMEOUT);
    }

    @Override
    public void run() {

        Log.d(TAG, "started");

        while (!this.isInterrupted()) {

            if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {

                if (!joinedGroup) {

                    try {

                        multicastSocket.joinGroup(groupInetSocketAddress,
                                NetworkInterface.getByInetAddress(getWifiInetAddress()));

                        wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
                        wifiManager.createMulticastLock(TAG).acquire();

                        joinedGroup = true;

                    } catch (IOException ex) {

                        Log.e(TAG, "Failed to join Multicast group: " + ex.getMessage());
                    }
                }

                try {

                    byte[] buffer = new byte[256];

                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

                    multicastSocket.receive(packet);

                    Message message = new Message(packet);

                    Log.d(TAG, "message from " + message.getIp() + " " + message.getMsg());

                    for (MessageListener listener : listenerList)
                        listener.onMessageReceived(message);

                } catch (SocketTimeoutException ex) {

                    Log.e(TAG, "Timed out: " + ex.getMessage());

                } catch (IOException ex) {

                    Log.e(TAG, ex.getMessage());
                }
            } else
                joinedGroup = false;
        }
    }

    InetAddress getWifiInetAddress() throws UnknownHostException {

        ByteBuffer wifiRawAddress = ByteBuffer.allocate(4);
        wifiRawAddress.order(ByteOrder.LITTLE_ENDIAN).putInt(wifiManager.getConnectionInfo().getIpAddress());

        return InetAddress.getByAddress(wifiRawAddress.array());
    }

}

回答1:


1. 255.255.255.255 is NOT a multicast address but BroadCast address.

2. Please check that you are properly closing the sockets when the communication is completed.

See below the list of all the multicast address..........

224.0.0.0   Base address (reserved)
224.0.0.1   The All Hosts multicast group addresses all hosts on the same network segment.
224.0.0.2   The All Routers multicast group addresses all routers on the same network segment.
224.0.0.4   This address is used in the Distance Vector Multicast Routing Protocol (DVMRP) to address multicast routers.
224.0.0.5   The Open Shortest Path First (OSPF) All OSPF Routers address is used to send Hello packets to all OSPF routers on a network segment.
224.0.0.6   The OSPF All D Routers address is used to send OSPF routing information to designated routers on a network segment.
224.0.0.9   The Routing Information Protocol (RIP) version 2 group address is used to send routing information to all RIP2-aware routers on a network segment.
224.0.0.10  The Enhanced Interior Gateway Routing Protocol (EIGRP) group address is used to send routing information to all EIGRP routers on a network segment.
224.0.0.13  Protocol Independent Multicast (PIM) Version 2
224.0.0.18  Virtual Router Redundancy Protocol (VRRP)
224.0.0.19 - 21     IS-IS over IP
224.0.0.22  Internet Group Management Protocol (IGMP) Version 3
224.0.0.102     Hot Standby Router Protocol version 2 (HSRPv2) / Gateway Load Balancing Protocol (GLBP)
224.0.0.107     Precision Time Protocol version 2 peer delay measurement messaging
224.0.0.251     Multicast DNS (mDNS) address
224.0.0.252     Link-local Multicast Name Resolution (LLMNR) address
224.0.1.1   Network Time Protocol clients listen on this address for protocol messages when operating in multicast mode.
224.0.1.39  The Cisco multicast router AUTO-RP-ANNOUNCE address is used by RP mapping agents to listen for candidate announcements.
224.0.1.40  The Cisco multicast router AUTO-RP-DISCOVERY address is the destination address for messages from the RP mapping agent to discover candidates.
224.0.1.41  H.323 Gatekeeper discovery address
224.0.1.129 - 132   Precision Time Protocol version 1 time announcements
224.0.1.129     Precision Time Protocol version 2 time announcements


来源:https://stackoverflow.com/questions/11634942/android-multicast-is-only-working-using-255-255-255-255-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!