DatagramSocket.receive() 问题

发布于 2024-08-20 18:32:02 字数 510 浏览 6 评论 0原文

我使用 Java 中的 DatagramSocket 类从用 C 编写的客户端接收 udp 数据包。以下是接收代码(服务器套接字已设置):

byte[] inputByte = new byte[1];
        DatagramPacket recvdPacket = new DatagramPacket(inputByte, inputByte.length);



        try {
            serverSocket.receive(recvdPacket);

然后打印内容。我的调试消息(此代码中未显示)表明它已成功到达 receive() 部分并正在等待(这是一个阻塞调用)。问题是:

该服务器仅在发送消息的客户端运行时第二次接收数据包,而不是第一次。即使客户端在第一次运行时发送多个数据包,服务器也不会显示任何内容,直到第二次运行整个客户端。可以安全地假设问题出在服务器端而不是客户端吗? (客户端代码不是我写的……太恶心了,而且是用 C 写的)。

I'm using the DatagramSocket class in Java to receive udp packets from a client written in C. Here is the code that receives (the server socket is already set up):

byte[] inputByte = new byte[1];
        DatagramPacket recvdPacket = new DatagramPacket(inputByte, inputByte.length);



        try {
            serverSocket.receive(recvdPacket);

And then it prints the contents. My debugging messages (not shown in this code) indicate that it's successfully getting to the receive() part and is waiting (it's a blocking call). Here is the problem:

This server only receives packets the second time the client sending the messages is run, never the first. Even if the client sends multiple packets the first run, the server never shows anything until the second time the entire client is run. Is it safe to assume the problem is in the server side, not the client side? (the client side code was not written by me...it's disgusting, and in C).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-08-27 18:32:03

我会尽力给出最好的答案,但只有 3 行代码可供查看,你可能会做一些我看不到的坏事。您可以发布更多相关代码吗?

在我看来,问题可能出在客户端。还有一种可能是您的网络对 UDP 流量做了一些奇怪的事情(我遇到过其中一些)。如果您想隔离哪一方有问题,我建议通过两者在服务器计算机上运行 wireshark运行客户端,然后查看服务器计算机正在接收哪种数据包。确保在客户端的连续运行之间留出几秒钟的时间,然后过滤有问题的 UDP 端口。如果在客户端第一次运行时没有数据包到达服务器,那么您的问题可能出在客户端的某个地方。

I'll try to give the best answer I can, but with only 3 lines of code to look at, you may be doing something bad I can't see. Can you post some more of the relevant code?

Sounds to me like the problem may be in the client. There's also a possibility that your network is doing something funky to UDP traffic (I've run into some of those). If you want to isolate which side has the problem, I'd recommend running wireshark on the server machine through both runs of the client and then see what kind of packets the server machine is receiving. Make sure you give a few seconds between successive runs of the client, then filter for the UDP port in question. If there's no packets reaching the server on that first run of the client, then your problem is probably in the client somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文