Qt-网络与通信-UDP网络通讯

用户数据报协议是一种简单的轻量级、不可靠、面向数据、无连接的传出层协议,可以应用于在可靠性不是十分重要的场合,如短消息,广播信息等。

例如一下场合

网络数据大多为短消息

拥有大量客户端

对数据安全性无特殊要求

网络负担飞常重,但对响应速度要求高

 

示例截图

 

服务器代码

.h

#ifndef UDPSERVER_H
#define UDPSERVER_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UdpServer : public QDialog
{
    Q_OBJECT

public:
    UdpServer(QWidget *parent = 0);
    ~UdpServer();

public slots:
    void StartBtnClicked();
    void timeout();
private:
    QLabel *TimerLabel;
    QLineEdit *TextLineEdit;
    QPushButton *StartBtn;
    QVBoxLayout *mainLayout;

    int port;
    bool isStarted;
    QUdpSocket *udpSocket;
    QTimer *timer;

};

#endif // UDPSERVER_H

 

.cpp

 

#include "udpserver.h"

UdpServer::UdpServer(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UDP Server"));
    TimerLabel = new QLabel(tr("计时器:"),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(bool)),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("Stop");
        timer->start(1000);
        isStarted = true;
    }
    else
    {
        StartBtn->setText("Start");
        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())
    {
        return;
    }
}

 

客户端代码

.h

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>
class UdpClient : public QDialog
{
    Q_OBJECT

public:
    UdpClient(QWidget *parent = 0);
    ~UdpClient();
public slots:
    void CloseBtnClicked();
    void dataReceived();
private:
    QTextEdit *ReceiveTextEdit;
    QPushButton *CloseBtn;
    QVBoxLayout *mainLayout;

    int port;
    QUdpSocket *udpSocket;
};

#endif // UDPCLIENT_H

 

.cpp

#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("UODClient");
    ReceiveTextEdit = new QTextEdit(this);
    CloseBtn = new QPushButton("close",this);

    mainLayout = new QVBoxLayout(this);

    mainLayout->addWidget(ReceiveTextEdit);
    mainLayout->addWidget(CloseBtn);


    connect(CloseBtn,SIGNAL(clicked(bool)),this,SLOT(CloseBtnClicked()));
    port = 5555;
    udpSocket = new QUdpSocket(this);

    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));

    bool result = udpSocket->bind(port);
    if(!result)
    {
        QMessageBox::information(this,"Error","udp socket create error!");
        return;
    }
}

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();
        ReceiveTextEdit->insertPlainText(msg);
    }

}

 

 

工程连接:https://gitee.com/DreamLife-Technology_DreamLife/UDPProject

 

 

posted @ 2018-02-28 21:55  DreamDog  阅读(488)  评论(0编辑  收藏  举报