QT?UDP網(wǎng)絡(luò)編程實(shí)現(xiàn)簡(jiǎn)單消息傳輸
本文實(shí)例為大家分享了QT UDP實(shí)現(xiàn)簡(jiǎn)單消息傳輸?shù)木唧w代碼,供大家參考,具體內(nèi)容如下
這幾天看了下Qt的udp,順便實(shí)現(xiàn)了下簡(jiǎn)單的消息傳輸,看起來(lái)比較簡(jiǎn)單。
UDP服務(wù)器:
截圖如下:

代碼:
server.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QUdpSocket>
#include<QtNetwork>
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
#include<QGridLayout>
#include<QTimer>
class Widget : public QWidget
{
? ? Q_OBJECT
public:
? ? Widget(QWidget *parent = 0);
? ? ~Widget();
protected:
? ? QTimer* timer;
? ? QImage* image;
private slots:
? ? void send();
? ? void bordcaststart();
private:
? ? QPushButton* start_ptn;
? ? QPushButton* close_ptn;
? ? QPushButton* send_ptn;
? ? QLabel* label;
? ? QLineEdit* edit;
? ? QLabel* image_label;
? ? QGridLayout* layout;
? ? QUdpSocket* udpsocket;
};
#endif // WIDGET_Hserver.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
{
? ? start_ptn=new QPushButton("start");
? ? close_ptn=new QPushButton("quit");
? ? send_ptn=new QPushButton("send");
? ? label=new QLabel;
? ? label->setText("this is test!");
? ? edit=new QLineEdit;
? ? layout=new QGridLayout(this);
? ? layout->addWidget(label);
? ? layout->addWidget(edit);
? ? layout->addWidget(send_ptn,1,1);
? ? layout->addWidget(start_ptn,2,0);
? ? layout->addWidget(close_ptn,2,1);
? ? this->resize(400,400);
? ? timer=new QTimer(this);
? ? udpsocket=new QUdpSocket(this);
? ? connect(start_ptn,SIGNAL(clicked(bool)),this,SLOT(bordcaststart()));
? ? connect(close_ptn,SIGNAL(clicked(bool)),this,SLOT(close()));
// ? ?connect(timer,SIGNAL(timeout()),this,SLOT(send()));
}
Widget::~Widget()
{
}
void Widget::send()
{
? ? QByteArray datagram= "Broadcast message " +edit->text().toUtf8();
? ? udpsocket->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,45454);
}
void Widget::bordcaststart()
{
? ?// timer->start(1000);
? ? start_ptn->setDisabled(true);
? ? connect(send_ptn,SIGNAL(clicked(bool)),this,SLOT(send()));
}客戶端
截圖:

client.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QUdpSocket>
#include<QtNetwork>
#include<QLabel>
#include<QTimer>
#include<QPushButton>
#include<QGridLayout>
class Widget : public QWidget
{
? ? Q_OBJECT
public:
? ? Widget(QWidget *parent = 0);
? ? ~Widget();
protected:
private:
? ? QGridLayout* layout;
? ? QPushButton* quit_ptn;
? ? QLabel* label;
? ? QUdpSocket* udpsocket;
private slots:
? ? void boarcast();
};
#endif // WIDGET_Hclient.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
{
? ? label=new QLabel;
? ? quit_ptn=new QPushButton("quit");
? ? layout=new QGridLayout(this);
? ? layout->addWidget(label);
? ? layout->addWidget(quit_ptn);
? ? this->resize(200,200);
? ? udpsocket=new QUdpSocket(this);
? ? udpsocket->bind(45454, QUdpSocket::ShareAddress);
? ? connect(udpsocket,SIGNAL(readyRead()),this,SLOT(boarcast()));
? ? connect(quit_ptn,SIGNAL(clicked(bool)),this,SLOT(close()));
}
Widget::~Widget()
{
}
void Widget::boarcast()
{
? ? while (udpsocket->hasPendingDatagrams()) {
? ? ? ? ? ? QByteArray datagram;
? ? ? ? ? ? datagram.resize(udpsocket->pendingDatagramSize());
? ? ? ? ? ? QHostAddress sender;
? ? ? ? ? ? quint16 senderPort;
? ? ? ? ? ? udpsocket->readDatagram(datagram.data(), datagram.size(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? &sender, &senderPort);
? ? ? ? ? ? label->setText(datagram);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--舉例及編程中的心得
本文主要講解DSP浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算舉例及編程中的心得 ,具有參考價(jià)值,需要的朋友可以參考一下。2016-06-06
C++開(kāi)發(fā)之PugiXML庫(kù)基礎(chǔ)用法示例詳解
PugiXML庫(kù)是一個(gè)功能強(qiáng)大、簡(jiǎn)單易用的C++ XML解析庫(kù),它提供了一組方便的函數(shù)來(lái)解析、創(chuàng)建和修改XML文檔,本文介紹了如何使用PugiXML庫(kù)來(lái)解析、創(chuàng)建和修改XML文檔,以及如何處理錯(cuò)誤和異常,感興趣的朋友跟隨小編一起看看吧2024-03-03
詳解C語(yǔ)言中scanf函數(shù)使用的一些注意點(diǎn)
這篇文章主要介紹了C語(yǔ)言中scanf函數(shù)使用的一些注意點(diǎn),scanf函數(shù)的使用是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-04-04
C++實(shí)現(xiàn)企業(yè)職工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)企業(yè)職工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語(yǔ)言實(shí)現(xiàn)單詞小助手改進(jìn)版
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)單詞小助手的改進(jìn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細(xì)介紹
本篇文章是對(duì)wprintf 中使用%I64d格式化輸出LONGLONG進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++異步數(shù)據(jù)交換實(shí)現(xiàn)方法介紹
這篇文章主要介紹了C++異步數(shù)據(jù)交換實(shí)現(xiàn)方法,異步數(shù)據(jù)交換,除了阻塞函數(shù) send() 和 recv() 之外,Boost.MPI 還支持與成員函數(shù) isend() 和 irecv() 的異步數(shù)據(jù)交換2022-11-11

