最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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_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

客戶端很簡單,怎么實(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言實(shí)現(xiàn)學(xué)籍管理系統(tǒng)

    C語言實(shí)現(xiàn)學(xué)籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++ 中const 類型限定符不兼容問題

    C++ 中const 類型限定符不兼容問題

    這篇文章主要介紹了C++ 中const 類型限定符不兼容問題的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • C語言解3元1次方程組 用初中學(xué)的最基本的聯(lián)合消元法

    C語言解3元1次方程組 用初中學(xué)的最基本的聯(lián)合消元法

    最近就想自己能不能先寫個(gè)算線性方程組的程序呢?后來就想了這么個(gè)方法,暫時(shí)只能算3元的,任意元的接下來繼續(xù)想。有太多硬編碼,希望有興趣的讀者可以給點(diǎn)修改建議
    2013-11-11
  • 深入分析C++中deque的使用

    深入分析C++中deque的使用

    本篇文章介紹了,深入分析C++中deque的使用。需要的朋友參考下
    2013-05-05
  • C語言實(shí)現(xiàn)推箱子小游戲

    C語言實(shí)現(xiàn)推箱子小游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng)

    C++實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++深入探究list的模擬實(shí)現(xiàn)

    C++深入探究list的模擬實(shí)現(xiàn)

    list相較于vector來說會顯得復(fù)雜,它的好處是在任意位置插入,刪除都是一個(gè)O(1)的時(shí)間復(fù)雜度,本文主要介紹了C++中List的模擬實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 初識C++的const關(guān)鍵字,常量與常變量

    初識C++的const關(guān)鍵字,常量與常變量

    這篇文章主要為大家詳細(xì)介紹了C++的const關(guān)鍵字,常量與常變量,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++11智能指針中的 unique_ptr實(shí)例詳解

    C++11智能指針中的 unique_ptr實(shí)例詳解

    unique是獨(dú)特的、唯一的意思,故名思議,unique_ptr可以“獨(dú)占”地?fù)碛兴赶虻膶ο?,它提供一種嚴(yán)格意義上的所有權(quán)。這篇文章主要介紹了C++11智能指針中的 unique_ptr實(shí)例詳解,需要的朋友可以參考下
    2020-06-06
  • VisualStudio2022打包項(xiàng)目文件為.exe安裝包

    VisualStudio2022打包項(xiàng)目文件為.exe安裝包

    本文主要介紹了VisualStudio2022打包項(xiàng)目文件為.exe安裝包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論

克什克腾旗| 体育| 垫江县| 铜陵市| 荣昌县| 来安县| 常宁市| 奉新县| 内黄县| 昭平县| 平邑县| 安福县| 水城县| 肥乡县| 分宜县| 泰宁县| 贵州省| 桐城市| 和平区| 新竹市| 拉萨市| 乐山市| 武夷山市| 卢湾区| 张家口市| 马边| 仲巴县| 黔江区| 长垣县| 全椒县| 镶黄旗| 吉木萨尔县| 乌兰察布市| 大化| 扶风县| 中阳县| 大渡口区| 泰来县| 佛坪县| 运城市| 获嘉县|