当前位置:   article > 正文

QT5 下UDP 编程实例:客户端,服务器端_qt5 udp客户端

qt5 udp客户端

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

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

  1. udpserver.h:
  2. class UdpServer:public QDialog
  3. {
  4. Q_OBJECT
  5. public:
  6. UdpServer(QWidget *parent=0,Qt::WindowFlags f=0);
  7. ~UdpServer();
  8. prvate:
  9. QLabel *TimerLabel;
  10. QLineEdit *TextLineEdit;
  11. QPushButton *StartBtn;
  12. QVBoxLayout *mainLayout;
  13. };
  14. udpserver.cpp
  15. UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f)
  16. {
  17. setWindowTitle(tr("UDP 服务器"));
  18. TimerLabel = new QLabel(tr("Timer:),this);
  19. TextLineEdit = new QLineEdit(this);
  20. StartBtn = new QPushButton(tr("Start"),this);
  21. mainLayout = new QVBoxLayout(this);
  22. mainLayout->addWidget(TimerLabel);
  23. mainLayout->addWidget(TextLineEdit);
  24. mainLayout->addWidget(StartBtn);
  25. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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

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

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

  1. public slots:
  2. void StartBtnClicked();
  3. void timeout();
  4. private
  5. int port;
  6. bool isStarted;
  7. QDdpSocket *udpSocket;
  8. QTimer *timer;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

  1. conne(startBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));
  2. port = 5555
  3. isStarted = false
  4. UDPSocket = new QUdpSocket(this);//构建一个socket
  5. timer = new QTimer(this);//构建一个定时器
  6. connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

槽函数实现:

  1. void UdpServer::StartBtnClicked()
  2. {
  3. if(!isStarted)
  4. {
  5. StartBtn->setText(tr("STOP");
  6. timer->start(1000);//启动定时器
  7. isStarted = true;
  8. }
  9. else
  10. {
  11. StartBtn->setText(tr("Start"));
  12. isStarted = false;
  13. timer->stop();
  14. }
  15. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

timeout函数的实现广播消息:

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

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

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

  1. class UdpClient:public QDialog
  2. {
  3. Q_OBJECT
  4. public:
  5. UdpClient(QWidget *parent =0,Qt::WindowFlags f=0);
  6. ~UdpClient();
  7. private:
  8. QTextEdit *ReceiveTextEdit;
  9. QPushButton *CloseBtn;
  10. QVBoxLayout *mainLayout;
  11. };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

udpclient.cpp

  1. UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f)
  2. {
  3. setWindowTitle(tr("Udp client"));
  4. ReceiveText = new QTextEdit(this);
  5. CloseBtn = new QPushButton(this);
  6. mainLayout = new QVBoxLayout(this);
  7. mainLayout->addWidget(ReceiveTextEdit);
  8. mainLayout->addWidget(CloseBtn);
  9. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

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

  1. public slots:
  2. void CloseBtnClicked();
  3. void dataReceived();
  4. private
  5. int port;
  6. QUdpSocket *UDPSocket;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

  1. connect(CloseBtnSIGNAL(clicked()),thisSLOTCloseBtnClicked()));
  2. port = 5555//端口号
  3. udpSocket = new QUdpSocketthis);//构建客户端的socket
  4. connect(udpSocket,SIGNAL(readyRead()),thisSLOT(dataReceived()));//连接readyRead()信号与读取数据的槽
  5. bool result = udpSocket->bind(port);//绑定到指定的端口号
  6. if(!result)//若绑定不成功,给出出错信息
  7. {
  8. QMessageBox::information(this,tr(“Error”),tr(“UDP Socket Error”));
  9. return
  10. }
  11. 各个槽函数的实现
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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的简单通信例子,代码只给出了简单的一部分。布局可以自己看着实现。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/305188
推荐阅读
相关标签
  

闽ICP备14008679号