UDP from AndroidEmulator (--Genymotion--) to localhost Server(10.0.2.2) does not work?(从 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 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 AndroidEmulator (--Genymotion--) 到 localhost Server(10.0.2.2) 的 UDP 不起作用?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
