QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實例講解)
QT有封裝好的UDP協(xié)議的類,QUdpSocket,里面有我們想要的函數(shù)接口。感興趣的話,可以看看。
先搞服務(wù)端吧,寫一個子類,繼承QDialog類,起名為UdpServer類。頭文件要引用我們上邊說的QUdpSocket這個類,還有我們想要的布局的類。
#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文件里,我們先是把界面顯示出來,然后用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
客戶端很簡單,怎么實現(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。直到點擊關(guān)閉,或者服務(wù)端停止。
以上這篇QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:C++數(shù)據(jù)結(jié)構(gòu)之文件壓縮(哈夫曼樹)實例詳解
欄 目:C語言
本文標(biāo)題:QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實例講解)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1319.html
您可能感興趣的文章
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10深入解析Linux下\r\n的問題
- 01-10Linux C 獲取進(jìn)程退出值的實現(xiàn)代碼
- 01-10解析Linux下的時間函數(shù):設(shè)置以及獲取時間的方法
- 01-10深入探討linux下進(jìn)程的最大線程數(shù)、進(jìn)程最大數(shù)、進(jìn)程打開的文
- 01-10基于linux下獲取時間函數(shù)的詳解
- 01-10深入sizeof的使用詳解
- 01-10Linux下semop等待信號時出現(xiàn)Interrupted System Call錯誤(EINTR)解決方法
- 01-10深入解析C中的數(shù)值與真假
- 01-10用c語言實現(xiàn)冒泡排序,選擇排序,快速排序


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法


