gpt4 book ai didi

java - 为什么我的简单应用程序没有收到 UDP 数据包

转载 作者:行者123 更新时间:2023-12-03 11:55:33 34 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How can I fix 'android.os.NetworkOnMainThreadException'?

(66 个回答)


6年前关闭。




我正在尝试创建一个简单的应用程序,它将接收来自 WiFi 模块的 UDP 数据包。 WiFi模块每5秒发送一个数据包,但我一直无法成功接收。

使用 toasts 作为调试的一种方式,我以正确的顺序收到它们,直到“这里也没有得到它”,这告诉我问题出在 'socket.receive(packet)' 行。

有没有人知道为什么它可能不起作用?我还通过在线查看大量问题/示例尝试了代码的变体,但它们仍然会导致“socket.receive(packet)”行出现问题。

另外,我写了一个python脚本来做同样的事情,在同一个端口上寻找UDP数据包,并用QPython运行它,它确实收到了数据包,所以看起来数据包是由WiFi模块发送的,并且接收到了通过平板电脑,只是应用程序由于某种原因不会读取数据。

任何帮助或见解将不胜感激,谢谢。

这是代码:

public void run() {

Toast.makeText(this, "RIGHT BEFORE TRY", Toast.LENGTH_LONG).show();

DatagramSocket socket;
DatagramPacket packet;


try {
Toast.makeText(this, "IN THE TRY", Toast.LENGTH _LONG).show();
socket = new DatagramSocket(9750);
byte[] buf = new byte[1024]; //buffer
socket.setSoTimeout(100000);
Toast.makeText(this, "Timeout is: " + socket.getSoTimeout(), Toast.LENGTH_LONG).show();
//DatagramPacket packet = new DatagaramPacket(buf, buf.length);
//Toast.makeText(this, "PACKET SIZE IS: "+buf.length, Toast.LENGTH_LONG).show();

// while(true) {
Toast.makeText(this, "IN THE WILD!", Toast.LENGTH_LONG).show();
packet = new DatagramPacket(buf, buf.length);
Toast.makeText(this, "PACKET SIZE IS: " + buf.length, Toast.LENGTH_LONG).show();

try {
//socket.receive(packet);
socket.receive(packet);
Toast.makeText(this, "GOT SOMETHING!", Toast.LENGTH_LONG).show();
}
catch (Exception i) {
// TODO Auto-generated catch block
//i.printStackTrace();
Toast.makeText(this, "DIDN'T GET IT HERE EITHER!", Toast.LENGTH_LONG).show();
}
//byte[] result = new byte[packet.getLength()];
byte[] result = packet.getData();
System.arraycopy(packet.getData(), 0, result, 0, packet.getLength());
String msg = new String(result);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
Toast.makeText(this, "END WHILE!", Toast.LENGTH_LONG).show();
//}

} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Toast.makeText(this, "DIDN'T GET IT!", Toast.LENGTH_LONG).show();

}

}

更新:在前两个答案之后,看起来我们试图在主线程中进行网络操作,这是不可行的,所以我们目前正在尝试用另一个线程或异步任务来实现它。希望有一些好消息:)

最佳答案

正如您所说,有 NetworkOnMainThread 异常,您只需将此问题放在新线程上并启动它...

Thread thread = new Thread() {
@Override
public void run() {
Toast.makeText(this, "RIGHT BEFORE TRY", Toast.LENGTH_LONG).show();

DatagramSocket socket;
DatagramPacket packet;


try {
Toast.makeText(this, "IN THE TRY", Toast.LENGTH _LONG).show();
socket = new DatagramSocket(9750);
byte[] buf = new byte[1024]; //buffer
socket.setSoTimeout(100000);
Toast.makeText(this, "Timeout is: " + socket.getSoTimeout(), Toast.LENGTH_LONG).show();
//DatagramPacket packet = new DatagaramPacket(buf, buf.length);
//Toast.makeText(this, "PACKET SIZE IS: "+buf.length, Toast.LENGTH_LONG).show();

// while(true) {
Toast.makeText(this, "IN THE WILD!", Toast.LENGTH_LONG).show();
packet = new DatagramPacket(buf, buf.length);
Toast.makeText(this, "PACKET SIZE IS: " + buf.length, Toast.LENGTH_LONG).show();

try {
//socket.receive(packet);
socket.receive(packet);
Toast.makeText(this, "GOT SOMETHING!", Toast.LENGTH_LONG).show();
}
catch (Exception i) {
// TODO Auto-generated catch block
//i.printStackTrace();
Toast.makeText(this, "DIDN'T GET IT HERE EITHER!", Toast.LENGTH_LONG).show();
}
//byte[] result = new byte[packet.getLength()];
byte[] result = packet.getData();
System.arraycopy(packet.getData(), 0, result, 0, packet.getLength());
String msg = new String(result);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
Toast.makeText(this, "END WHILE!", Toast.LENGTH_LONG).show();
//}

} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Toast.makeText(this, "DIDN'T GET IT!", Toast.LENGTH_LONG).show();

}


}
};
thread.start();

关于java - 为什么我的简单应用程序没有收到 UDP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403639/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com