UDP从AndroidEmulator(--Genymotion--)到本地主机服务器(10.0.2.2)不工作? [英] UDP from AndroidEmulator (--Genymotion--) to localhost Server(10.0.2.2) does not work?

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

问题描述

我只是尝试MonodroidApp(AndroidEmulator)和localDevServer之间发送和接收数据。我明白本地主机是专门映射为10.0.2.2的Andr​​oidEmulator,所以我做了以下,但应用程序没有响应。

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

这code被证实与单声道工作的Mac和使用指针localDevServer:远程主机=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?

感谢你。

推荐答案

好,一个重要的事情,我忘了提的是 我使用的为Android模拟器是Genymotion。

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

因此​​,这似乎是,10.0.2.2并不本地主机指向默认

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

<一个href="http://blog.zeezonline.com/2013/11/access-localhost-from-genymotion/">http://blog.zeezonline.com/2013/11/access-localhost-from-genymotion/

在我的环境中(OSX 10.9)与Genymotion,从仿真器的本地主机地址

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

10.0.3.2 ,和code ++工程。

"10.0.3.2", and the code works.

这篇关于UDP从AndroidEmulator(--Genymotion--)到本地主机服务器(10.0.2.2)不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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