从 AndroidEmulator (--Genymotion--) 到 localhost 服务器 (10.0.2.2) 的 UDP 不起作用? [英] UDP from AndroidEmulator (--Genymotion--) to localhost Server(10.0.2.2) does not work?
问题描述
我只是尝试在 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?
谢谢.
推荐答案
好吧,我忘记提及的一件重要事情是我用于 android 的模拟器是 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 服务器 (10.0.2.2) 的 UDP 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!