溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信的示例分析

發(fā)布時(shí)間:2021-09-16 09:50:39 來(lái)源:億速云 閱讀:130 作者:柒染 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信的示例分析,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

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

#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_OBJECT
public:
 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文件里,我們先是把界面顯示出來(lái),然后用udp的writedategram把想要傳的寫(xiě)進(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把要傳的東西打印出來(lái),進(jìn)行測(cè)試,看看是否傳過(guò)去了。

客戶(hù)端:

#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

客戶(hù)端很簡(jiǎn)單,怎么實(shí)現(xiàn)布局,我就不多說(shuō)了,主要是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。

QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信的示例分析

客戶(hù)端收到的:

QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信的示例分析

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

看完上述內(nèi)容,你們對(duì)QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信的示例分析有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI