标签:
一:Qt网络开发之QNetworkAccessManager (http://m.blog.csdn.net/blog/u010002704/41355917)
一个应用程序,一个QNetworkAccessManager就足够了
每一个回复QNetworkReply都需要删除,否则会出现内存泄露,根据Qt的帮助文档,在接收完数据的槽中使用deleteLater(),防止内存泄露。
UDP服户端绑定广播[喝小酒的网摘]http://blog.const.net.cn/a/11512.htm
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网络开发之QNetworkAccessManager 及 qt4使用QUdpSocket发送数据报datagrams
标签:
原文地址:http://www.cnblogs.com/zyy5/p/4503262.html