欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年07月27日 09:28:12   投稿:jingxian  
下面小編就為大家?guī)硪黄猀T網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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。直到點(diǎn)擊關(guān)閉,或者服務(wù)端停止。

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

相關(guān)文章

  • opencv利用視頻的前n幀求平均圖像

    opencv利用視頻的前n幀求平均圖像

    這篇文章主要為大家詳細(xì)介紹了opencv利用視頻的前n幀求平均圖像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • VisualStudio2010安裝教程

    VisualStudio2010安裝教程

    這篇文章通過圖文并茂的形式給大家介紹VisualStudio2010安裝教程,在日常開發(fā)中是必不可少的搭建過程,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • 基于MFC實現(xiàn)貪吃蛇小游戲

    基于MFC實現(xiàn)貪吃蛇小游戲

    這篇文章主要為大家詳細(xì)介紹了基于MFC實現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C語言實現(xiàn)通訊錄功能

    C語言實現(xiàn)通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 淺析直接插入排序與折半插入排序

    淺析直接插入排序與折半插入排序

    這篇文章主要介紹了直接插入排序與折半插入排序,有需要的朋友可以參考一下
    2013-12-12
  • 詳細(xì)理解函C語言的函數(shù)棧幀

    詳細(xì)理解函C語言的函數(shù)棧幀

    這篇文章主要為大家介紹了C語言的函數(shù)棧幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助,希望能夠給你帶來幫助
    2021-11-11
  • 大數(shù)據(jù)情況下桶排序算法的運(yùn)用與C++代碼實現(xiàn)示例

    大數(shù)據(jù)情況下桶排序算法的運(yùn)用與C++代碼實現(xiàn)示例

    在排序元素很多的情況下,其實桶排序的性能并不是太高,這里我們配合單鏈表的直接插入排序,來看下一大數(shù)據(jù)情況下桶排序算法的運(yùn)用與C++代碼實現(xiàn)示例:
    2016-07-07
  • C++實現(xiàn)智能柜管理系統(tǒng)

    C++實現(xiàn)智能柜管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)智能柜管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++詳細(xì)實現(xiàn)紅黑樹流程詳解

    C++詳細(xì)實現(xiàn)紅黑樹流程詳解

    今天我要跟大家介紹二叉搜索樹中的另一顆樹——紅黑樹,它主要是通過控制顏色來控制自身的平衡,但它的平衡沒有AVL樹的平衡那么嚴(yán)格
    2022-06-06
  • Qt?Creator配置opencv環(huán)境的全過程記錄

    Qt?Creator配置opencv環(huán)境的全過程記錄

    最近在PC端QT下配置opencv,想著以后應(yīng)該會用到,索性記錄下,這篇文章主要給大家介紹了關(guān)于Qt?Creator配置opencv環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2022-05-05

最新評論