qtudp多线程收发过程_Qt⼯作笔记-UDP多线程数据处理及发
送(简单实例)
⽬录
peer逻辑与运⾏
源码
逻辑与运⾏
程序逻辑图如下:
接收端运⾏截图如下:
客户端接收数据如下:
客户端⽤的是串⼝调试⼯具:
源码
程序结构如下:
源码如下:
data.h
#ifndef DATA_H
#define DATA_H
#include #include #include #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 << (QDebug os, PeerIP peerIP){
os << "(" << peerIP.IPv4Address << ", " << peerIP.port
<< ")";
return os;
}
};
class UDPMsg{
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:" + String() + " 端⼝:" + QString::number(peerIP->port) + "发来数据包, 正在处理数据"; return msg;
}
};
class UDPMsgSender:public UDPMsg{
public:
QString backFunction(const PeerIP *peerIP){
QHostAddress address(peerIP->IPv4Address);
QString msg = "已发送到IP:" + String() + " 端⼝:" + QString::number(peerIP->port) + "UDP数据包,准备发送数据"; return msg;
}
};
#endif // DATA_H
msgqueue.h
#ifndef MSGQUEUE_H
#define MSGQUEUE_H
#include #include #include class PeerIP;
class UDPMsg;
class Widget;
class MsgQueue: public QThread
{
enum MsgType{RecvQueue, SendQueue};
MsgQueue(Widget *widget, MsgType type);
~MsgQueue();
void appendPeerIP(const quint32 ipV4, const quint16 port); void stop();
protected:
void run();
private:
QListm_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 #include 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;
QListm_peerIP;
MsgQueue *m_sender;
MsgQueue *m_receiv;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
();
}
msgqueue.cpp
#include "msgqueue.h"
#include "data.h"
#include "widget.h"
#include 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);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论