UDP 客户端仅收到 1 条消息

发布于 2024-11-29 10:32:39 字数 771 浏览 3 评论 0原文

我目前正在开发一个服务器客户端应用程序。服务器通过 WAN 正常接收数据,客户端似乎也接收到数据,但客户端仅接收一次通信。 WAN 上是否有任何东西会使客户端始终只接收第一个返回的 UDP 通信,而不会接收后续的通信。感谢您的帮助。

客户端UDP监听代码

private void receiveUDP()
    {
        System.Net.IPEndPoint test = new System.Net.IPEndPoint(System.Net.IPAddress.Any,UDP_PORT_NUMBER);
        System.Net.EndPoint serverIP = (System.Net.EndPoint)test;
        server.Bind(serverIP);
        //server.Ttl = 50;

        EndPoint RemoteServ = (EndPoint)listenUDP;
        do
        {
            byte[] content = new byte[1024];
            int data = server.ReceiveFrom(content, ref RemoteServ);

            string message = Encoding.ASCII.GetString(content);


            ProcessCommands(message);


        } while (true);
    }

I have a server client application that I am currently working on. The server is receiving data fine over a WAN and the client seems to receive the data, but the client is only receiving one communication. Is there anything over a WAN that would make a client always only receive the first return UDP communication and none of the subsequent. Thanks for the help.

Client UDP Listening code

private void receiveUDP()
    {
        System.Net.IPEndPoint test = new System.Net.IPEndPoint(System.Net.IPAddress.Any,UDP_PORT_NUMBER);
        System.Net.EndPoint serverIP = (System.Net.EndPoint)test;
        server.Bind(serverIP);
        //server.Ttl = 50;

        EndPoint RemoteServ = (EndPoint)listenUDP;
        do
        {
            byte[] content = new byte[1024];
            int data = server.ReceiveFrom(content, ref RemoteServ);

            string message = Encoding.ASCII.GetString(content);


            ProcessCommands(message);


        } while (true);
    }

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

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

发布评论

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

评论(1

海未深 2024-12-06 10:32:39

这有点像在黑暗中进行刺探(因为您没有提供足够的代码来真正明确地说明发生了什么),但有一个主要原因导致您可能会始终看到某些 UDP 数据报无法通过 WAN 传送,而其他数据报则无法通过 WAN 传送总是能成功到达。这个原因就是MTU;可以在单个 UDP 数据报中发送的最大传输单元。这很容易产生诸如您所看到的行为(例如),您的第一个数据报是一条简短的“我接受您的连接”消息,然后您接着使用包含大文件的数据报;第一个(小)数据报小于 MTU 并被传送,而随后的(大)数据报大于 MTU,并在途中被丢弃。

对于 WAN 上的 UDP,MTU 不会高于大约 1500 字节,并且在许多情况下可能低至 1200 字节。任何大于该值的数据包都将被默默地丢弃在端点之间的某个位置。要通过 UDP 发送大数据块,您需要将它们分成小于传输数据的网段的 MTU 的数据块。

在 LAN 上,您通常可以发送任意大小的数据报。但一旦它们通过互联网或其他异构网络发送,它们很可能会被悄悄丢弃。

如果您确实需要发送大文件,您可以选择通过 TCP 传输; TCP 自动管理数据切割以适应 MTU,并确保其数据包全部被接收,并且按顺序接收;保证您不会收到通过 UDP 发送的数据报。

正如我上面提到的,这完全是在黑暗中刺探,实际上可能与你的实际问题无关。但这是房间里的大象,我们所要做的就是第一个数据包总是成功到达,而后面的数据包永远不会成功。

This is a bit of a stab in the dark (since you don't provide enough code to really say what's going on definitively), but there's one major reason why you might consistently see some UDP datagrams not be delivered over a WAN, while others always arrive successfully. This reason is MTU; the Maximum Transmission Unit which can be sent in a single UDP datagram. This can easily produce behaviour such as what you're seeing if (for example), your first datagram is a short "I accept your connection" message, and you then follow that with a datagrams containing large files; the first (small) datagram is smaller than the MTU and is delivered, while the following (large) datagrams are larger than the MTU, and are discarded en route.

For UDP over a WAN, the MTU will not be higher than about 1500 bytes, and in many situations may be as low as 1200 bytes. Any packets larger than that will be silently dropped somewhere between endpoints. To send large blocks of data via UDP, you need to chop them up into pieces smaller than the MTU for the network segment across which you're transmitting them.

On a LAN, you can usually get away with sending datagrams of any size. But as soon as they're being sent over the Internet or otherwise through heterogenous networks, they're likely to be silently discarded.

If you do need to send large files, you might choose to transmit them via TCP instead; TCP automatically manages chopping data up to fit within the MTU, and ensures that its packets are all received, and are received in order; guarantees that you will not receive from datagrams sent via UDP.

As I mentioned above, this is a complete stab in the dark and may not actually be related to your actual troubles. But it's the elephant in the room, when all we have to go on is that the first packet always arrives successfully, and later packets never do.

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