2010-06-18 44 views
0

我使用Silverlight與我開發的代理應用程序進行交互,但沒有代理向Silverlight應用程序發送消息,它使用空緩衝區執行接收完成的處理程序('\ 0的)。有什麼我做錯了嗎?這導致內存泄漏主要Silverlight Socket持續返回空緩衝區

this._rawBuffer = new Byte[this.BUFFER_SIZE]; 
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs(); 
receiveArgs.SetBuffer(_rawBuffer, 0, _rawBuffer.Length); 
receiveArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ReceiveComplete); 
this._client.ReceiveAsync(receiveArgs); 

if (args.SocketError == SocketError.Success && args.LastOperation == SocketAsyncOperation.Receive) 
{ 
    // Read the current bytes from the stream buffer 
    int bytesRecieved = this._client.ReceiveBufferSize; 
    // If there are bytes to process else the connection is lost 
    if (bytesRecieved > 0) 
    { 
     try 
     { 
      //Find out what we just received 
      string messagePart = UTF8Encoding.UTF8.GetString(_rawBuffer, 0, _rawBuffer.GetLength(0)); 
      //Take out any trailing empty characters from the message 
      messagePart = messagePart.Replace('\0'.ToString(), ""); 
      //Concatenate our current message with any leftovers from previous receipts 
      string fullMessage = _theRest + messagePart; 
      int seperator; 

      //While the index of the seperator (LINE_END defined & initiated as private member) 
      while ((seperator = fullMessage.IndexOf((char)Messages.MessageSeperator.Terminator)) > 0) 
      { 
       //Pull out the first message available (up to the seperator index 
       string message = fullMessage.Substring(0, seperator); 
       //Queue up our new message 
       _messageQueue.Enqueue(message); 
       //Take out our line end character 
       fullMessage = fullMessage.Remove(0, seperator + 1); 
      } 

      //Save whatever was NOT a full message to the private variable used to store the rest 
      _theRest = fullMessage; 

      //Empty the queue of messages if there are any 
      while (this._messageQueue.Count > 0) 
      { 
       ... 
      } 
     } 
     catch (Exception e) 
     { 
      throw e; 
     } 
     // Wait for a new message 

     if (this._isClosing != true) 
      Receive(); 
    } 

} 

在此先感謝。

回答

1

我假設第二塊代碼是你的ReceiveComplete處理程序。如果是這樣,您應該查看SocketAsyncEventArgs.BytesTransferred屬性以獲取收到的字節數。

+0

謝謝!最好的簡單的錯誤! – Benny 2010-06-18 05:34:35