1

我正在使用 C# 套接字(异步模式),需要从流中读取确切的字节数才能正确解析消息。由于我们系统中的消息非常长,因此 socket.EndRead 操作返回的字节数似乎少于 socket.BeginRead 中请求的字节数。只有在读取确切的字节数时才有机会完成 c# 套接字标记操作???正在使用 NetworkStream 吗???\

IAsyncRes ar = socket.BeginRead(1Mb byte message)
ar.Handle.Wait() // --> will signal ONLY when 1Mb us read !!!!
socket.EndRead() 

升级版:

我已经用 c# 迭代器解决了它。(此处未显示运行 irator 循环并负责执行 MoveNext 的线程)

protected IEnumerator<IAsyncResult> EnumReceiveExact(byte[] array)
        {


            int offset = 0;

            while (offset < array.Length)
            {
                SocketError err = SocketError.Success;
                IAsyncResult ar = _socket.BeginReceive(array, offset, array.Length - offset, SocketFlags.None, out err, null, null);
                Console.WriteLine("{0}:err:{1}", this, err);
                if (err != SocketError.Success)
                {
                    _socket.Close();
                    throw new Exception("Error " + err);
                }

                yield return ar;
                while (!ar.IsCompleted)
                {
                    yield return ar;
                }

                offset += _socket.EndReceive(ar, out err);
                if (err != SocketError.Success)
                {
                    _socket.Close();
                    throw new Exception("Error " + err);
                }

            }

        }
4

1 回答 1

0

与枚举器的良好调用,尽管我希望您的外部代码不仅仅是在 asyncresults 上调用 WaitOne,因为这会阻塞正在运行的线程。

如果您喜欢这种异步编码风格,请查看 NuGet 上的 Wintellect AsyncEnumerator - 它也使用迭代器,使代码非常高效,并添加了更简单的方法来处理取消和异常,同时确保所有 APM 结束方法都被调用。

我之前通过以下方式解决了确切的读取问题:

1) 为在套接字上发送的数据添加长度前缀
2) 使用以下方法定义一个在 Socket 上工作的辅助类:

public IAsyncResult BeginRead(AsyncCallback callback)
// Calculate whether to read length header or remaining payload bytes
// Issue socket recieve and return its IAsyncResult

public MemoryStream EndRead(IAsyncResult result)
// Process socket read, if payload read completely return as a memorystream
// If length header has been recieved make sure buffer is big enough
// If 0 bytes recieved, throw SocketException(10057) as conn is closed

public IAsyncResult BeginSend(AsyncCallback callback, MemoryStream data)
// Issue sends for the length header or payload (data.GetBuffer()) on the first call

public Boolean EndSend(IAsyncResult result)
// Process bytes sent, return true if payload sent completely.
// If 0 bytes sent, throw SocketException(10057)

所以它仍然需要在循环中调用,但看起来像一个正常的异步操作,例如通过 asyncenumerator 调用(没有取消检查和异常处理):

do
{
    socketHelper.BeginSend(ae.End(1, ar => socketHelper.EndSend(ar)), sendData);
    yield return 1;
    doneSend = socketHelper.EndSend(ae.DequeueAsyncResult());
} 
while (!doneSend);
于 2012-05-31T10:07:51.360 回答