3

我习惯使用同步套接字。为了处理尚未完全到达的消息,我将前 4 个字节设置为消息的预期长度。然后我会Socket.Receive(tcpRecv, 1024, SocketFlags.Peek);在不将其从缓冲区中拉出的情况下查看消息。如果所有这些都在那里,我会提取数据。如果不是,我会把它留在那里。我设计了我的协议,使任何消息都不会超过 1024 字节。

在异步套接字中,我看不到查看数据的方法。有没有办法做到这一点?有没有比偷看数据更好的方法呢?

谢谢。

-缺口

4

2 回答 2

3

您无需窥视:.NET 异步套接字允许您在不窥视的情况下实现相同类型的功能。我想你可能正在寻找这样的东西:

private void BeginReceive()
{
    if ( _clientState == EClientState.Receiving)
    {
        if (_asyncTask.BytesReceived != 0 && _asyncTask.TotalBytesReceived <= _maxPageSize)
        {
            SocketAsyncEventArgs e = new SocketAsyncEventArgs();
            e.SetBuffer(_asyncTask.ReceiveBuffer, 0, _asyncTask.ReceiveBuffer.Length);
            e.Completed += new EventHandler<SocketAsyncEventArgs>(ReceiveCallback);
            e.UserToken = _asyncTask.Host;

            bool comletedAsync = false;
            try
            {
                comletedAsync = _socket.ReceiveAsync(e);
            }
            catch (SocketException se)
            {
                Console.WriteLine("Error receiving data from: " + _asyncTask.Host);
                Console.WriteLine("SocketException: {0} Error Code: {1}", se.Message, se.NativeErrorCode);

                ChangeState(EClientState.Failed);
            }

            if (!comletedAsync)
            {
                // The call completed synchronously so invoke the callback ourselves
                ReceiveCallback(this, e);
            }
        }
        else
        {
            //Console.WriteLine("Num bytes received: " + _asyncTask.TotalBytesReceived);
            ChangeState(EClientState.ReceiveDone);
        }
    }
}

当您收到回调时,您可以安排另一个接收:

private void ReceiveCallback(object sender, SocketAsyncEventArgs args)
{
    lock (_sync) // re-entrant lock
    {
        // Fast fail: should not be receiving data if the client
        // is not in a receiving state.
        if (_clientState == EClientState.Receiving)
        {
            String host = (String)args.UserToken;

            if (_asyncTask.Host == host && args.SocketError == SocketError.Success)
            {
                try
                {
                    Encoding encoding = Encoding.ASCII;
                    _asyncTask.BytesReceived = args.BytesTransferred;
                    _asyncTask.TotalBytesReceived += _asyncTask.BytesReceived;
                    _asyncTask.DocSource += encoding.GetString(_asyncTask.ReceiveBuffer, 0, _asyncTask.BytesReceived);

                    BeginReceive();
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Error receiving data from: " + host);
                    Console.WriteLine("SocketException: {0} Error Code: {1}", e.Message, e.NativeErrorCode);

                    ChangeState(EClientState.Failed);
                }
            }
            else if (_asyncTask.Host != host)
            {
                Console.WriteLine("Warning: received a callback for {0}, but the client is currently working on {1}.",
                    host, _asyncTask.Host);
            }
            else
            {
                Console.WriteLine("Socket Error: {0} when receiving from {1}",
                   args.SocketError,
                   _asyncTask.Host);
                ChangeState(EClientState.Failed);
            }
        }
    }
}

你可以在我的博客上看到整个异步客户端:http: //codesprout.blogspot.com/2011/04/asynchronous-http-client.html

于 2011-05-06T03:59:32.997 回答
1

您的相同数据流无需窥视即可工作:

  • 安排四字节读取
  • 完成后,将其保存在缓冲区中并将其解码为长度“n”
  • 安排长度为“n”的读取 - 4
  • 完成后,将其附加到已经存在的四个字节
  • 解码你的信息

与偷看的唯一区别是,您必须在最初读取四个字节时保存它们。

于 2011-05-06T03:07:13.503 回答