博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT 使用QUdpSocket QUdpServer UDP 建立客户端与服务器端
阅读量:4842 次
发布时间:2019-06-11

本文共 3751 字,大约阅读时间需要 12 分钟。

1. 模拟天气监控,每隔两秒从Server发送天气信息到Client.

 

2. 示例代码 

--------------------------- Server 端 -------------------------------------

修改工程文件.pro , QT += network, 添加net模块

udpsend.h

#ifndef UDPSEND_H#define UDPSEND_H#include 
#include
#include
class udpSend : public QPushButton //继承自QPushButton 双击退出{ Q_OBJECT public: udpSend(QWidget *parent = 0); ~udpSend();private slots: void sendDatagram();private: QUdpSocket udpSocket; QTimer timer;};#endif // UDPSEND_H

udpsend.cpp

#include "udpsend.h"udpSend::udpSend(QWidget *parent)    : QPushButton(tr("clicked, quit"),parent){    connect(this, SIGNAL(clicked()), this, SLOT(close()));    connect(&timer, SIGNAL(timeout()), this, SLOT(sendDatagram())); //定时发送    timer.start(2000);    setSizeIncrement(100,100);    setWindowTitle(tr("Weather Balloon"));}udpSend::~udpSend(){    }void udpSend::sendDatagram(){    QByteArray datagram;    QDataStream out(&datagram, QIODevice::WriteOnly);    out.setVersion(QDataStream::Qt_4_7);    out << QDateTime::currentDateTime() << qrand()/10000000.0 << qrand()/10000000.0 << qrand()/10000000.0 ;    //发送主机可以用QHostAddress("127.0.0.1")替换    udpSocket.writeDatagram(datagram, QHostAddress::LocalHost, 13999);  // UDP 发送数据}

------------------------------- Client 端 ------------------------------------

 udpclient.h

#ifndef UDPCLIENT_H#define UDPCLIENT_H#include 
#include
#include
class udpClient : public QDialog{ Q_OBJECT public: udpClient(QWidget *parent = 0); ~udpClient();private slots: void processPendingDatagrams();private: QUdpSocket udpSocket; QLabel *dateLabel; QLabel *timeLabel; QLabel *tempLabel; QLabel *humLabel; QLabel *altiLabel; QLineEdit *dateEdit; QLineEdit *timeEdit; QLineEdit *tempEdit; QLineEdit *humEdit; QLineEdit *altiEdit;};#endif // UDPCLIENT_H

udpclient.cpp

#include "udpclient.h"udpClient::udpClient(QWidget *parent)    : QDialog(parent){    udpSocket.bind(13999);   //建立监听    connect(&udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));   //接收准备    dateLabel = new QLabel(tr("Date"));    timeLabel = new QLabel(tr("Time"));    tempLabel = new QLabel(tr("Temp"));    humLabel  = new QLabel(tr("Hum"));    altiLabel = new QLabel(tr("Alti"));    dateEdit  = new QLineEdit();    timeEdit  = new QLineEdit();    tempEdit  = new QLineEdit();    humEdit   = new QLineEdit();    altiEdit  = new QLineEdit();    QGridLayout *gLayout = new QGridLayout;    gLayout->addWidget(dateLabel, 0, 0);    gLayout->addWidget(dateEdit, 0, 1);    gLayout->addWidget(timeLabel, 1, 0);    gLayout->addWidget(timeEdit, 1, 1);    gLayout->addWidget(tempLabel, 2, 0);    gLayout->addWidget(tempEdit, 2, 1);    gLayout->addWidget(humLabel, 3, 0);    gLayout->addWidget(humEdit, 3, 1);    gLayout->addWidget(altiLabel, 4, 0);    gLayout->addWidget(altiEdit, 4, 1);    setLayout(gLayout);}udpClient::~udpClient(){    }void udpClient::processPendingDatagrams(){    QByteArray datagram;    do{        datagram.resize(udpSocket.pendingDatagramSize());        udpSocket.readDatagram(datagram.data(), datagram.size());  //接收数据    }while( udpSocket.hasPendingDatagrams() );    QDateTime dateTime;    double temperature;    double humidity;    double altitude;    QDataStream in(&datagram, QIODevice::ReadOnly);    in.setVersion(QDataStream::Qt_4_7);    in >> dateTime >> temperature >> humidity >> altitude;    dateEdit->setText(dateTime.date().toString());    timeEdit->setText(dateTime.time().toString());    tempEdit->setText(tr("%1 C").arg(temperature));    humEdit->setText(tr("%1 %").arg(humidity));    altiEdit->setText(tr("%1 m").arg(altitude));}

 

 

 

转载于:https://www.cnblogs.com/xj626852095/p/3648129.html

你可能感兴趣的文章
UML和模式应用学习笔记(6)——系统顺序图、系统操作和层
查看>>
Android -- startActivityForResult和setResult
查看>>
1019 General Palindromic Number (20 分)
查看>>
关于c语言中指针的一些理解
查看>>
Expm 2_2 查找中项问题
查看>>
启动与关闭hadoop
查看>>
7.2 Move Field(搬移字段)
查看>>
[置顶] C#执行Excel宏模版的方法
查看>>
2015年9月28日JQuery提前预习预热笔记
查看>>
perl 删除过期文件
查看>>
document.write与document.getElementById的区别
查看>>
搜索可用docker镜像
查看>>
python基础知识梳理-----7函数
查看>>
函数极限的定义
查看>>
POJ 3684 Priest John&#39;s Busiest Day 2-SAT+输出路径
查看>>
oracle10g、oracle client和plsql devement 三者之间的关系
查看>>
ICDM评选:数据挖掘十大经典算法
查看>>
巧用「打印」功能实现PDF单页提取
查看>>
【转】Mongo初体验
查看>>
Java中synchronized和同步方法
查看>>