In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the "Qt implementation of UDP multithreaded data processing and sending method tutorial" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
Logic and operation
The program logic diagram is as follows:
The screenshot of the receiver running is as follows:
The client receives the data as follows:
The client uses a serial debugging tool:
Source code
The program structure is as follows:
The source code is as follows
Data.h
# ifndef DATA_H#define DATA_H# include # 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 insertMsgInList (m_type, msg) If (m_type = = RecvQueue) {/ / here you can write back-end processing} else {masking widget-> senderMsg (mlist [I]-> IPv4Address, mlist[ I]-> port);}} msleep (1000);}}
Widget.cpp
# include "widget.h" # include "ui_widget.h" # include "data.h" # include "msgqueue.h" # include # include Widget::Widget (QWidget * parent): QWidget (parent), ui (new Ui::Widget) {ui- > setupUi (this); this- > setWindowTitle ("CSDN IT1995"); m_udpSocket = new QUdpSocket (this) If (! receiverListWidget- senderListWidget--> bind (7755)) {qDebug () senderListWidget- > insertItem (0, msg);} else {ui- > receiverListWidget- > insertItem (0, msg);}} void Widget::senderMsg (quint32 ipV4, quint16 port) {QHostAddress address (ipV4); writeDatagram (QByteArray ("I am fine, Socket!"), 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 & & mpeerIP [I]-> port = = port) {qDebug () appendPeerIP (ipV4, port); masked receiv-> appendPeerIP (ipV4, port);} void Widget::closeEvent (QCloseEvent * event) {Q_UNUSED (event) masked sender-> stop (); milled receiv-> stop (); QEventLoop loop; QTimer::singleShot (1000, & loop, SLOT (quit ()); loop.exec (); this- > close () } void Widget::readPendingDatagrams () {while (srcAddress Socket-> hasPendingDatagrams ()) {QHostAddress srcAddress; quint16 nSrcPort; QByteArray datagram; datagram.resize (srcAddress.toIPv4Address () > pendingDatagramSize ()); readDatagram (datagram.data (), datagram.size (), & srcAddress, & Sockett); canAppendInList (srcAddress.toIPv4Address (), Sockett) }} "Qt to achieve UDP multithreaded data processing and sending method tutorial" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.