打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt之UDP的网络广播例子

UDP 用户数据报协议,是一种轻量级、无连接,不可靠,数据报的传输层协议。

一般用在短消息、广播消息等中,本文中是用在广播定时发送消息中。

 

注意还是要在.pro中加入 QT += network

广播定时发送给用户数据:

1、UDP的服务器端

        先创建一个QUdpSocket对象,再创建一个定时器QTimer每秒发送一次数据,

最后利用QUdpSocket的writeDatagram()函数来设置地址和绑定端口来发送数据。

 

2、UDP的客服端

       创建一个绑定好端口的QUdpSocket对象,此时最重要的一句话

connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived())); //其中dataReceived是自定义的函数,接受数据函数

当QUdpSocket对象每次要读入一个新数据的时候,就发送信号readyRead();

      再来就是编写我们自定义的接受数据函数了,利用QUdpSocket的readDatagram()函数即可获取数据。

 

截图:

 

 

 

服务端:

  1. #ifndef UDPSERVER_H  
  2. #define UDPSERVER_H  
  3.   
  4. #include <QDialog>  
  5. class QLabel;  
  6. class QLineEdit;  
  7. class QUdpSocket;  
  8.   
  9. class UdpServer: public QDialog  
  10. {  
  11.     Q_OBJECT  
  12.   
  13. private:  
  14.     int port;  
  15.     bool isStarted;  
  16.     QUdpSocket *udpSocket;  
  17.     QTimer *timer;  
  18.   
  19.     QLabel *timerLabel;  
  20.     QLineEdit *contEdit;  
  21.     QPushButton *startButton;  
  22.   
  23. public:  
  24.     UdpServer(QWidget *parent = 0);  
  25.   
  26. private slots:  
  27.     void slotStartButton();  
  28.     void timeoutDo();  
  29. };  
  30.   
  31. #endif // UDPSERVER_H  


 

  1. #include "udpserver.h"  
  2.   
  3. #include <QtGui>  
  4. #include <QUdpSocket>  
  5.   
  6. UdpServer::UdpServer(QWidget *parent)  
  7.     : QDialog(parent)  
  8. {  
  9.     port = 5555;  
  10.     isStarted = false;  
  11.     udpSocket = new QUdpSocket(this);  
  12.     timer = new QTimer(this);  
  13.     //设置定时器,当时间到的时候就调用timeoutDo()槽  
  14.     connect(timer, SIGNAL(timeout()), this, SLOT(timeoutDo()));  
  15.   
  16.     QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));  
  17.     timerLabel = new QLabel(tr("定时发送"));  
  18.   
  19.     contEdit = new QLineEdit;  
  20.     startButton = new QPushButton(tr("开始"));  
  21.     connect(startButton, SIGNAL(clicked()),  
  22.             this, SLOT(slotStartButton()));  
  23.   
  24.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  25.     mainLayout->addWidget(timerLabel);  
  26.     mainLayout->addWidget(contEdit);  
  27.     mainLayout->addWidget(startButton);  
  28.     setLayout(mainLayout);  
  29.     setWindowTitle(tr("UDP Server"));  
  30. }  
  31.   
  32. void UdpServer::slotStartButton()  
  33. {  
  34.     if(false == isStarted) {  
  35.         startButton->setText(tr("停止"));  
  36.         isStarted = true;  
  37.         timer->start(1000);  
  38.     }  
  39.     else {  
  40.         startButton->setText(tr("开始"));  
  41.         isStarted = false;  
  42.         timer->stop();  
  43.     }  
  44. }  
  45.   
  46. void UdpServer::timeoutDo()  
  47. {  
  48.     QString msg = contEdit->text();  
  49.     int length = 0;  
  50.     if("" == msg)  
  51.         return ;  
  52.     else if((length = udpSocket->writeDatagram(  
  53.                  msg.toLatin1(), msg.length(),  
  54.                  QHostAddress::Broadcast, port)) != msg.length()) {  
  55.         return ;  
  56.     }  
  57. }  

 

  1. #include "udpserver.h"  
  2.   
  3. #include <QApplication>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication app(argc, argv);  
  8.   
  9.     UdpServer *server = new UdpServer;  
  10.     server->show();  
  11.   
  12.     return app.exec();  
  13. }  

 

 

//客服端:

  1. #ifndef UDPCLIENT_H  
  2. #define UDPCLIENT_H  
  3.   
  4. #include <QDialog>  
  5. class QTextEdit;  
  6. class QUdpSocket;  
  7.   
  8. class UdpClient: public QDialog  
  9. {  
  10.     Q_OBJECT  
  11.   
  12. private:  
  13.     int port;  
  14.     QUdpSocket *udpSocket;  
  15.   
  16.     QTextEdit *showText;  
  17.     QPushButton *closeButton;  
  18.   
  19. public:  
  20.     UdpClient(QWidget *parent = 0);  
  21.   
  22. private slots:  
  23.     void slotClose();  
  24.     void dataReceived();  
  25. };  
  26.   
  27.   
  28. #endif // UDPCLIENT_H  


 

  1. #include "udpclient.h"  
  2.   
  3. #include <QtGui>  
  4. #include <QUdpSocket>  
  5.   
  6. UdpClient::UdpClient(QWidget *parent)  
  7.     : QDialog(parent)  
  8. {  
  9.     port = 5555;  
  10.     udpSocket = new QUdpSocket(this);  
  11.     bool result = udpSocket->bind(port);  
  12.     if(!result) {  
  13.         QMessageBox::information(this, tr("error"),  
  14.                                  tr("udp socket create error!"));  
  15.         return ;  
  16.     }  
  17.     connect(udpSocket, SIGNAL(readyRead()),  
  18.             this, SLOT(dataReceived()));  
  19.   
  20.     showText = new QTextEdit;  
  21.     showText->setReadOnly(true);  
  22.     closeButton = new QPushButton(tr("Close"));  
  23.     connect(closeButton, SIGNAL(clicked()),  
  24.             this, SLOT(slotClose()));  
  25.   
  26.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  27.     mainLayout->addWidget(showText);  
  28.     mainLayout->addWidget(closeButton);  
  29.     setLayout(mainLayout);  
  30.     setWindowTitle(tr("UDP Client"));  
  31. }  
  32.   
  33. //private slots  
  34. void UdpClient::slotClose()  
  35. {  
  36.     udpSocket->close();  
  37. }  
  38.   
  39. void UdpClient::dataReceived()  
  40. {  
  41.     while(udpSocket->hasPendingDatagrams()) {  
  42.         QByteArray datagram;  
  43.         datagram.resize(udpSocket->pendingDatagramSize());  
  44.   
  45.         udpSocket->readDatagram(datagram.data(), datagram.size());  
  46.         showText->insertPlainText(datagram.data());  
  47.     }  
  48. }  


 

  1. #include "udpclient.h"  
  2.   
  3. #include <QApplication>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication app(argc, argv);  
  8.   
  9.     UdpClient *client = new UdpClient;  
  10.     client->show();  
  11.   
  12.     return app.exec();  
  13. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【2011.03.17】QT 的UDP SOCKET编程
Qt的Udp编程总结
高性能异步Socket服务器(UDP)
仿QQ聊天简单的UDP传输实例
C# udp 通信 例子
Using TCP/IP or UDP to Communicate with Serialized Objects
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服