您好,我正在尝试连接到网络上的盒子。它上面有一个工作的 UDP 服务器。使用下面的代码,我可以与盒子通信并从我的手机发送/接收 UDP 数据包。但是,我无法弄清楚如何使用 android 模拟器进行设置。我在 StackOverflow 以及其他不走运的论坛上阅读了很多内容。我在 Windows 8
安卓代码:
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*
从安卓模拟器登录:
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,但它不转发响应。有谁知道我应该将目的地响应发送到哪里?