从 AndroidEmulator (--Genymotion--) 到 localhost Server(10.0.2.2) 的 UDP 不起作用? [英] UDP from AndroidEmulator (--Genymotion--) to localhost Server(10.0.2.2) does not work?

查看:30
本文介绍了从 AndroidEmulator (--Genymotion--) 到 localhost Server(10.0.2.2) 的 UDP 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试在 MonodroidApp(AndroidEmulator) 和 localDevServer 之间发送和接收数据.我了解 localhost 在 AndroidEmulator 上专门映射到10.0.2.2",所以我做了以下操作,但应用程序没有响应.

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);

此代码经验证可与 Mono for Mac 和带有指针的 localDevServer 一起使用:remoteHost = "127.0.0.1"

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

所以,

remoteHost = "10.0.2.2" 模式不起作用.

remoteHost = "10.0.2.2" pattern does not work.

我错过了什么?任何人,有什么想法吗?

What do I miss? Anyone, any thought?

谢谢.

推荐答案

好吧,我忘了说一件重要的事情是我用的安卓模拟器是Genymotion.

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

所以,10.0.2.2"似乎没有将 localhost 作为默认值.

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/

在我使用 Genymotion 的环境(OSX 10.9)中,模拟器的本地主机地址是

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

10.0.3.2",代码就可以了.

这篇关于从 AndroidEmulator (--Genymotion--) 到 localhost Server(10.0.2.2) 的 UDP 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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