【问题标题】:Silverlight Socket Constantly Returns With Empty BufferSilverlight 套接字不断返回空缓冲区
【发布时间】:2010-06-18 05:16:38
【问题描述】:

我正在使用 Silverlight 与我开发的代理应用程序进行交互,但是在没有代理向 Silverlight 应用程序发送消息的情况下,它使用空缓冲区 ('\0's) 执行接收完成处理程序。有什么我做错了吗?它导致严重内存泄漏。

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();
    }

}

提前致谢。

【问题讨论】:

    标签: silverlight sockets


    【解决方案1】:

    我假设第二个代码块是您的ReceiveComplete 处理程序。如果是这样,您应该查看SocketAsyncEventArgs.BytesTransferred 属性以获取接收到的字节数。

    【讨论】:

    • 谢谢!!最简单的错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2012-10-18
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多