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));}