国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)

2020-01-26 13:59:56
字體:
供稿:網(wǎng)友

QT有封裝好的UDP協(xié)議的類,QUdpSocket,里面有我們想要的函數(shù)接口。感興趣的話,可以看看。

先搞服務(wù)端吧,寫一個(gè)子類,繼承QDialog類,起名為UdpServer類。頭文件要引用我們上邊說的QUdpSocket這個(gè)類,還有我們想要的布局的類。

#ifndef UDPSERVER_H#define UDPSERVER_H#include <QDialog>#include <QLabel>#include <QLineEdit>#include <QPushButton>#include <QVBoxLayout>#include <QtNetwork/QUdpSocket>#include <QtNetwork/QHostAddress>#include <QTimer>class UdpServer : public QDialog{ Q_OBJECTpublic: UdpServer(QWidget *parent = 0,Qt::WindowFlags f= 0); ~UdpServer();private: QLabel * TimerLabel; QLineEdit * TextLineEdit; QPushButton* StartBtn; QVBoxLayout * mainLayout; public slots: void StartBtnClicked(); void timeout(); private: int port; bool isStarted; QUdpSocket * udpSocket; QTimer *timer;};#endif // UDPSERVER_H

在.cpp文件里,我們先是把界面顯示出來,然后用udp的writedategram把想要傳的寫進(jìn)去。

#include "udpserver.h"UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f) : QDialog(parent,f){ setWindowTitle(tr("UDP SERVER")); TimerLabel = new QLabel(tr("show time:"),this); TextLineEdit = new QLineEdit(this); StartBtn = new QPushButton(tr("start"),this); mainLayout = new QVBoxLayout(this); mainLayout-> addWidget(TimerLabel); mainLayout-> addWidget(TextLineEdit); mainLayout-> addWidget(StartBtn); connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked())); port = 5555; isStarted = false; udpSocket = new QUdpSocket(this); timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));}UdpServer::~UdpServer(){}void UdpServer::StartBtnClicked(){ if(!isStarted) {  StartBtn->setText(tr("STOP"));  timer->start(1000);  isStarted = true; } else {  StartBtn->setText(tr("BEGIN"));  isStarted = false;  timer->stop(); }}void UdpServer::timeout(){ QString msg = TextLineEdit->text(); int length=0; if(msg=="") {  return; } if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length()) {  qDebug() << msg.toLatin1();  return; }}

我這里用qDebug把要傳的東西打印出來,進(jìn)行測試,看看是否傳過去了。

客戶端:

#ifndef UDPCLIENT_H#define UDPCLIENT_H#include <QDialog>#include <QVBoxLayout>#include <QTextEdit>#include <QPushButton>#include <QtNetwork/QUdpSocket> class UdpClient : public QDialog{ Q_OBJECT public: UdpClient(QWidget *parent = 0); ~UdpClient(); private: QTextEdit* ReceiceTextEdit; QPushButton* CloseBtn; QVBoxLayout* mainLayout; public slots: void CloseBtnClicked(); void dataReceived(); private: int port; QUdpSocket* udpSocket;};#endif // UDPCLIENT_H

客戶端很簡單,怎么實(shí)現(xiàn)布局,我就不多說了,主要是dataReceive函數(shù)。

#include "udpclient.h"#include <QMessageBox>#include <QHostAddress>UdpClient::UdpClient(QWidget *parent) :QDialog(parent){ setWindowTitle("UDP CLIENT"); ReceiceTextEdit = new QTextEdit(this); CloseBtn = new QPushButton(tr("Close"),this); mainLayout = new QVBoxLayout(this); mainLayout->addWidget(ReceiceTextEdit); mainLayout->addWidget(CloseBtn); connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked())); port =5555; udpSocket = new QUdpSocket(this); bool result = udpSocket->bind(port); if(!result) {  QMessageBox::information(this,tr("ERROR"),tr("connect error"));  return; } connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));} UdpClient:: ~UdpClient(){}void UdpClient::CloseBtnClicked(){ close();}void UdpClient::dataReceived(){ while(udpSocket->hasPendingDatagrams()) {  QByteArray datagram;  datagram.resize(udpSocket->pendingDatagramSize());  udpSocket->readDatagram(datagram.data(),datagram.size());  QString msg=datagram.data();  ReceiceTextEdit->insertPlainText(msg); }}

最后顯示一下界面,服務(wù)端發(fā)送hello。

客戶端收到的:

不停的在打印hello。直到點(diǎn)擊關(guān)閉,或者服務(wù)端停止。

以上這篇QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 铜川市| 金湖县| 循化| 弥勒县| 巴塘县| 香格里拉县| 无极县| 嘉兴市| 綦江县| 新建县| 普洱| 田阳县| 凤冈县| 敖汉旗| 澄迈县| 冷水江市| 丹寨县| 保靖县| 偏关县| 龙胜| 松江区| 察哈| 内丘县| 延庆县| 东兴市| 梨树县| 修武县| 芮城县| 八宿县| 江源县| 瑞安市| 曲周县| 内丘县| 稷山县| 新建县| 九龙城区| 衡山县| 新野县| 漳州市| 泰州市| 五常市|