How to read complete data in QTcpSocket?

前端 未结 4 1558
灰色年华
灰色年华 2020-12-18 02:33

Now the server (implemented with java) will send some stream data to me, my code is like below:

connect(socket, SIGNAL(readyRead()), this, SLOT(read_from_ser         


        
4条回答
  •  轮回少年
    2020-12-18 03:20

    you can use a buffer field to hold the unfinished data temporarily and handle packets as they complete:

    {
        while (socket->bytesAvailable())
        {
            buffer.append(socket->readAll());
            int packetSize = getPacketSize(buffer);
            while(packetSize>0)
            {
                handlePacket(buffer.left(packetSize);
                buffer.remove(0,packetSize);
                packetSize = getPacketSize(buffer);
            }
        }
    }
    

提交回复
热议问题