QT_Socket_udp
2016-03-17 23:34
429 查看
这是一个基于QT Socket的udp小程序
有Udp1.cpp,Udp2.cpp,MyWidget.cpp。
下面先看
MyWidget.cpp
Udp1.cpp
Udp2.cpp
我- - - - - - - 是- - - - - - - 华- - - - - - - 丽- - - - - - - 的- - - - - - - 分- - - - - - - 割- - - - - - - 线- - - - - - -
最后,补充一下头文件
Udp1.h
Udp2.h
有Udp1.cpp,Udp2.cpp,MyWidget.cpp。
下面先看
MyWidget.cpp
#include "MyWidget.h" #include <QFile> #include <QApplication> #include <QDebug> #include <QBuffer> #include <QLabel> #include <QTextStream> #include <QDataStream> #include "Udp1.h" #include "Udp2.h" MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { } int main(int argc,char** argv) { QApplication app(argc,argv); Udp1 udp1;udp1.show(); Udp2 udp2;udp2.show(); udp1.setWindowTitle("udp1"); udp2.setWindowTitle("udp2"); app.exec(); }
Udp1.cpp
#include "Udp1.h" #include <QTimer> #include <QDateTime> Udp1::Udp1(QWidget *parent) : QWidget(parent) { _udp = new QUdpSocket; _udp->bind(10001); connect(_udp,SIGNAL(readyRead()),this,SLOT(slotReaydRead())); QTimer* timer = new QTimer; timer->setInterval(1000); timer->start(); connect(timer,&QTimer::timeout,[&](){ quint64 timestamp = QDateTime::currentMSecsSinceEpoch(); QString str = QString::number(timestamp); #if 0 _udp->writeDatagram(str.toUtf8(),QHostAddress("127.0.0.1"),10002); #else //广播 _udp->writeDatagram(str.toUtf8(),QHostAddress::Broadcast,10002); //多播 _udp->writeDatagram(str.toUtf8(),QHostAddress("224.0.0.131"),10002); #endif }); } void Udp1::slotReaydRead() { while(_udp->hasPendingDatagrams()){ qint32 datagramSize = _udp->pendingDatagramSize(); QByteArray buf(datagramSize,0); _udp->readDatagram(buf.data(),buf.size()); qDebug() << "udp1" << buf; } }
Udp2.cpp
#include "Udp2.h" #include <QTimer> #include <QDateTime> Udp2::Udp2(QWidget *parent) : QWidget(parent) { _udp = new QUdpSocket; _udp->bind(QHostAddress::AnyIPv4,10002); _udp->joinMulticastGroup(QHostAddress("224.0.0.131")); connect(_udp,SIGNAL(readyRead()),this,SLOT(slotReaydRead())); QTimer* timer = new QTimer; timer->setInterval(1000); timer->start(); connect(timer,&QTimer::timeout,[&](){ quint64 timestamp = QDateTime::currentMSecsSinceEpoch(); QString str = QString::number(timestamp); _udp->writeDatagram(str.toUtf8(),QHostAddress("127.0.0.1"),10001); }); } void Udp2::slotReaydRead() { while(_udp->hasPendingDatagrams()){ qint32 datagramSize = _udp->pendingDatagramSize(); QByteArray buf(datagramSize,0); _udp->readDatagram(buf.data(),buf.size()); qDebug() << "udp2" << buf; } }
我- - - - - - - 是- - - - - - - 华- - - - - - - 丽- - - - - - - 的- - - - - - - 分- - - - - - - 割- - - - - - - 线- - - - - - -
最后,补充一下头文件
Udp1.h
#ifndef UDP1_H #define UDP1_H #include <QWidget> #include <QUdpSocket> #include <QByteArray> class Udp1 : public QWidget { Q_OBJECT public: explicit Udp1(QWidget *parent = 0); QUdpSocket* _udp; signals: public slots: void slotReaydRead(); }; #endif // UDP1_H
Udp2.h
#ifndef UDP1_H #define UDP1_H #include <QWidget> #include <QUdpSocket> #include <QByteArray> class Udp1 : public QWidget { Q_OBJECT public: explicit Udp1(QWidget *parent = 0); QUdpSocket* _udp; signals: public slots: void slotReaydRead(); }; #endif // UDP1_H
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- c++ primer 第五版 笔记前言
- Qt 5.6更新至RC版,最终版本近在咫尺
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例