简体   繁体   English

QT4到QT5的网络问题-UDP

[英]QT4 to QT5 networking issue - UDP

I'm pretty new to Qt. 我对Qt很陌生。 I inherited a QtCreator project built using Qt 4.8.1 and have been attempting to rebuild it with Qt 5.4.2 to add functionality. 我继承了一个使用Qt 4.8.1构建的QtCreator项目,并一直在尝试使用Qt 5.4.2对其进行重建以添加功能。 I have successfully rebuilt the project under windows 7 (Mingw) and linux with Qt5. 我已经使用Qt5在Windows 7(Mingw)和Linux下成功重建了该项目。

Everything works well until I start the connected devices and receive the UDP stream - where the old project (on the same machine as new code) will happily receive and store data, the new project chokes very quickly at some arbitrary amount of data and will not accept new data. 一切正常,直到我启动连接的设备并接收UDP流为止-旧项目(与新代码在同一台机器上)将愉快地接收和存储数据,新项目在任意数量的数据下很快停止运行,并且不会接受新数据。

EDIT packet size - all incoming small packets are received. 编辑数据包大小-接收所有传入的小数据包。 There is typically some control and ack packets of small size ( ~100 bytes ) that work fine. 通常,有些较小的控制和ack数据包(约100字节)可以正常工作。 The problem is once data acquisition starts; 问题是一旦开始数据采集; the incoming data packet payload is 1441 bytes and this will choke rapidly. 传入的数据包有效载荷为1441字节,这将迅速阻塞。

#include <QThread>
#include <QUdpSocket>
#ifdef linux
#include <sys/socket.h>
#elif _WIN32
#include <winsock2.h>
#else
#error Platform not supported
#end

// etc

void ReceiveSocket::run()
{
    udpReceiveSocket = new QUdpSocket();
    connect( udpReceiveSocket, SIGNAL( readyRead() ), this, SLOT( readPendingDatagrams() ) );
    udpReceiveSocket->moveToThread( this );
    exec();
}

// etc

void ReceiveSocket::readPendingDatagrams()
{
    QByteArray * data;
    while( udpReceiveSocket->hasPendingDatagrams() ) {
        data = new QByteArray;
        data->resize( udpReceiveSocket->pendingDatagramSize() );
        QHostAddress sender;
        quint16 senderPort;
        udpReceiveSocket->readDatagram( data->data(),
                sdata->size(),&sender, &senderPort );
        emit processDatagrams( data );
    }
}  

Has anyone experience this issue migrating to Qt5? 有没有人在迁移到Qt5时遇到此问题? Is there an obvious cause to an arbitrary stoppage? 是否有明显的原因导致任意停止? I have added a debug counter in the processDatagrams method to show the running count of datagrams processed and it varies but is around a few hundred before it quits. 我在processDatagrams方法中添加了一个调试计数器,以显示已处理数据报的运行计数,该计数有所变化,但在退出前大约有几百个。

EDIT I added a counter to the while loop and printed using qDebug. 编辑我在while循环中添加了一个计数器,并使用qDebug打印。 This would obviously slow things down and the data does acquire at about 1/10th the actual data rate and it will continue indefinitely. 显然,这会减慢速度,并且数据确实会以实际数据速率的1/10左右的速度获取,并且它将无限期地继续。 This may hint at the problem. 这可能暗示了问题。

void ReceiveSocket::readPendingDatagrams()
{
    volatile int i = 0;
    QByteArray * data;
    while( udpReceiveSocket->hasPendingDatagrams() ) {
        qDebug()<<i;
        i++;
        data = new QByteArray;
        data->resize( udpReceiveSocket->pendingDatagramSize() );
        QHostAddress sender;
        quint16 senderPort;
        udpReceiveSocket->readDatagram( data->data(),
                sdata->size(),&sender, &senderPort );
        emit processDatagrams( data );
    }
}  

For now, I'm going to use Qt4 libraries until I have a better opportunity to debug behavior with Qt5 现在,我将使用Qt4库,直到我有更好的机会使用Qt5调试行为为止

First of all, no need for new QByteArray 首先,不需要new QByteArray

void ReceiveSocket::readPendingDatagrams()
{
    while( udpReceiveSocket->hasPendingDatagrams() ) {
        int len = udpReceiveSocket->pendingDatagramSize();
        QByteArray data;
        data->resize(len);
        udpReceiveSocket->readDatagram(data.data());
        emit processDatagrams( data );
    }
}  

so this should fix a possible error with data (not initialized) and sdata (whatever this is). I discarded the variables 因此,这应该解决data (未初始化) and sdata (whatever this is). I discarded the variables )的可能错误(whatever this is). I discarded the variables (whatever this is). I discarded the variables sender and senderPort` because they were not needed in your code and you can add them if needed later (whatever this is). I discarded the variables sender and senderPort`,因为在您的代码中不需要它们,以后可以添加它们

Note that UDP datagrams are kind of restricted in size. 请注意,UDP数据报的大小受到限制。 they will get split up at a certain length by the socket itself when it is longer than the packet size. 当套接字长度超过数据包大小时,它们将被套接字本身分成一定的长度。 I usually use packets with size of <512, should be the minimum default length of UDP data packets by routers and such 我通常使用大小小于512的数据包,应该是路由器等发出的UDP数据包的最小默认长度

This bug appeared in Qt 5.4.2. 此错误出现在Qt 5.4.2中。 Either downgrade to 5.4.1 or enclose the event handling code in a loop as suggested by Zaiborg. 降级到5.4.1或按照Zaiborg的建议将事件处理代码括在循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM