无法将UDP数据包从开发机器发送到Android Emulator

时间:2013-01-29 23:47:06

标签: android android-emulator udp

由于Android模拟器不支持多播,如here所述,我一直在努力解决我的问题。

为此,我编写了一个小型Java程序,它侦听多播数据包,然后将其重复到localhost地址。因为这个“多播转发器”在与Android仿真器相同的机器上运行,所以我希望能够将多播数据包环回到仿真器中。不幸的是,这似乎不起作用。

这是我在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'(描述为here)但是这引发了异常,虽然这似乎只有在我想从模拟器访问我的开发机器时才有用,而我想从我的访问模拟器开发机器。 有没有人有任何建议?

0 个答案:

没有答案