UDP from AndroidEmulator (--Genymotion--) to localhost Server(10.0.2.2) does not work?

烈酒焚心 提交于 2019-12-19 10:19:19

问题


I simply try to send and receive data between MonodroidApp(AndroidEmulator) and a localDevServer. I understand localhost is specially mapped to "10.0.2.2" on AndroidEmulator, so I did the following, but the app does not respond.

    System.Text.Encoding enc = System.Text.Encoding.UTF8;
        string sendMsg = "testtest";
        byte[] sendBytes = enc.GetBytes(sendMsg);

        int localPort = 39000;
        var udp = new System.Net.Sockets.UdpClient(localPort);

        //send data
        string remoteHost = "10.0.2.2";//"127.0.0.1";
        int remotePort = 15000;
        udp.Send(sendBytes, sendBytes.Length,
            remoteHost, remotePort);

        //receive data
        System.Net.IPEndPoint remoteEP = null;
        byte[] rcvBytes = udp.Receive(ref remoteEP);
        string rcvMsg = enc.GetString(rcvBytes);
        Console.WriteLine("received data:{0}", rcvMsg);
        Console.WriteLine("sender address:{0}/port:{1}",
            remoteEP.Address, remoteEP.Port);

This code is verified to work with Mono for Mac and the localDevServer with the pointer: remoteHost = "127.0.0.1"

so,

remoteHost = "10.0.2.2" pattern does not work.

What do I miss? Anyone, any thought?

Thank you.


回答1:


Ok, one important thing I forgot to mention is The emulator I use for android is Genymotion.

So, it appears to be that "10.0.2.2" does not point localhost as default.

http://blog.zeezonline.com/2013/11/access-localhost-from-genymotion/

In my environment(OSX 10.9) with Genymotion,the localhost address from the emulator is

"10.0.3.2", and the code works.



来源:https://stackoverflow.com/questions/20913638/udp-from-androidemulator-genymotion-to-localhost-server10-0-2-2-does-not

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