QT网络编程UDP下C/S架构广播通信的示例分析
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,今天就跟大家聊聊有关QT网络编程UDP下C/S架构广播通信的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。QT有封装好的UDP协议的类
千家信息网最后更新 2026年11月04日QT网络编程UDP下C/S架构广播通信的示例分析
今天就跟大家聊聊有关QT网络编程UDP下C/S架构广播通信的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
QT有封装好的UDP协议的类,QUdpSocket,里面有我们想要的函数接口。感兴趣的话,可以看看。
先搞服务端吧,写一个子类,继承QDialog类,起名为UdpServer类。头文件要引用我们上边说的QUdpSocket这个类,还有我们想要的布局的类。
#ifndef UDPSERVER_H#define UDPSERVER_H#include#include #include #include #include #include #include #include class UdpServer : public QDialog{ Q_OBJECTpublic: UdpServer(QWidget *parent = 0,Qt::WindowFlags f= 0); ~UdpServer();private: QLabel * TimerLabel; QLineEdit * TextLineEdit; QPushButton* StartBtn; QVBoxLayout * mainLayout; public slots: void StartBtnClicked(); void timeout(); private: int port; bool isStarted; QUdpSocket * udpSocket; QTimer *timer;};#endif // UDPSERVER_H
在.cpp文件里,我们先是把界面显示出来,然后用udp的writedategram把想要传的写进去。
#include "udpserver.h"UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f) : QDialog(parent,f){ setWindowTitle(tr("UDP SERVER")); TimerLabel = new QLabel(tr("show time:"),this); TextLineEdit = new QLineEdit(this); StartBtn = new QPushButton(tr("start"),this); mainLayout = new QVBoxLayout(this); mainLayout-> addWidget(TimerLabel); mainLayout-> addWidget(TextLineEdit); mainLayout-> addWidget(StartBtn); connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked())); port = 5555; isStarted = false; udpSocket = new QUdpSocket(this); timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));}UdpServer::~UdpServer(){}void UdpServer::StartBtnClicked(){ if(!isStarted) { StartBtn->setText(tr("STOP")); timer->start(1000); isStarted = true; } else { StartBtn->setText(tr("BEGIN")); isStarted = false; timer->stop(); }}void UdpServer::timeout(){ QString msg = TextLineEdit->text(); int length=0; if(msg=="") { return; } if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length()) { qDebug() << msg.toLatin1(); return; }}我这里用qDebug把要传的东西打印出来,进行测试,看看是否传过去了。
客户端:
#ifndef UDPCLIENT_H#define UDPCLIENT_H#include#include #include #include #include class UdpClient : public QDialog{ Q_OBJECT public: UdpClient(QWidget *parent = 0); ~UdpClient(); private: QTextEdit* ReceiceTextEdit; QPushButton* CloseBtn; QVBoxLayout* mainLayout; public slots: void CloseBtnClicked(); void dataReceived(); private: int port; QUdpSocket* udpSocket;};#endif // UDPCLIENT_H
客户端很简单,怎么实现布局,我就不多说了,主要是dataReceive函数。
#include "udpclient.h"#include#include UdpClient::UdpClient(QWidget *parent) :QDialog(parent){ setWindowTitle("UDP CLIENT"); ReceiceTextEdit = new QTextEdit(this); CloseBtn = new QPushButton(tr("Close"),this); mainLayout = new QVBoxLayout(this); mainLayout->addWidget(ReceiceTextEdit); mainLayout->addWidget(CloseBtn); connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked())); port =5555; udpSocket = new QUdpSocket(this); bool result = udpSocket->bind(port); if(!result) { QMessageBox::information(this,tr("ERROR"),tr("connect error")); return; } connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));} UdpClient:: ~UdpClient(){}void UdpClient::CloseBtnClicked(){ close();}void UdpClient::dataReceived(){ while(udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(),datagram.size()); QString msg=datagram.data(); ReceiceTextEdit->insertPlainText(msg); }}
最后显示一下界面,服务端发送hello。

客户端收到的:
不停的在打印hello。直到点击关闭,或者服务端停止。
看完上述内容,你们对QT网络编程UDP下C/S架构广播通信的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
内容
客户
客户端
服务
架构
示例
网络
网络编程
分析
广播
编程
通信
函数
布局
文件
界面
上边
东西
兴趣
子类
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手冲咖啡和咖啡机区别
科技互联网
湖北九亚互联网络科技有限公司
北京路客互联网科技有限公司
互联网科技卖菜
互联网与科技发展
互联网 科技公司
凡科互联网科技
多迪互联网科技有限公司
互联网 科技服务平台
成都玖崟互联网科技有限公司乐商圈怎么样
互联网科技中介
成都互联网科技公司100强
互联网与医疗科技
中国互联网十大科技股排名
互联网金融科技展
畅思互联网科技
互联网公司黑科技创始人
互联网科技数字货币
上海万顺科技互联网有限公司招聘
新华互联网科技学校
华宇互联网科技
互联网科技小贷
互联网科技视频
成都玖崟互联网科技有限公司乐商圈怎么样
青岛科技大学互联网大赛研究生
广西互联网科技孵化中心
广州互联网高科技取餐柜
互联网时代科技型公司的机会
普陀区互联网教育科技管理办法