Android的多播只能工作使用255.255.255.255地址 [英] Android Multicast is only working using 255.255.255.255 address

查看:1175
本文介绍了Android的多播只能工作使用255.255.255.255地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置多播主机使用的端口5500。然后230.0.0.1,在另一边,我说要加入组230.0.0.1的端口5500。它加入了它接收的数据包了几秒钟。然后停止突然。如果我使用255.255.255.255接收到的数据包正常。这究竟是为什么?在code为组播发送者是如下:

 私有类StatusBroadcasterThread继承Thread
{

    私有静态最终布尔DEBUG = App.DEBUG;
    私有静态最后字符串变量=StatusBroadcasterThread;

    私人DatagramSocket的broadcastSocket;

    公共StatusBroadcasterThread(INT端口)抛出SocketException {

        broadcastSocket =新的DatagramSocket(端口);

        this.start();
    }

    @覆盖
    公共无效的run(){

        而(!this.isInterrupted()){

            尝试 {

                。byte []的缓冲区= status.toString()的GetBytes();

                DatagramPacket类包=新的DatagramPacket(缓冲区,buffer.length,InetAddress.getByName(App.Config.multicastAddress)
                        App.Config.multicastPort);

                broadcastSocket.send(包);

                如果(调试)
                    Log.d(TAG,已发送+新的String(packet.getData()));

            }赶上(IOException异常E){

                Log.e(TAG,错误:+ e.getMessage());
            }

            尝试 {
                睡觉(App.Config.broadcastInterval);
            }赶上(InterruptedException的前){
            }
        }
    }
}
 

接收线程:

 私有类ReceiverThread继承Thread
{

    私有静态最后字符串变量= ComMessageReceiver.TAG +主题;

    私人WifiManager wifiManager;
    私人的MulticastSocket的MulticastSocket;
    私人InetSocketAddress groupInetSocketAddress;
    私人布尔joinedGroup = FALSE;

    公共ReceiverThread(弦乐群,INT端口,INT超时)抛出IOException异常{

        超();

        wifiManager =(WifiManager)App.context.getSystemService(Context.WIFI_SERVICE);
        groupInetSocketAddress =新的InetSocketAddress(InetAddress.getByName(组),端口);
        MulticastSocket多=新的MulticastSocket(端口);
        multicastSocket.setSoTimeout(超时);

    }

    公共ReceiverThread()抛出IOException异常{

        这个(Config.multicastAddress,Config.multicastPort,DEFAULT_TIMEOUT);
    }

    @覆盖
    公共无效的run(){

        Log.d(TAG,启动);

        而(!this.isInterrupted()){

            如果(wifiManager.getWifiState()== WifiManager.WIFI_STATE_ENABLED){

                如果(!joinedGroup){

                    尝试 {

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

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

                        joinedGroup = TRUE;

                    }赶上(IOException异常前){

                        Log.e(TAG,无法加入组播组:+ ex.getMessage());
                    }
                }

                尝试 {

                    byte []的缓冲区=新的字节[256];

                    DatagramPacket类包=新的DatagramPacket(缓冲区,buffer.length);

                    multicastSocket.receive(包);

                    消息消息=新的信息(数据包);

                    Log.d(TAG,+ message.getIp()++ message.getMsg()的信息);

                    对于(消息监听监听器:listenerList)
                        listener.onMessageReceived(消息);

                }赶上(SocketTimeoutException如果前){

                    Log.e(TAG,超时:+ ex.getMessage());

                }赶上(IOException异常前){

                    Log.e(TAG,ex.getMessage());
                }
            } 其他
                joinedGroup = FALSE;
        }
    }

    InetAddress类getWifiInetAddress()抛出的UnknownHostException {

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

        返回InetAddress.getByAddress(wifiRawAddress.array());
    }

}
 

解决方案

1 255.255.255.255 不会组播地址,但广播地址。

2 请检查你的正确关闭插座当通信结束。

请参阅下面的所有多播地址的列表..........

  224.0.0.0基址(预留)
224.0.0.1的所有主机的组播组地址在同一网段的所有主机。
224.0.0.2所有路由器组播组地址在同一网段的所有路由器。
224.0.0.4这个地址是用在​​距离矢量组播路由协议(DVMRP),以解决组播路由器。
224.0.0.5开放式最短路径优先(OSPF)所有的OSPF路由器地址,用于Hello报文发送到所有的OSPF路由器上的网段上。
224.0.0.6 OSPF的所有D型路由器的地址是用来发​​送OSPF路由信息到指定路由器上的网段上。
224.0.0.9的路由信息​​协议(RIP)版本2组地址是用来发​​送路由信息给所有RIP2感知路由器上的网段上。
224.0.0.10增强内部网关路由协议(EIGRP)组地址来发送路由信息给所有EIGRP的路由器上的网段上。
224.0.0.13协议无关组播(PIM)版本2
224.0.0.18虚拟路由器冗余协议(VRRP)
224.0.0.19  -  21 IS-IS通过IP
224.0.0.22 Internet组管理协议(IGMP)第3版
224.0.0.102热备份路由器协议版本2(HSRPv2)/网关负载均衡协议(GLBP)
224.0.0.107 precision时间协议版本2对时延测量消息
224.0.0.251组播DNS(mDNS来)地址
224.0.0.252链路本地多播名称解析(LLMNR)地址
在多播模式下运行时224.0.1.1网络时间协议的客户端侦听该地址的协议消息。
224.0.1.39思科组播路由器AUTO-RP报站地址使用RP映射代理收听候选通知。
224.0.1.40思科组播路由器AUTO-RP-发现地址是从RP映射代理的消息,发现应聘者的目的地址。
224.0.1.41 H.323网守发现地址
224.0.1.129  -  132 precision时间协议版本1时间通知
224.0.1.129 precision时间协议版本2次公告
 

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. 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

这篇关于Android的多播只能工作使用255.255.255.255地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆