C# Socket ReceiveAsync

本文关键字:ReceiveAsync Socket | 更新日期: 2023-09-27 18:09:22

我习惯于同步套接字,并且有一些令人头痛的事情要达到我现在的地步,特别是Socket.Receive(..)并不总是接收所有字节

这是我的代码,我曾经使用

    public byte[] Receive(int size)
    {
        var buffer = new byte[size];
        var r = 0;
        do
        {
            // ReSharper disable once InconsistentlySynchronizedField
            var c = _clientSocket.Receive(buffer, r, size - r, SocketFlags.None);
            if (c == 0)
            {
                throw new SocketExtendedException();
            }
            r += c;
        } while (r != buffer.Length);
        return buffer;
    }

现在我开始在Windows Phone中使用套接字,但.Receive(..)不可用,我设法让Socket.ReceiveAsync(..)工作,但我担心(到目前为止没有发生问题)这是我的新代码,我还没有实现检查是否已收到所有字节,我也不知道我是否必须使用以下代码

    private byte[] ReadBySize(int size = 4)
    {
        var readEvent = new AutoResetEvent(false);
        var buffer = new byte[size];
        var recieveArgs = new SocketAsyncEventArgs()
        {
            UserToken = readEvent
        };
        recieveArgs.SetBuffer(buffer, 0, size);
        recieveArgs.Completed += recieveArgs_Completed;
        _connecter.ReceiveAsync(recieveArgs);
        readEvent.WaitOne();
        if (recieveArgs.BytesTransferred == 0)
        {
            if (recieveArgs.SocketError != SocketError.Success)
                throw new SocketException((int)recieveArgs.SocketError);
            throw new CommunicationException();
        }
        return buffer;
    }
    void recieveArgs_Completed(object sender, SocketAsyncEventArgs e)
    {
        var are = (AutoResetEvent)e.UserToken;
        are.Set();
    }

这是我第一次使用ReceiveAsync,有人能指出我可能做错了什么或需要改变

C# Socket ReceiveAsync

好的,我取了一个大的缓冲区,并分批发送它,中间有一个睡眠间隔,以复制'未接收到所有字节',所以我上面的代码没有接收到所有字节。对于那些谁也使用ReceiveAsync(..)这里是我的代码,工作

    private byte[] ReadBySize(int size = 4)
    {
        var readEvent = new AutoResetEvent(false);
        var buffer = new byte[size]; //Receive buffer
        var totalRecieved = 0;
        do
        {
            var recieveArgs = new SocketAsyncEventArgs()
            {
                UserToken = readEvent
            };
            recieveArgs.SetBuffer(buffer, totalRecieved, size - totalRecieved);//Receive bytes from x to total - x, x is the number of bytes already recieved
            recieveArgs.Completed += recieveArgs_Completed;
            _connecter.ReceiveAsync(recieveArgs);
            readEvent.WaitOne();//Wait for recieve
            if (recieveArgs.BytesTransferred == 0)//If now bytes are recieved then there is an error
            {
                if (recieveArgs.SocketError != SocketError.Success)
                    throw new ReadException(ReadExceptionCode.UnexpectedDisconnect,"Unexpected Disconnect");
                throw new ReadException(ReadExceptionCode.DisconnectGracefully);
            }
            totalRecieved += recieveArgs.BytesTransferred;
        } while (totalRecieved != size);//Check if all bytes has been received
        return buffer;
    }
    void recieveArgs_Completed(object sender, SocketAsyncEventArgs e)
    {
        var are = (AutoResetEvent)e.UserToken;
        are.Set();
    }

我使用Socket应用程序的方式是发送一个由一些变量组成的Buffer

[0] -> 0,1,2 0 is keep alive, 1 means there are data, 2 means a type off error occured
[1,2,3,4] size of the actual buffer I am sending
[x(size of 1,2,3,4)] the actual 'Serialized' data buffer

您可以创建这样的套接字扩展:

public static Task<int> ReceiveAsync(this Socket socket,
    byte[] buffer, int offset, int size, SocketFlags socketFlags)
{
    if (socket == null) throw new ArgumentNullException(nameof(socket));
    var tcs = new TaskCompletionSource<int>();
    socket.BeginReceive(buffer, offset, size, socketFlags, ar =>
    {
        try { tcs.TrySetResult(socket.EndReceive(ar)); }
        catch (Exception e) { tcs.TrySetException(e); }
    }, state: null);
    return tcs.Task;
}

然后是一个方法来读取你想要的大小,像这样:

public static async Task<byte[]> ReadFixed(Socket socket, int bufferSize)
{
    byte[] ret = new byte[bufferSize];
    for (int read = 0; read < bufferSize; read += await socket.ReceiveAsync(ret, read, ret.Length - read, SocketFlags.None)) ;
    return ret;
}