码迷,mamicode.com
首页 > 其他好文 > 详细

Qt 的udpSocket通信

时间:2015-06-25 13:40:02      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:

udpSocket = new QUdpSocket(this); 
QByteArray datagram = "blog.const.net.cn"; 
udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454); 
udpSocket->bind(QHostAddress("192.168.1.104"), 45454); 

UDP客户端广播
QUdpSocket *udpsocket1 = new QUdpSocket(this);
QByteArray datagram = "http://blog.const.net.cn"; udpsocket1->writeDatagram(datagram.data(),datagram.size(), QHostAddress::Broadcast,ui->spinBox->text().toInt());


UDP客户端发送到指定IP
QUdpSocket *udpsocket1 = new QUdpSocket(this);
QByteArray datagram = "http://blog.const.net.cn"; udpsocket1->writeDatagram(datagram.data(),datagram.size(), QHostAddress("192.168.1.104"),ui->spinBox->text().toInt());

收数据
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); 
while (udpsocket->hasPendingDatagrams()) 

QByteArray datagram; 
datagram.resize(udpsocket->pendingDatagramSize()); 
QHostAddress sender; 
quint16 senderPort; 
udpsocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); 
qDebug()<<datagram.data()<<datagram.size()<<senderPort<<sender.toString(); //processTheDatagram(datagram);
}

 
分类: Qt

Qt 的udpSocket通信

标签:

原文地址:http://www.cnblogs.com/IamQtCreator/p/4599580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!