c#UDP检查我是否收到了整个数据包?

时间:2009-09-20 10:22:39

标签: c# udp

这是我的第一个udp测试程序,我想我现在明白了一点:) 这里的任何方式都是我的代码:

static void Main(string[] args)
        {
            Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSocket.Bind(new IPEndPoint(IPAddress.Any, 111));
            Console.WriteLine("Waiting for connection");
            byte[] buffer = new byte[1024*64];
            int count = udpSocket.Receive(buffer);
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 111);
            EndPoint endPoint = (EndPoint)ipEndPoint;
            udpSocket.ReceiveFrom(buffer, ref endPoint);

            Console.WriteLine("Message recived from, " + endPoint.ToString() + " data length: " + count);
            Console.ReadKey();

        }

但是如何确保我收到整个数据包?

2 个答案:

答案 0 :(得分:3)

您可以容纳最多64k字节的数据包,这是UDP数据包的最大大小。你总是会读完整个数据包。

答案 1 :(得分:-1)

最好的办法是检查UDP header中的长度字段,看看你是否有足够的字节。

相关问题