2012-03-26 124 views
0

我有一個udp套接字奇怪的問題。首先,請允許我向您提供的代碼:UDP套接字問題

服務器端:

 UdpClient udpClient = new UdpClient(UdpPort); 

     udpClient.Connect(pendingClient.IpEndPoint, pendingClient.UdpPort); 

     foreach (int frameNumber in pendingClient.FramesToSend) 
     { 
      byte[] frameBytes = ... 
      udpClient.Send(frameBytes, frameBytes.Length); 
     } 
     udpClient.Close(); 

客戶端:

using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) 
     { 
      socket.Bind(new IPEndPoint(IPAddress.Any, UdpPort)); 

      socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000); 

      Console.WriteLine("Receiving frames"); 
      while (!_sendingCompleted) 
      { 
       byte[] receive = new byte[Constants.FrameSize + Frame.HeaderSizeBytes]; 
       try 
       { 
        socket.Receive(receive); 
       } 
       catch (SocketException exception) 
       { 
        if (exception.SocketErrorCode == SocketError.TimedOut) 
        { 
         break; 
        } 

        throw; 
       } 
      ... 
} 

的問題是:當我從服務器發送到客戶端第一次一切都很完美,但是當我做第二次(等等)的時候 - 客戶端什麼都收不到。什麼可能是這個問題的原因,我怎麼解決它?

非常感謝。

回答

0

看起來你是不是在循環接收數據,請嘗試以下方法:

do 
{ 

    bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); 

    page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes); 

} while (bytes > 0);