android studio自带模拟器,接收PC端UDP数据包,接收不到。设置端口转发后也没有效果。
设置端口转发
telnet localhost 5554
auth auth_token
ok
redir add udp:pc_port:android_port
ok
redir list
android 端代码
new Thread(
() -> {
try {
Log.i("udptest", "监听7777端口接收数据");
DatagramSocket socket = new DatagramSocket(7777);
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
while (true) {
Log.i("udptest", "监听7777端口等待接收数据");
try {
socket.receive(packet);
Log.i("udptest", "prot 7777 receive pack len = " + packet.getLength());
} catch (IOException ex) {
System.err.println("ex: " + ex);
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}).start();
PC端代码
try {
DatagramSocket socket = new DatagramSocket();
while (true) {
byte data[] = "a".getBytes();
// 对方的IP和端口号
System.out.println(data.length);
DatagramPacket pack = new DatagramPacket(data, data.length, InetAddress.getByName("127.0.0.1"), 7777);
socket.send(pack);
System.out.println("send message ");
ThreadUtil.sleep(3000);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
android端logcat查看一直接收不到数据,尝试更换端口,依然没有效果。
请问遇到过的朋友指教一下,会是什么原因导致的