0

我正在编写一个简单的网络应用程序。客户端发送到服务器消息服务器在 QTextEdit 中打印并响应客户端。我正在使用 QTcpServer 和 QTcpSocket。有一个我无法解决的问题。接收数据是 quint16 + QTime + QString,它作为 QByteArrey 发送。我使用 quint16 接收数据块的大小。并且由于某种原因,当客户端发送到服务器时

next block size: 16 (quint16 value)
block size: 18 

服务器获取:

next block size: 30073  (quint16 value)
block size: 18 

如您所见,由于某种原因,服务器从 QDataStrem 获取了错误的变量值,并且始终为 30073。我不明白为什么?

void Widget::slotSendToServer()
{
    logTextEdit->append("slotSendToServer()");
    QByteArray arrBlock;
    QDataStream serverSendStream(&arrBlock, QIODevice::ReadWrite);
    QString messageStr = messageLineEdit->text();

    serverSendStream << quint16(0) << QTime::currentTime()
                     << messageStr;

    serverSendStream.device()->seek(0);
    serverSendStream << (quint16)(arrBlock.size() - sizeof(quint16));

    qDebug() << "next_block_size:"
             <<(quint16)(arrBlock.size() - sizeof(quint16)) 
             << endl
             << "full size of Byte arrey:" << arrBlock.size();

    tcpSocket->write(arrBlock);
    messageLineEdit->clear();
}



void Widget::slotReadClient()
{
    logTextEdit->append("slotReadClient()");
    QTcpSocket *tcpSocket = (QTcpSocket*)sender();
    QDataStream clientReadStream(tcpSocket);

    while(true)
    {
        if (!next_block_size)
        {
            if (tcpSocket->bytesAvailable() < sizeof(quint16))
            {
                break;
            }
            clientReadStream >> next_block_size;
        }

        if (tcpSocket->bytesAvailable() < next_block_size)
        {
            break;
        }
        QTime   time;
        QString messageTextStr;
        clientReadStream >> time >> messageTextStr;

        QString messageCompleteStr =
                time.toString() + " " + "Client has sent - " 
                                + messageTextStr;
        logTextEdit->append("Message received: ");
        logTextEdit->append(messageCompleteStr);

        next_block_size = 0;

        sendToClient(tcpSocket,
                     "Server Response: Received \"" 
                      + messageTextStr + "\"");
    }
}
4

2 回答 2

2

您应该确保next_block_size每次连接套接字时将变量初始化为 0。

如果你不重用同一个QTcpSocket对象,这可以在你的Widget类构造函数中完成,或者如果你这样做,在连接到信号的插槽中完成connected()

于 2013-06-30T12:47:34.090 回答
-1

我不知道你为什么做的这么复杂。这应该有效:

void Widget::slotSendToServer()
{
    logTextEdit->append("slotSendToServer()");
    QByteArray arrBlock;
    QDataStream serverSendStream(tcpSocket);

    serverSendStream << QTime::currentTime()
                     << messageLineEdit->text();
    messageLineEdit->clear();
}

void Widget::slotReadClient()
{
    logTextEdit->append("slotReadClient()");
    QTcpSocket *tcpSocket = (QTcpSocket*)sender();
    QDataStream clientReadStream(tcpSocket);

    QTime time;
    QString message;

    clientReadStream >> time >> message;

    emit YourSignal(time, message);
}

您不必担心大小,QDataStream 会跟踪它,您只需保持与写入时相同的读取顺序,因此您的缓冲区arrBlock浪费了代码。


根据文档,当阅读时并非所有数据都可用时,我的代码应该阻塞,这只是缺点。对于像日期和一些字符串这样的小数据,它永远不会发生。

关于你的代码,你肯定用 while 循环搞乱了你的读者,所以这里是没有这个循环的更正。

void Widget::slotReadClient()
{
    logTextEdit->append("slotReadClient()");
    QTcpSocket *tcpSocket = (QTcpSocket*)sender();
    QDataStream clientReadStream(tcpSocket);

    if (next_block_size==0) {
        // need to read data size
        if (tcpSocket->bytesAvailable() < sizeof(quint16))
            return; // wait for next signal
        // next_block_size must be type of qint16
        clientReadStream >> next_block_size;
    }
    if (tcpSocket->bytesAvailable() < next_block_size) {
        // not enought data to complete read immediately
        return;  // wait for next signal
    }
    QTime time;
    QString messageTextStr;
    clientReadStream >> time >> messageTextStr;
    QString messageCompleteStr =
                time.toString() + " " + "Client has sent - " 
                                + messageTextStr;
    logTextEdit->append("Message received: ");
    logTextEdit->append(messageCompleteStr);

    // mark that data read has been finished
    next_block_size = 0;
}
于 2013-06-29T20:30:23.560 回答