Qt文档阅读笔记-Multicast Receiver Example与Multicast Sender Example解析
目錄
?
Multicast Receiver Example
Multicast Sender Example
Multicast Receiver Example
多播接收實例!
要注意幾點:
1.這個組播QHostAddress最好是填寫D類IP,填ABC類在某些情況下可以的,因為路由器默認D類是組播地址,所以為了避免不必要的麻煩,最好是D類!
2.QHostAddress配的D類地址,與真實IP地址一般是不一樣的;
3.調用joinMulticastGroup()函數把組播地址加入到socket中,使得數據包能夠成功接收!
4.bind()的時候,記得QHostAddress::AnyIPv4讓其接收所有的組播包!
5.每一組組播源最好設置同一個QHostAddress(可以設成不一樣,但可能會不通,這里關鍵看路由器,交換機的配置狀態)!
?
源碼如下:
receiver.h
#ifndef RECEIVER_H #define RECEIVER_H#include <QDialog> #include <QHostAddress>QT_BEGIN_NAMESPACE class QLabel; class QPushButton; class QUdpSocket; QT_END_NAMESPACEclass Receiver : public QDialog {Q_OBJECTpublic:Receiver(QWidget *parent = 0);private slots:void processPendingDatagrams();private:QLabel *statusLabel;QPushButton *quitButton;QUdpSocket *udpSocket;QHostAddress groupAddress; };#endifmain.cpp
#include <QApplication>#include "receiver.h"int main(int argc, char *argv[]) {QApplication app(argc, argv);Receiver receiver;receiver.show();return app.exec(); }receiver.cpp
#include <QtWidgets> #include <QtNetwork>#include "receiver.h"Receiver::Receiver(QWidget *parent): QDialog(parent) {groupAddress = QHostAddress("239.255.43.21");statusLabel = new QLabel(tr("Listening for multicasted messages"));quitButton = new QPushButton(tr("&Quit"));udpSocket = new QUdpSocket(this);udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);udpSocket->joinMulticastGroup(groupAddress);connect(udpSocket, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));QHBoxLayout *buttonLayout = new QHBoxLayout;buttonLayout->addStretch(1);buttonLayout->addWidget(quitButton);buttonLayout->addStretch(1);QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addWidget(statusLabel);mainLayout->addLayout(buttonLayout);setLayout(mainLayout);setWindowTitle(tr("Multicast Receiver")); }void Receiver::processPendingDatagrams() {while (udpSocket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());udpSocket->readDatagram(datagram.data(), datagram.size());statusLabel->setText(tr("Received datagram: \"%1\"").arg(datagram.data()));} }Multicast Sender Example
組播發送案例
注意點:
1.要設置TTL(Time To Live),這個是只在三層才有的,每經過一個路由器TTL減1,廣播一般是二層的,所以沒有TTL這個概念,TTL這個的好處,可以避免網絡風暴等,讓路由器開銷過大!
2.setSocketOption()第一個參數是告訴socket發送的是什么包,比如廣播包!
?
源碼如下:
sender.h
#ifndef SENDER_H #define SENDER_H#include <QDialog> #include <QHostAddress>QT_BEGIN_NAMESPACE class QDialogButtonBox; class QLabel; class QPushButton; class QTimer; class QUdpSocket; class QSpinBox; QT_END_NAMESPACEclass Sender : public QDialog {Q_OBJECTpublic:Sender(QWidget *parent = 0);private slots:void ttlChanged(int newTtl);void startSending();void sendDatagram();private:QLabel *statusLabel;QLabel *ttlLabel;QSpinBox *ttlSpinBox;QPushButton *startButton;QPushButton *quitButton;QDialogButtonBox *buttonBox;QUdpSocket *udpSocket;QTimer *timer;QHostAddress groupAddress;int messageNo; };#endifmain.cpp
#include <QApplication>#include "sender.h"int main(int argc, char *argv[]) {QApplication app(argc, argv);Sender sender;sender.show();return app.exec(); }sender.cpp
#include <QtWidgets> #include <QtNetwork>#include "sender.h"Sender::Sender(QWidget *parent): QDialog(parent) {groupAddress = QHostAddress("239.255.43.21");statusLabel = new QLabel(tr("Ready to multicast datagrams to group %1 on port 45454").arg(groupAddress.toString()));ttlLabel = new QLabel(tr("TTL for multicast datagrams:"));ttlSpinBox = new QSpinBox;ttlSpinBox->setRange(0, 255);QHBoxLayout *ttlLayout = new QHBoxLayout;ttlLayout->addWidget(ttlLabel);ttlLayout->addWidget(ttlSpinBox);startButton = new QPushButton(tr("&Start"));quitButton = new QPushButton(tr("&Quit"));buttonBox = new QDialogButtonBox;buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);timer = new QTimer(this);udpSocket = new QUdpSocket(this);messageNo = 1;connect(ttlSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ttlChanged(int)));connect(startButton, SIGNAL(clicked()), this, SLOT(startSending()));connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addWidget(statusLabel);mainLayout->addLayout(ttlLayout);mainLayout->addWidget(buttonBox);setLayout(mainLayout);setWindowTitle(tr("Multicast Sender"));ttlSpinBox->setValue(1); }void Sender::ttlChanged(int newTtl) {udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl); }void Sender::startSending() {startButton->setEnabled(false);timer->start(1000); }void Sender::sendDatagram() {statusLabel->setText(tr("Now sending datagram %1").arg(messageNo));QByteArray datagram = "Multicast message " + QByteArray::number(messageNo);udpSocket->writeDatagram(datagram.data(), datagram.size(),groupAddress, 45454);++messageNo; }?
總結
以上是生活随笔為你收集整理的Qt文档阅读笔记-Multicast Receiver Example与Multicast Sender Example解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++|Qt工作笔记-4种方法判断当
- 下一篇: C++|Qt工作笔记-C++获取当前系统