Home > Mobile >  QUdpSocket. WriteDatagram subcontract send failure, bosses, please help take a look
QUdpSocket. WriteDatagram subcontract send failure, bosses, please help take a look

Time:09-20

Question is like this, at first I use this code to the subcontract and send data, but at the end of the bag, in front of all return 1,
Plus the time interval between later found the subcontract send can improve
But this time interval and I grasp is not good, still occasional failure is too short, too long is can all send success, but I'm not sure whether related to running environment, will not change machine also not line,
And it is not in line with my business requirements, my project requires every 15 milliseconds to send a packet, if the time interval between each packet to the subcontract, that can't be 15 millisecond send a
My subcontract code below, bosses can help me have a look at how to optimize, or give some ideas, grateful,

 
# define UDP_MAX_SIZE 65507

Struct ImageFrameHead {
Int funCode;//! Function code
Unsigned int uTransFrameHdrSize;//! Sizeof (WIFI_FRAME_HEADER)
Unsigned int uTransFrameSize;//! Sizeof (WIFI_FRAME_HEADER) + Data Size

//data frame variable
Unsigned int uDataFrameSize;//the total size of the data frame
Unsigned int uDataFrameTotal;//a frame of data is divided into the number of transmission frame
Unsigned int uDataFrameCurr;//the data frame of the current frame number
Unsigned int uDataInFrameOffset;//data frames in the whole frame of offset
};


 
Void MainWidget: : sendPackData (QByteArray buffer, QString addr, quint16 port)
{
Char * m_sendBuf=new char (1024 * 1000),
Int size=buffer. The size ();
int num=0;
Int count=0;
Int endSize=size % UDP_MAX_SIZE;
If (endSize==0) {
Num=the size/UDP_MAX_SIZE;
}
The else {
Num=the size/UDP_MAX_SIZE + 1;
}

While (the count & lt; Num) {
Memset (m_sendBuf, 0, 1024 * 1000);
ImageFrameHead mes.
Mes. FunCode=24;
Mes. UTransFrameHdrSize=sizeof (ImageFrameHead);
If ((count + 1)! Num)={
Mes. UTransFrameSize=UDP_MAX_SIZE;
}
The else {
Mes. UTransFrameSize=endSize;
}
Mes. UDataFrameSize=size;
Mes. UDataFrameTotal=num;
Mes. UDataFrameCurr=count + 1;
Mes. UDataInFrameOffset=count * (1024 * 1000 - sizeof (ImageFrameHead));
Memcpy (m_sendBuf, (char *) & amp; Mes, sizeof (ImageFrameHead));

Qint64 result=receiver - & gt; WriteDatagram (m_sendBuf, mes. UTransFrameSize + mes. UTransFrameHdrSize,
QHostAddress (addr), port);

QTime dieTime=QTime: : currentTime () addMSecs (15);
While (QTime: : currentTime () & lt; DieTime)
QCoreApplication: : processEvents (QEventLoop: : AllEvents, 100);

count++;
}
QDebug (" ok ");
}
  •  Tags:  
  • Qt