使用信号/槽,不要使用循环的方式,在初始化时
udpsocket = new QUdpSocket(this);
bool ret = udpsocket->bind(port, QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint);
if (!ret)
exit(0);
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
void Mainwindow::readPendingDatagrams()
{
while(udpsocket->hasPendingDatagrams())
{
.....