c#套接字接收完整数据

时间:2016-10-21 17:46:48

标签: c# sockets

我仍然坚持使用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#套接字编程的新手,我原谅任何错误!

2 个答案:

答案 0 :(得分:0)

  

这是我使用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.
    }

答案 1 :(得分:0)

没有必要设置缓冲区大小,这种方法适合响应大小:

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