2009-09-20 93 views
0

嗯,這是我的第一個UDP測試程序,我想我現在明白了一點吧:) 這裏的任何方法是到目前爲止我的代碼:c#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(); 

     } 

但我如何確保我得到了整個包?

回答

3

數據包的空間最多爲64k字節,這是UDP數據包的最大大小。你會一直閱讀整個數據包。

-1

最好的辦法是檢查UDP header中的長度字段,看看是否有足夠的字節。

+0

好吧,我不知道我會怎麼做? – Peter 2009-09-20 10:30:35