一.Server端
头文件:
#include <QUdpSocket>
变量:
QUdpSocket *udpSocket;
源码端(CPP)
#include <QHostAddress>
关键方法:
if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length()) {
}
二.客户端
头文件:
#include <QUdpSocket>
变量:
QUdpSocket *udpSocket;
源码端(CPP)
#include <QMessageBox>
#include <QHostAddress>
关键方法:
bool result=udpSocket->bind(port);//判断是否绑定成功
if(!result)
{
QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
return;
}
while(udpSocket->hasPendingDatagrams())//循环接受信息
{
QByteArray datagram;
datagram.resize(udpSocket->pe