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 不起作用?
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01