您好我正在尝试连接到网络上的盒子。它上面有一个工作的UDP服务器。使用下面的代码,我能够与盒子通信并从我的手机发送/接收UDP数据包。但是,我无法弄清楚如何使用Android模拟器进行设置。我已经在StackOverflow以及其他论坛上阅读了很多内容并没有运气。我在Windows 8上
Android代码:
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到上面的那个。
我也移植了redir
telnet localhost 5554
redir add udp:4381:4381
从手机登录:
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模拟器登录:
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,但它不会转发响应。有谁知道我应该将目的地回复发送到哪里?
答案 0 :(得分:3)
没有办法可以直接将您的Android模拟器连接到网络上的任何物理内容。
Android Emulator创建自己的 LAN ,每个模拟器实例都是一个具有相同IP的新LAN实例(这没关系,因为它们无法互相访问)
如果您坚持使用仿真器与Box通信,则应在PC上创建UDP代理套接字(使用JAVA,C#,C ++,VB ...或任何您想要的桌面编程语言)
他们的桌面应用程序将起作用
在Android的开发者网站上有一个很好的主题。
请查看Here
祝你好运