4

因为 Android 模拟器不支持这里描述的多播,所以我一直在尝试解决我的情况。

为此,我编写了一个小型 Java 程序,它侦听多播数据包,然后将其重复到 localhost 地址。因为这个“多播中继器”与安卓模拟器在同一台机器上运行,我希望我可以将多播数据包环回模拟器。不幸的是,这似乎不起作用。

这是我在 Eclipse 和模拟器之外运行的“多播转发器”方法。我检查了它是否从网络上其他地方的另一台机器接收到 UDP 消息,该机器传输包含时间的 UDP 数据包:

public static void main(String[] args) throws IOException{
        boolean loop = true;
        MulticastSocket socket = new MulticastSocket(4446);
        InetAddress address = InetAddress.getByName("230.0.0.1");
    socket.joinGroup(address);
        DatagramSocket txSocket = new DatagramSocket();   // UDP socket to re transmit on
        DatagramPacket packet;
        while (loop){
        byte[] buf = new byte[256];
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);

            String received = new String(packet.getData(), 0, packet.getLength());
            System.out.println("Quote of the Moment: " + received);
            packet = new DatagramPacket(received.getBytes(), received.getBytes().length, InetAddress.getLocalHost, 4448);
            txSocket.send(packet);
    }

    socket.leaveGroup(address);
    socket.close();
    }

这是我在 android 应用程序中运行的“接收器”代码:

ConnectivityManager connMgr = (ConnectivityManager) 
                getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                txtView.append("Network connected.\r\n");
                WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                WifiManager.MulticastLock wmmc = wm.createMulticastLock("MulticastTest");
                if (wmmc.isHeld()){
                    txtView.append("Multicast lock is held\r\n");
                }else{
                    txtView.append("Multicast lock NOT held, attempting to acquire.\r\n");
                    wmmc.acquire();
                }
                if (wmmc.isHeld()){
                    try{
                        DatagramSocket rxSocket = new DatagramSocket(4448);
                        rxSocket.setSoTimeout(5000);
                        DatagramPacket packet;

                            // get a few quotes
                        for (int i = 0; i < 5; i++) {

                            byte[] buf = new byte[256];
                                packet = new DatagramPacket(buf, buf.length);
                                try{
                                    rxSocket.receive(packet);
                                    String received = new String(packet.getData(), 0, packet.getLength());
                                    txtView.append(received + "\r\n");
                                } catch (InterruptedIOException ex){
                                    txtView.append("Socket timed out.\r\n");
                                }
                                //System.out.println("Quote of the Moment: " + received);
                        }

                        //socket.leaveGroup(address);
                        //socket.close();
                    }catch (Exception ex){
                        txtView.append(ex.toString() + "\r\n");
                    }finally{
                        wmmc.release();
                    }
                }else{
                    txtView.append("Could not acquire multicast lock.\r\n");
                }
            }else{
                txtView.append("Network NOT connected");
            }

不幸的是,接收套接字刚刚超时。我尝试绑定到“10.0.2.2”(在此处描述),但这会引发异常,尽管这似乎只有在我想从模拟器访问我的开发机器而我想从我的开发机器访问模拟器时才有用。有没有人有什么建议?

4

0 回答 0