Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android多播只能使用255.255.255.255地址工作_Java_Android_Sockets_Network Programming_Multicast - Fatal编程技术网

Java Android多播只能使用255.255.255.255地址工作

Java Android多播只能使用255.255.255.255地址工作,java,android,sockets,network-programming,multicast,Java,Android,Sockets,Network Programming,Multicast,我尝试使用端口5500将多播主机设置为230.0.0.1。然后,在另一边,我说在端口5500加入230.0.0.1组。它加入并在几秒钟内收到数据包。然后它突然停止了。如果我使用255.255.255.255,它将正常接收数据包。为什么会这样?多播发送方的代码如下所示: private class StatusBroadcasterThread extends Thread { private static final boolean DEBUG = App.DEBUG; pri

我尝试使用端口5500将多播主机设置为230.0.0.1。然后,在另一边,我说在端口5500加入230.0.0.1组。它加入并在几秒钟内收到数据包。然后它突然停止了。如果我使用255.255.255.255,它将正常接收数据包。为什么会这样?多播发送方的代码如下所示:

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) {
            }
        }
    }
}
接收器线程:

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
不是多播地址,而是广播地址。

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
2.通信完成后,请检查您是否正确关闭了插座

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

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

因此,如果我想让事情正常运行,我需要使用这些地址中的一个?您的230.0.0.1在多播范围内..并且
因此应该可以工作
。我已经给了您列表供参考<代码>类别D用于多播,其范围为224.0.0.0-239.255.255.255您说它有时工作,但后来停止工作。。。我建议您检查您的线程,关闭流,连接,检查服务器是否在5500端口停止侦听。使用netstat-n/a检查端口。它表示正在超时。请查看接收器线程。尝试
删除超时并执行它。。。。。。。。。。。。。。。。。。我认为它超时了,在连接建立之前,也检查你的“Wifi状态”,while循环中有一个条件,看看它是否工作……嗨,你是如何测试这段代码的?使用安卓本地主机还是本地网络?