UDP客户端仅收到1条消息 [英] UDP Client receives only 1 message

查看:145
本文介绍了UDP客户端仅收到1条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前正在处理的服务器客户端应用程序.服务器正在通过WAN很好地接收数据,而客户端似乎正在接收数据,但是客户端仅接收到一种通信. WAN上是否有任何内容可以使客户端始终只接收第一个返回的UDP通信,而没有接收到任何后续的UDP通信.谢谢您的帮助.

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.

客户端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);
    }

推荐答案

这在黑暗中有些刺痛(因为您没有提供足够的代码来确切说明正在发生的事情),但这是一个主要的原因为什么您可能会始终看到某些UDP数据报没有通过WAN传递,而另一些总是成功到达的原因.原因是MTU. 最大传输单位,它可以在单个UDP数据报中发送.这很容易产生一些行为,例如您所看到的(例如),第一个数据报是一条简短的我接受您的连接"消息,然后再跟随包含大文件的数据报;第一个(小)数据报小于MTU并已交付,而随后的(大)数据报大于MTU,并在途中被丢弃.

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.

对于WAN上的UDP,MTU不会高于1500字节,并且在许多情况下可能会低至1200字节.大于此大小的任何数据包将在端点之间静默丢弃.要通过UDP发送大数据块,您需要将它们切成小于要传输数据的网段的MTU的大小.

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.

在LAN上,通常可以发送任意大小的数据报.但是,一旦它们通过Internet或通过异构网络发送,它们很可能会被静默丢弃.

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.

如果确实需要发送大文件,则可以选择通过TCP传输它们; TCP自动管理斩波数据以适合MTU,并确保其所有数据包均已接收并按顺序接收;保证您不会收到通过UDP发送的数据报.

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.

这篇关于UDP客户端仅收到1条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆