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();private:int 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();private:int 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的简单通信例子,代码只给出了简单的一部分。布局可以自己看着实现。
- QT5 下udp 编程实例
- UDP windows下socket编程实例
- Linux下TCP和UDP编程实例
- UDP windows下socket编程实例
- 【Qt5开发及实例】29、实现服务器端的编程,UDP协议
- 【Qt5开发及实例】30、实现客户端的编程,UDP协议
- UDP编程实例
- java----UDP编程实例
- python udp编程实例
- Java UDP编程实例
- Qt5--多窗口编程实例
- LInux 下网络编程之一UDP通信实例详解
- Linux网络编程下UDP洪水攻击实例介绍
- linux下UDP编程
- linux下udp编程
- TCP/UDP 网络编程实例
- linux udp socket编程实例
- 嵌入式 UDP网络编程实例
- nginx虚拟主机的配置
- centos 7 下 C语言连接MYSQL
- 用户权限管理spring security
- C#调用C++字符集与类型对比
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- QT5 下udp 编程实例
- iOS开发——单例
- LibGDX_2.1: LibGDX 应用框架(6 大系统交互接口)
- ====手势的使用
- 《疯狂JAVA讲义》——instanceof运算符
- [读书笔记] The.Way.To.Go
- c#调用JAVA提供的WebService处理日期格式
- 以POJ1061青蛙的约会为例谈扩展欧几里得算法
- iOS工程适配64-bit经验分享