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;
};#endif
main.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;
};#endif
main.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解析相关推荐
- Qt文档阅读笔记-Broadcast Receiver Example解析
这篇博文的例子是说明如何在局域网上搭建广播包接收端. 这里使用了Qt Network API,搭建本地广播包接收端. 结构如下: 代码如下: receiver.h #ifndef RECEIVER_H ...
- Qt文档阅读笔记-共享库的创建与调用
使用共享库的符号 这个符号可以作用在变量.类.函数中,并且这些都可以被调用端使用. 在编译共享库中,需要使用export符号.在使用端调用的时候使用import符号. 这里是本人从文档中记录的笔记,大 ...
- Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图
Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图 QHeightMapSurfaceDataProxy:是Q3DSurface的一个基本代理类. 他是专门加载高度图. 高度图是没有X, ...
- Qt文档阅读笔记-Rotations Example相关
Rotations Example文档阅读笔记 使用这种方式,对y轴和z轴进行旋转. QQuaternion yRotation = QQuaternion::fromAxisAndAngle(0.0 ...
- Qt文档阅读笔记-QWebEngineView及QML WebEngineView
这里主要是最近有给Java Web项目及Qt项目需要混合,自己搞的QtWebEngine没有问题,而用了项目里面的,就有问题,在此阅读下官方资料,看看能不能解决这样莫名其妙的问题,在此记录下本次的阅读 ...
- Qt文档阅读笔记-moc工具使用及其理论(moc工具如何添加到makefile中)
moc(Meta-Object Compiler):用于处理Qt中关于C++的扩展. moc工具读取C++头文件,看看这个头文件里面的类是不是包含Q_OBJECT宏,它会将这个头文件,转换为带有met ...
- Qt文档阅读笔记-Fortune Client Example实例解析
目录 官方解析 实例代码 博主增加解析 官方解析 Fortune Client Example 以使用QTcpSocket为例子,服务端可以配合Fortune Server或Threaded Fort ...
- Qt文档阅读笔记-对Style Plugin Example实例的解析
目录 前言 Style Plugin Example SimpleStylePlugin Class 定义 SimpleStylePlugin Class 实现 main()函数 The Simple ...
- Qt文档阅读笔记-QSet官方解析及实例
目录 官方解析 博主栗子 官方解析 QSet类是一个模板类,他是一个哈希表集合. QSet<T>是Qt的一个普通容器类.QSet存储的值是不指明顺序的,QSet对这些值提供了快速检索的功能 ...
最新文章
- C++之typedef 小记
- Vue单文件组件的使用
- pyqt5——菜单和工具栏
- python 删除链表中的重复元素
- 22-爬虫之scrapy框架分布式09
- Java核心篇之HashMap--day6
- (第八章)左右外连接
- Python 进阶之递归函数一点都不难!
- React Fiber 了解一下
- 局域网中服务器群配置ssh免密
- 2011年国庆老家记录
- 计算机制作ppt考试题,计算机二级PPT真题:制作计算机发展简史PPT
- qq空间个人档html代码,QQ空间如何进行个人档管理?
- 论组织管理-EMBA课程小记
- AB01固定资产过账
- 【深度】被加班,狼性文化面纱下的奴性文化
- Linux监控多台远程服务器磁盘空间剩余情况并发送预警邮件以及电话告警
- 词云可视化——四行代码轻松上手
- 李宏毅——一天搞懂深度学习PPT学习笔记
- 什么游戏蓝牙耳机好?专业电竞玩家教你如何选择
热门文章
- onbeforeunload与onunlond的区别
- Python很简单,你一定能学会【加油!】
- 很有气势的语言openeim
- [多图]全球范围内Linux用户普及度数据
- .net控件FreeTextBox使用方法
- BAT网页10年巨变,背后的前端技术你都知道吗?
- 最悲剧的HTML5 API : Position地理位置
- 双目估计方法_教你提高双目立体视觉系统的精度!
- “你都硕士博士了,竟然还不如我!”
- 纪念诺贝尔生理医学奖获得者 Sydney Brenner (1927-2019)