udp 20ms 接收保存端口数据 数据出现重复 缺失,怎么解决?有偿帮忙-急

udp 20ms 接收保存端口数据 数据出现重复 缺失,怎么解决?
代码如下:
radarParaCC = new MyUdpClientRadarControl(this);
radarParaCC->setUdpPort(8850);
radarParaCC->setReceType(5);
connect(radarParaCC ,SIGNAL(sendRadarDataFrom23(char*)) ,this ,SLOT(jiexiRadarDataFrom23(char*)));
udp部分:
void MyUdpClientRadarControl::InitUDPSocket()
{
//m_groupAddress = QHostAddress("239.255.43.21");//组播功能
m_udpSocket = new QUdpSocket();
//m_udpSocket->bind(QHostAddress::Any, m_UdpPort);//m_UdpPort
m_udpSocket->bind(m_UdpPort);
//udpSocket->bind(45454, QUdpSocket::ShareAddress);
//udpSocket->joinMulticastGroup(groupAddress);//加入组播

connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()), Qt::DirectConnection);
connect(this ,SIGNAL(_sendData()) ,this ,SLOT(dataSend()));

}
//接收数据
QMutex mutex;
void MyUdpClientRadarControl::dataReceived()
{
char charCheck;//校验位,反正顺序的错位
while (m_udpSocket->hasPendingDatagrams())//hasPendingDatagrams返回true时表示至少有一个数据报在等待被读取
{

    /*QReadWriteLock lock;
    lock.lockForWrite();
    lock.lockForRead();*/
    qint64 iSize = m_udpSocket->pendingDatagramSize();
    //接收的消息包:1字节消息类型+1字节结构类型+4字节消息长度+实际消息体
    //_StructNetMsg acceptmsg;

    char accepd[27000];
    //memset(accepd, 0, sizeof(accepd) / sizeof(char));//清空端口数据的缓存
    qint64 iresult = m_udpSocket->readDatagram(accepd, iSize);

    WORD aa =(unsigned char)accepd[0];
    WORD aa1 = (unsigned char)accepd[1];
    WORD aa2 =(unsigned char) accepd[2];
    WORD aa3 =(unsigned char) accepd[3];
    WORD aa4 =(unsigned char) accepd[4];
    WORD aa5 = (unsigned char)accepd[5];
    WORD aa6 =(unsigned char) accepd[6];



    if(iresult >0)
    {
        //allmsg.push_back(acceptmsg);
        //HG_ProOne_MainShow::instance()->w->dct->jiexiAdsbData(acceptmsg);
        if(m_receType == 1)
        {
            if(accepd[0] == 'M' &&accepd[1] == 'S' &&accepd[2] == 'G')
            {
                QString acceptmsg = accepd;//
                emit sendAdsbData(acceptmsg);

            }

        }
        else if(m_receType == 2)
        {
            emit sendYunTaiData(accepd);
        }
        else if(m_receType == 3)
        {
            emit sendRadarData(accepd);
        }
        else if(m_receType == 5)
        {
            /*msleep(10);*/
            //对包的校验顺序进行验证
            mutex.lock();
            emit sendRadarDataFrom23(accepd);//@question
            mutex.unlock();
        /*  msleep(2);*/
            //charCheck = accepd[2];//校验位,反正顺序的错位
            /*m_udpSocket->reset();*/
            /*qDebug() << (int)accepd[2]<<endl;*/
        }
        //int a = 0;
    }
    //将接收到的数据入数据中心层
    //memset(accepd, 0, sizeof(accepd) / sizeof(char));//清空端口数据的缓存
    //HG_ProOne_MainShow::instance()->w->dct->ddfn->DealDataForNetputinReceiveData(m_iPNumber, acceptmsg);//putinReceiveDataForXW(getonemsg);

/*  m_udpSocket->*/

    /*lock.unlock();*/
}

}

保存文件部分:
void RadarControl::jiexiRadarDataFrom23(char* corData)
{
mutex1.lock();
QStringList sections = luJCaiDada.split(QRegExp("[#]")); //把每一个块装进一个QStringList中
QString xmlpath1 = sections[0] + "time";
QFile file5("D:\2");
//char* corData1;
//corData1=corData;

if (file5.open(QIODevice::WriteOnly | QIODevice::Append))
{
    file5.open(QIODevice::WriteOnly);
    QDataStream out(&file5);

    out.writeRawData(corData, 27);

}
file5.close();
}


在@question处
c++

2个回答

udp有可能出现这个问题,你试试换成TCP看看

qq_21319399
best_RD 项目要求必须时udp呀
一年多之前 回复

udp数据会出现丢失,但是不应该出现重复啊,查查代码。对于丢失,可以用wireshark抓包看看,是丢失在网卡接收之后还是之前就丢失了。

qq_21319399
best_RD 抓包可以抓到,数据正常
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问