Qt development - Network Programming UDP Client

Today is New Year's Day, I wish you all a Happy New Year!


table of Contents

Renderings

Manual Readings

bind binding:

readRead():

Routine

udpclient.h

udpclient.cpp


Renderings

And an upper end service in conjunction with the transceiver

Manual Readings

bind binding:

For UDP nested keywords, after binding, whenever a UDP data table reaches the specified address or designated port, readRead () signal will be transmitted!

readRead():

Each time new data is read from the current read channel device, this signal will be issued. It is sent only when new data is available again, for example when a new payload network data arrives at the network sockets, or when a new block of data when attached to the device.

This signal can not be recursive transmit (what signals can transmit a recursive ??)

Note If you re-enter the event loop or call waitForReadyRead () in a tank connected to readyRead () signal, the signal will not be re-issued (although waitForReadyRead () may still return true).

If you re-enter the event loop in a tank connected to readyRead () signal or call waitForReadyRead (), the signal will not be re-issued (although waitForReadyRead () may still return true).

Routine

udpclient.h

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>
#include <QMessageBox>
#include <QHostAddress>

class UDPClient : public QDialog
{
    Q_OBJECT

public:
    UDPClient(QWidget *parent = nullptr);
    ~UDPClient();

public slots:
    void CloseBtnClicked();
    void dataRecieved();

private:
    //布局控件
    QTextEdit *ReciveTextEdit;
    QPushButton *CloseBtn;
    QVBoxLayout *mainLayout;
    //网络控件
    int port;
    QUdpSocket *udpSocket;

};

#endif // UDPCLIENT_H

udpclient.cpp

#include "udpclient.h"

UDPClient::UDPClient(QWidget *parent)
    : QDialog(parent)
{
    setWindowIcon(QIcon("icon.png"));
    setWindowTitle(QStringLiteral("UDP客户端"));
    //布局设计
    ReciveTextEdit = new QTextEdit(this);
    CloseBtn = new QPushButton(this);
    CloseBtn->setText(QStringLiteral("关闭"));
    mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(ReciveTextEdit);
    mainLayout->addWidget(CloseBtn);
    //关闭事件关联
    connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked()));
    //网络参数设定,UDP端口参数设定
    quint16 port = 5555;
    udpSocket = new QUdpSocket(this);
    //数据事件关联
    connect(udpSocket,SIGNAL(readRead()),this,SLOT(dataRecived()));
    bool result = udpSocket->bind(port);//绑定到指定的端口上
    if(!result){//如果错误返回
        QMessageBox::information(this,QStringLiteral("错误!"),QStringLiteral("UDP出现了错误!"));
        return;
    }
    
}

UDPClient::~UDPClient()
{

}

//关闭按键事件
void UDPClient::CloseBtnClicked(){
    close();
}

void UDPClient::dataRecieved(){
    while (udpSocket->hasPendingDatagrams()) {//判断数据报表是否有效
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(),datagram.size());
        QString msg = datagram.data();
        ReciveTextEdit->insertPlainText(msg);//写入返回的数据
    }
    
    
}

 

Published 242 original articles · won praise 237 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_41895747/article/details/104080939