Qt简单应⽤socket⼴播通信1:要应⽤socket,⾸先得在 .prc ⽂件中增加:QT += network
2:#include <QtNetwork/QUdpSocket>
3:发送⽅,代码例如:connect(sendButton,SIGNAL(clicked()),this,SLOT(broadcastDatagram())); void broadcastDatagram() {
qt进程间通信 QString temp="...................";
QByteArray datagram;
datagram.append(temp);
udpSocket->writeDatagram(datagram,QHostAddress::Broadcast,45455);
}
4:接收⽅,代码例如:
udpSocket=new QUdpSocket(this);
udpSocket->bind(45454,QUdpSocket::ShareAddress);
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(pendingDatagram()));
void User::pendingDatagram()
{
while(udpSocket->hasPendingDatagrams()){
QByteArray datagram;
size(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
showTextBrowser->append(datagram.data());
}
}
这样就能够接收到信息了
5: 细节未完善
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论