QT5 下udp 编程实例

来源:互联网 发布:dede上传网站源码 编辑:程序博客网 时间:2024/05/01 20:28

下面是一个简单的QT5下的udp通信的下例子。服务器不停的利用定时器来向socket发送广播消息,客户端可以接收该消息并显示。

首先建立工程UdpServer.pro。建立各控件的布局。

udpserver.h:class UdpServer:public QDialog{Q_OBJECTpublic:UdpServer(QWidget *parent=0,Qt::WindowFlags f=0);~UdpServer();prvate:QLabel *TimerLabel;QLineEdit *TextLineEdit;QPushButton *StartBtn;QVBoxLayout *mainLayout;};udpserver.cppUdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f){setWindowTitle(tr("UDP 服务器"));TimerLabel = new QLabel(tr("Timer:),this);TextLineEdit = new QLineEdit(this);StartBtn = new QPushButton(tr("Start"),this);mainLayout = new QVBoxLayout(this);mainLayout->addWidget(TimerLabel);mainLayout->addWidget(TextLineEdit);mainLayout->addWidget(StartBtn);}

下面实现服务器的消息广播功能:
在UdpServer.pro 中加入 :

QT += network
这样就可以支持网络开发了。

在udpserver.h中添加想要的槽函数

public slots:void StartBtnClicked();void timeout();privateint port;bool isStarted;QDdpSocket *udpSocket;QTimer *timer;

在源文件udpserver.cpp的构造函数中添加如下代码

conne(startBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));port = 5555;isStarted = false;UDPSocket = new QUdpSocket(this);//构建一个sockettimer = new QTimer(this);//构建一个定时器connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));

槽函数实现:

void UdpServer::StartBtnClicked(){if(!isStarted){StartBtn->setText(tr("STOP");timer->start(1000);//启动定时器isStarted = true;}else{StartBtn->setText(tr("Start"));isStarted = false;timer->stop();}}

timeout函数的实现广播消息:

void Udpserver::timeout(){QString message = TextLineEdit->text();//获取要广播的消息文本int length = 0//文本长度置零if(message= “”)//如果输入的文本为空,就返回{return;}/*向指定的地址和端口写入数据报消息,如果写入的长度和输入的长度不同,程序返回*/if((length = UDPSocket->writeDatagram(message.toLatin1(),message.length(),QHOSTAddress::Broadcast,port))!=message.length()){return;}}

*——————————————————————————————————————————————————————————————–

下面udp客户端的实现。获取服务器广播的消息,并显示出来。
首先建立工程 UdpClient.pro 建立各个控件的布局。
udpclient.h:

class UdpClient:public QDialog{Q_OBJECTpublic:UdpClient(QWidget *parent =0,Qt::WindowFlags f=0);~UdpClient();private:QTextEdit *ReceiveTextEdit;QPushButton *CloseBtn;QVBoxLayout *mainLayout;};

udpclient.cpp

UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f){setWindowTitle(tr("Udp client"));ReceiveText = new QTextEdit(this);CloseBtn = new QPushButton(this);mainLayout = new QVBoxLayout(this);mainLayout->addWidget(ReceiveTextEdit);mainLayout->addWidget(CloseBtn);}

在UdpClient.pro中添加以下语句以支持网络开发:
QT += network

在udpclient.h中添加以下代码:

public slots:void CloseBtnClicked();void dataReceived();privateint port;QUdpSocket *UDPSocket;

在udpclient.cpp构造函数中添加以下代码:

connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked()));port = 5555//端口号udpSocket = new QUdpSocket(this);//构建客户端的socketconnect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));//连接readyRead()信号与读取数据的槽bool result = udpSocket->bind(port);//绑定到指定的端口号if(!result)//若绑定不成功,给出出错信息{QMessageBox::information(this,tr(“Error”),tr(“UDP Socket Error”));return;}各个槽函数的实现

void UdpClient::CloseBtnClicked()//关闭socket
{
close();
}

void UdpClient::dataReceived()//接收数据并显示
{
while(udpSocket->hasPendingDatagram())//只要socket有数据到达就循环的读取显示
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString message = datagram.data();
ReceiveTextEdit->insertPlainText(message);
}
}
“`

————————————————————————————————————————————————————————————————–
以上就是一个简单的udp的简单通信例子,代码只给出了简单的一部分。布局可以自己看着实现。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 单位有个事特别多的领导怎么办 网上申请公司核名核不下来怎么办 老公把小三晒朋友圈老婆该怎么办 小三怀孕不愿意打掉怎么办 小三怀孕了引产怎么办 小三发现小四小三发现小四后怎么办 七十岁老人肺癌还有小三样怎么办 留学出国学历公认证怎么办 学历认证报告弄丢了怎么办 想读大专没考上怎么办 学信网身份证被注册了怎么办 学信网手机号换了密保忘了怎么办 学信网手机号改了密保忘了怎么办 学信网手机号忘了密保也忘了怎么办 学信网账号被注销了怎么办 原味奶酪太难吃怎么办 cma年费太贵了怎么办 会计证3年没年检怎么办 幼师面试没有什么特长怎么办 电子学历注册表过期了怎么办 学信网查不到电子注册登记表怎么办 学历证书电子注册备案表过期怎么办 举报人对处理结果不满意怎么办 亚马逊账号申诉后余额怎么办 怎么办北京市工会会员互助服务卡 国家发改委录用公示后怎么办 慕课的账号忘了怎么办 清华同方无线鼠标没反应怎么办 全日制专硕考上公务员了怎么办 超出了期刊编辑部审稿时间怎么办 教师晋职称毕业证丢了怎么办 下属部门一直不交材料怎么办 简书投稿被拒绝怎么办 论文引用文献为0怎么办 小米屏幕录制卸载了怎么办 用edius剪的视频卡顿怎么办 微店手机号换号登不进去了怎么办 微信加人被限制怎么办 微信与电脑同步怎么办 微信号盗了红包怎么办 微信被别人登录冻结了怎么办?