您好,登录后才能下订单哦!
请求超时!
请点击 重新获取二维码本篇内容介绍了“Qt实现UDP多线程数据处理及发送的方法教程”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
程序逻辑图如下:
接收端运行截图如下:
客户端接收数据如下:
客户端用的是串口调试工具:
程序结构如下:
源码如下:
data.h
#ifndef DATA_H #define DATA_H #include <QObject> #include <QHostAddress> #include <QString> #include <QDebug> #define SenderListWidget 0 #define ReceviListWidget 1 class PeerIP{ public: quint32 IPv4Address; quint16 port; PeerIP(const quint32 Ip, const quint16 por){ IPv4Address = Ip; port = por; } friend QDebug operator << (QDebug os, PeerIP peerIP){ os << "(" << peerIP.IPv4Address << ", " << peerIP.port << ")"; return os; } }; class UDPMsg{ public: virtual QString backFunction(const PeerIP *peerIP){ Q_UNUSED(peerIP) return ""; } protected: UDPMsg(){} virtual ~UDPMsg(){} }; class UDPMsgReciver:public UDPMsg{ public: QString backFunction(const PeerIP *peerIP){ QHostAddress address(peerIP->IPv4Address); QString msg = "接收到P:" + address.toString() + " 端口:" + QString::number(peerIP->port) + "发来数据包, 正在处理数据"; return msg; } }; class UDPMsgSender:public UDPMsg{ public: QString backFunction(const PeerIP *peerIP){ QHostAddress address(peerIP->IPv4Address); QString msg = "已发送到IP:" + address.toString() + " 端口:" + QString::number(peerIP->port) + "UDP数据包,准备发送数据"; return msg; } }; #endif // DATA_H
msgqueue.h
#ifndef MSGQUEUE_H #define MSGQUEUE_H #include <QThread> #include <QList> #include <QWidget> class PeerIP; class UDPMsg; class Widget; class MsgQueue: public QThread { public: enum MsgType{RecvQueue, SendQueue}; MsgQueue(Widget *widget, MsgType type); ~MsgQueue(); void appendPeerIP(const quint32 ipV4, const quint16 port); void stop(); protected: void run(); private: QList<PeerIP*> m_list; MsgType m_type; bool m_canExit; UDPMsg *m_udpMsg; Widget *m_widget; }; #endif // MSGQUEUE_H
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QList> QT_BEGIN_HEADER class QUdpSocket; QT_END_NAMESPACE class PeerIP; class MsgQueue; namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); void insertMsgInList(const int Type, const QString msg); void senderMsg(quint32 ipV4, quint16 port); protected: void canAppendInList(const quint32 ipV4, const quint16 port); void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; protected slots: void readPendingDatagrams(); private: Ui::Widget *ui; QUdpSocket *m_udpSocket; QList<PeerIP*> m_peerIP; MsgQueue *m_sender; MsgQueue *m_receiv; }; #endif // WIDGET_H
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
msgqueue.cpp
#include "msgqueue.h" #include "data.h" #include "widget.h" #include <QDebug> MsgQueue::MsgQueue(Widget *widget, MsgType type): m_canExit(false) { if(type == RecvQueue){ m_udpMsg = new UDPMsgSender; } else{ m_udpMsg = new UDPMsgReciver; } m_widget = widget; m_type = type; start(); } MsgQueue::~MsgQueue() { for(int i = 0; i < m_list.size(); i++){ delete m_list[i]; } } void MsgQueue::appendPeerIP(const quint32 ipV4, const quint16 port) { PeerIP *peerIp = new PeerIP(ipV4, port); m_list.append(peerIp); } void MsgQueue::stop() { m_canExit = true; } void MsgQueue::run() { while(!m_canExit){ for(int i = 0; i < m_list.size(); i++){ QString msg = m_udpMsg->backFunction(m_list[i]); m_widget->insertMsgInList(m_type, msg); if(m_type == RecvQueue){ //这里可以写后端处理 } else{ m_widget->senderMsg(m_list[i]->IPv4Address, m_list[i]->port); } } msleep(1000); } }
widget.cpp
#include "widget.h" #include "ui_widget.h" #include "data.h" #include "msgqueue.h" #include <QUdpSocket> #include <QNetworkDatagram> #include <QHostAddress> #include <QDebug> #include <QEventLoop> #include <QTimer> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setWindowTitle("CSDN IT1995"); m_udpSocket = new QUdpSocket(this); if(!m_udpSocket->bind(7755)){ qDebug() << "bind failed! The assert will be triggred!"; Q_ASSERT(!"bind failed!"); } m_sender = new MsgQueue(this, MsgQueue::SendQueue); m_receiv = new MsgQueue(this, MsgQueue::RecvQueue); connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); } Widget::~Widget() { delete ui; delete m_sender; delete m_receiv; for(int i = 0; i < m_peerIP.size(); i++){ delete m_peerIP[i]; } } void Widget::insertMsgInList(const int Type, const QString msg) { if(Type == SenderListWidget){ ui->senderListWidget->insertItem(0, msg); } else{ ui->receiverListWidget->insertItem(0, msg); } } void Widget::senderMsg(quint32 ipV4, quint16 port) { QHostAddress address(ipV4); m_udpSocket->writeDatagram(QByteArray("I am fine, fuck you!"), address, port); } void Widget::canAppendInList(const quint32 ipV4, const quint16 port) { for(int i = 0; i < m_peerIP.size(); i++){ if(m_peerIP[i]->IPv4Address == ipV4 && m_peerIP[i]->port == port){ qDebug() << "client in list"; return; } } PeerIP *peerIP = new PeerIP(ipV4, port); m_peerIP.append(peerIP); m_sender->appendPeerIP(ipV4, port); m_receiv->appendPeerIP(ipV4, port); } void Widget::closeEvent(QCloseEvent *event) { Q_UNUSED(event) m_sender->stop(); m_receiv->stop(); QEventLoop loop; QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec(); this->close(); } void Widget::readPendingDatagrams() { while(m_udpSocket->hasPendingDatagrams()){ QHostAddress srcAddress; quint16 nSrcPort; QByteArray datagram; datagram.resize(m_udpSocket->pendingDatagramSize()); m_udpSocket->readDatagram(datagram.data(), datagram.size(), &srcAddress, &nSrcPort);; canAppendInList(srcAddress.toIPv4Address(), nSrcPort); } }
“Qt实现UDP多线程数据处理及发送的方法教程”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
PHP网站源码布吉模板网站建设宝安百度竞价包年推广木棉湾网站优化推广福永英文网站建设布吉seo南澳建站东莞百度网站优化排名石岩建设网站石岩关键词排名包年推广大芬外贸网站设计宝安百度标王大芬外贸网站设计民治网络推广南山网站设计坑梓网络广告推广盐田外贸网站建设荷坳网站改版坪山企业网站设计大芬网站推广工具坑梓网站建设设计大运外贸网站制作光明关键词排名包年推广爱联网页制作深圳网络营销松岗网站优化按天扣费广州SEO按天计费沙井网页制作坂田模板网站建设布吉至尊标王广州营销型网站建设歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运