Android模拟器UDP无法接收;在手机工作正常 [英] Android Emulator UDP cannot receive; works fine on phone
问题描述
您好,我试图连接到一个框,在网络上。它上有一个工作的UDP服务器。随着code以下我能够与盒UDP数据包从我的电话沟通,发送/接收。不过,我可以如何设置使用Android模拟器想不通。我读过很多关于计算器以及与运气没有其他论坛。我在Windows 8
Hello I am trying to connect to a box that is on the network. It has a working UDP server on it. With the code below I am able to communicate with the box and send/receive UDP packets from my phone. However, I cannot figure out how to setup using the android emulator. I've read a lot on StackOverflow as well as other forums with not luck. I am on windows 8
Android的code:
Android code:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.os.Handler;
import android.util.Log;
public class udp implements Runnable {
// Private variable
private String IPServer = "10.0.2.2"; // For Emulator
//private String IPServer = "255.255.255.255"; // For Phone
private int portServer = 6286;
private int portDestin = 4381;
private InetAddress serverAddr;
private InetAddress localAddr;
private DatagramSocket socketSend;
private DatagramSocket socketList;
private DatagramPacket packetSend;
private DatagramPacket packetList;
private Handler uiHandler;
public udp(){
};
public void send() {
// Retrieve the server name
try {
Log.d("UDP", "Creating InetAddress");
serverAddr = InetAddress.getByName(IPServer);
} catch (Exception e) {
Log.e("UDP", "InetAddress Error:", e);
}
// Create UDP sockets
try {
Log.d("UDP", "Creating Sockets");
socketSend = new DatagramSocket(portServer);
socketList = new DatagramSocket(portDestin);
}
catch(Exception e) {
Log.e("UDP", "DatagramSocket Error:", e);
}
// Sets socket to broadcast
try {
Log.d("UDP", "SetBroadcast");
socketSend.setBroadcast(true);
}
catch(Exception e) {
Log.e("UDP", "SetBroadcast Error:", e);
}
// Create UDP packets
try {
Log.d("UDP", "Creating packets");
byte[] dataSend = new byte[32];
byte[] dataRead = new byte[32];
String msg = "Packet Broadcast";
dataSend = msg.getBytes();
packetSend = new DatagramPacket(dataSend, dataSend.length, serverAddr, portDestin);
packetList = new DatagramPacket(dataRead, dataRead.length, serverAddr, portDestin);
}
catch(Exception e) {
Log.e("UDP", "DatagramPacket Error:", e);
}
// Send packet
try {
Log.d("UDP", "Sending packet");
socketSend.send(packetSend);
}
catch(Exception e) {
Log.e("UDP", "Send Error:", e);
}
// Receive packet
Log.d("UDP", "Receiving packet");
for(int i=0; i<2; i++)
{
try {
socketList.receive(packetList);
}
catch(Exception e) {
Log.e("UDP", "Receive Error:", e);
}
String packetRec = new String(packetList.getData());
Log.d("UDP", "Received: "+packetRec);
}
socketSend.close();
socketList.close();
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
我要传播到整个网络;这就是为什么使用255.255.255.255即时通讯,但对于Android,我读了它不工作,我在模拟器中打开设置,找到我的IP,以上面的那个。
I need to broadcast to the entire network; that is why Im using 255.255.255.255 but for android I read that it doesn't work and I opened settings in the Emulator and found my IP to the one above.
我还口再导向
telnet localhost 5554
redir add udp:4381:4381
日志从电话:
log from phone:
02-11 12:01:09.743: D/UDP(17253): Creating InetAddress
02-11 12:01:09.743: D/UDP(17253): Creating Sockets
02-11 12:01:09.753: D/UDP(17253): SetBroadcast
02-11 12:01:09.753: D/UDP(17253): Creating packets
02-11 12:01:09.753: D/UDP(17253): Sending packet
02-11 12:01:09.753: D/UDP(17253): Receiving packet
02-11 12:01:09.753: D/UDP(17253): Received: Packet Broadcast????????????
02-11 12:01:09.763: D/UDP(17253): Received: ??????PACKETSTUFF???????????fP*
日志从Android模拟器:
log from android emulator:
02-11 20:00:22.742: D/UDP(1201): Creating InetAddress
02-11 20:00:22.742: D/UDP(1201): Creating Sockets
02-11 20:00:22.752: D/UDP(1201): SetBroadcast
02-11 20:00:22.772: D/UDP(1201): Creating packets
02-11 20:00:22.772: D/UDP(1201): Sending packet
02-11 20:00:22.772: D/UDP(1201): Receiving packet
02-11 20:00:22.772: D/UDP(1201): Received: Packet Broadcast????????????????????
在此先感谢
更新:
目前,我得到了它,使我的仿真器可以发送使用UDP转发NetworkActiv AUTAPF主机之外的数据包。它发出了我的UDP,但它不不转发响应。有谁知道在哪里我送前人的精力目的地应对?
I currently got it so that my emulator can send a packet outside the host computer using a UDP forwarder NetworkActiv AUTAPF. It sends out my UDP but it doesn't not forward the response. Does anyone know where I sould send the destination response to?
推荐答案
有没有办法你可以在你的Android模拟器连接到任何东西物理直接在网络上。
Android模拟器创建自己的 LAN ,然后每个模拟器实例使用相同的IP地址一个新的LAN实例(这是可以的,因为他们不能互相访问)
Android Emulator creates its own LAN and each emulator instance a new LAN instance with the same IPs (This is OK because they can't access each other)
如果你坚持使用模拟器与Box沟通,你应该建立在PC上的UDP代理插槽(使用Java,C#,C ++,VB ...或者你想要的任何桌面编程语言)
If you insist on using the Emulator to communicate with the Box , you should create a UDP proxy socket on your PC (Using JAVA , C# , C++ , VB ... or any desktop programming language you want)
他们这样你的桌面应用程序工作原理
They way your Desktop App Will Work
- 让你的局域网子网以后determain数据包的源。
- 在启动UDP监听器的端口服务器使用。
- 找到发送者的IP地址(有API来的),如果发件人是模拟器,用盒子IP重新发送数据包箱
有关于Android的开发者网站上的主题一个很好的demonistration。
There is a good demonistration on the subject on Android's developers website.
请看看这里
好运
这篇关于Android模拟器UDP无法接收;在手机工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!