1:要应用socket,首先得在 .prc 文件中增加:QT += network
2:#include <QtNetwork/QUdpSocket>
3:发送方,代码例如:connect(sendButton,SIGNAL(clicked()),this,SLOT(broadcastDatagram()));
void broadcastDatagram() {
QString temp="...................";
QByteArraydatagram;
datagram.append(temp);
udpSocket->writeDatagram(datagram,QHostAddress::Broadcast,45455);
}
4:接收方,代码例如:
udpSocket=newQUdpSocket(this);
udpSocket->bind(45454,QUdpSocket::ShareAddress); connect(udpSocket,SIGNAL(readyRead()),this,SLOT(pendingDatagram()));
voidUser::pendingDatagram()
{
while(udpSocket->hasPendingDatagrams()){
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
showTextBrowser->append(datagram.data()); }
} 这样就能够接收到信息了
5: 细节未完善