RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 552419
Accepted
Bearded Beaver
Bearded Beaver
Asked:2020-08-06 01:54:55 +0000 UTC2020-08-06 01:54:55 +0000 UTC 2020-08-06 01:54:55 +0000 UTC

QTcpSocket 拍数据

  • 772

我正在编写一个客户端-服务器应用程序的原型,在战斗模式下,应用程序必须每秒传输大约 60 次 QString、QVariant 形式的 ~ 200 个值。

问题是,以这种在客户端发送的速度,我成功地收到了大约 50 个数据包中的一个,在其他情况下,从流中获取的变量结果是无效的 ( QVariant::invalid)

服务器代码:

void TDummyWorker::initialize()
{
    timer = new QTimer(this);
    timer->setInterval(10); 
    connect(timer,SIGNAL(timeout()),this,SLOT(foo()));   //будем посылать данные с заданным интервалом
    timer->start();
    server = new QTcpServer(this);
    connect(server,&QTcpServer::newConnection,this,&TDummyWorker::somebodyConnected);
    server->listen(QHostAddress::Any, 33333);
}

void TDummyWorker::foo()
{
    QVector <TNamedVariable> vars;
    static int n=0;
    int ndata = 200;
    for (int i=0; i<ndata; i++)
    {
        //формируем фейковые данные
        vars.append(TNamedVariable("variable_number_" % QString::number(i), (i%2 != 0 ? float((i+n)*0.01) : int(i+n)) ));
    }
    for (QMap<int ,QTcpSocket *>::iterator it = clients.begin(); it!=clients.end(); ++it)
    {
        sendToClient(*it, vars);  //посылаем данные всем подключенным клиентам
    }
    n++;
    if (n>1000000) n=0;
}

void TDummyWorker::sendToClient(QTcpSocket *socket, QVector <TNamedVariable> &vars)
{
    QByteArray arr;
    QDataStream out(&arr, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_5_2);
    int n=vars.size();
    out << quint16(0);
    for(int i=0; i<n;i++)
    {
        out << vars[i];
    }
    out.device()->seek(0);
    out << quint16(arr.size() - sizeof(quint16));
    sendMessage(vars[0].value.toString() % " " % QString::number(arr.size() - sizeof(quint16)));
    socket->write(arr);
}

这就是我在客户端获取数据的方式:

void TClient::recieveData()
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_5_2);
    QVector <TNamedVariable> vars;
    quint16 nextBlockSize=0;
    while (true)
    {
        if (nextBlockSize==0)
        {
            if (socket->bytesAvailable() < sizeof(quint16)) 
            {
                break;
            }
            stream >> nextBlockSize;
        }
        if (socket->bytesAvailable() < nextBlockSize) 
            break;
        TNamedVariable buf;
        while (!stream.atEnd())
        {
            stream >> buf;
            buf.debug();
            vars.append(buf);
        }

        nextBlockSize = 0;
    }
    emit sendData(vars);
}

如果我们每秒发送 1-2 个变量,那么客户端会正常接受所有内容,我增加传输频率和向量中变量的数量越多,我得到的损坏数据就越多。我怀疑这个错误是微不足道的,因为代码几乎完全来自教科书示例。

所有编辑后正确工作的代码:

void TClient::recieveData()
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_5_2);
    static int nbad = 0;
    TNamedVariable buf;
    static quint16 nextBlockSize=0;
    while (true)
    {
        if (nextBlockSize == 0)
        {
            if (socket->bytesAvailable() < sizeof(quint16))
            {
                break;
            }
            stream >> nextBlockSize;
        }
        if (socket->bytesAvailable() < nextBlockSize)
        {
            break;
        }
        while (!stream.atEnd())
        {
            stream >> buf;
            if (!buf.value.isValid())
            {
                qDebug()<<"op :("<<++nbad;
            }
            if (buf.name == "eod")
                break;
            vars.append(buf);
        }
        if (buf.name == "eod")
        {
            emit sendData(vars);
            vars.clear();
        }
        nextBlockSize = 0;
    }

}
qt
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    user177227
    2020-08-06T03:00:22Z2020-08-06T03:00:22Z

    问题最有可能出现在 中nextBlockSize,或者更确切地说,变量存在于本地,而不是在对 的调用之间保留其值recieveData()。

    收到时,网络数据包可能包含 的值nextBlockSize,以及消息正文中的一些数据。然后检查的代码块nextBlockSize将正常工作,但条件:

    if (socket->bytesAvailable() < nextBlockSize) break;
    

    ... 将导致执行上下文退出该方法。然后nextBlockSize它将失去它的价值,并且recieveData()使用新上传的数据的新呼叫将无法正常工作,因为。来自流的消息的大小先前已被读取。

    添加

    另一个问题可能出现while (!stream.atEnd()) {}在recieveData(). 接收到的消息,除了其内容之外,还可能包含下一条消息的部分数据。在这种情况下,指定的周期将无法正常工作。

    void SocketManager::onReadyRead() {
        QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
        if(socket == Q_NULLPTR) return;
    
        QDataStream stream(socket);
        stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    
        forever {
            quint32 receive_data_size
                = socket->property("receive-data-size").toUInt();
    
            if(receive_data_size == 0) {
                if(socket->bytesAvailable() < (qint64)sizeof(quint32))
                    return;
    
                stream >> receive_data_size;
    
                socket->setProperty("receive-data-size", receive_data_size);
            }
    
            if(socket->bytesAvailable() < receive_data_size) return;
    
            socket->setProperty("receive-data-size", 0);
    
            QVariant var;
            stream >> var;
    
            emit dataReceived(var);
        }
    }
    

    在所示示例中,只有一个循环forever,即在读取当前消息数据后从头开始重新迭代(包括检查消息大小)。

    receive_data_size是消息大小。在示例中,它存储在套接字对象的属性中。如果多个套接字正在调用同一个接收槽,这将很有用。

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5