c#套接字接收全部数据

本文关键字:全部 数据 套接字 | 更新日期: 2023-09-27 18:14:09

我仍然停留在Tcp套接字,我目前正在一个服务器/客户端项目上工作我试图接收已由客户端发送的完整数据,但我没有得到这一切。
经过一番研究,我知道one Send doesnt surely mean one receive,所以我需要继续读取,直到缓冲区大小达到发送的数据大小。

    public void Connect()
    {
        try
        {
            _ThisSocket.Connect(this._HOST);
        }catch {}
        new Thread(() =>
        {
            while (_ThisSocket.Connected)
            {
                _ThisSocket.Receive(BufferSize, 0, 4, SocketFlags.None);
                int Size = BitConverter.ToInt32(BufferSize, 0);
                while (Size > 0)
                {
                    if (Size < _ThisSocket.ReceiveBufferSize)
                    {
                        _Buffer = new byte[Size];
                    }
                    else
                    {
                        _Buffer = new byte[_ThisSocket.ReceiveBufferSize];
                    }
                    _ThisSocket.BeginReceive(_Buffer, 0, _Buffer.Length, SocketFlags.None, Receive, _Buffer.Length);
                }
            }
        }
        ).Start();

    }
private void Receive(IAsyncResult AR)
{
    int Size = (int)AR.AsyncState;
    Byte[] buff = new Byte[Size];
    Array.Copy(_Buffer, buff, Size);
    String Data = Encoding.Unicode.GetString(buff);
    String Cmd = Crypter.Decrypt(Data);
    Switch(Cmd);
}

c#套接字接收全部数据

这是我使用Socket类的一个例子。接收大的例子文件和套接字上的所有数据:

     private byte[] ReceiveLargeFile(Socket socket, int lenght)
     {
        // send first the length of total bytes of the data to server
        // create byte array with the length that you've send to the server.
        byte[] data = new byte[lenght];  

        int size = lenght; // lenght to reveive
        var total = 0; // total bytes to received
        var dataleft = size; // bytes that havend been received 
         // 1. check if the total bytes that are received < than the size you've send before to the server.
         // 2. if true read the bytes that have not been receive jet
         while (total < size) 
        {
            // receive bytes in byte array data[]
            // from position of total received and if the case data that havend been received.
            var recv = socket.Receive(data, total, dataleft, SocketFlags.None);
            if (recv == 0) // if received data = 0 than stop reseaving
            {
                data = null;
                break;
            }
            total += recv;  // total bytes read + bytes that are received
            dataleft -= recv; // bytes that havend been received
        }
        return data; // return byte array and do what you have to do whith the bytes.
    }

不需要设置缓冲区大小,下面的方法适合响应大小:

public static byte[] ReceiveAll(this Socket socket)
{
    var buffer = new List<byte>();
    while (socket.Available > 0)
    {
        var currByte = new Byte[1];
        var byteCounter = socket.Receive(currByte, currByte.Length, SocketFlags.None);
        if (byteCounter.Equals(1))
        {
            buffer.Add(currByte[0]);
        }
    }
    return buffer.ToArray();
}