我这里使用了数组进行数据的接收,还可以用ByteArray类型进行数据的接收。 这种数组接收的方式有一点问题,如果数据大小超过了数组的大小就会有错误产生,程序就会崩溃,所以大家尽量不要使用这种方式。 下次再更新另一种方式吧。
//revthread.cpp 该文件的udp设置 及接收函数
void MainWindow::UdpInit(void)
{
glv_UDPRevSocket =new QUdpSocket(this);
connect(glv_UDPRevSocket,SIGNAL(readyRead()),this,SLOT(datareceive()));
bool result= glv_UDPRevSocket->bind(port,QUdpSocket::ShareAddress);
if(!result)
{
qDebug()<<"udp init chucuo";
}
}
//报文接收缓冲区
char glv_recebuffer[200];
void MainWindow::datareceive()
{
if(recflag!=1)
recflag=1;
while (glv_UDPRevSocket->hasPendingDatagrams())
{
qDebug()<pendingDatagramSize();
glv_UDPRevSocket->readDatagram(glv_recebuffer,\
glv_UDPRevSocket->pendingDatagramSize());
}
}