Qt開發的UDP接收端

    上一篇已經說完了簡單UDP發送端開發,參見個人博客:Qt開發的UDP發送端。windows

    博文地址:https://my.oschina.net/marshal2bit/blog/803803app

    作完UDP發送端,我又嘗試作了一個UDP接收端。先上圖,作出來的樣子以下:socket

    

    1、開發環境(和上篇博文同樣)函數

    系統:Windows 7 專業版(64位)ui

    軟件:qt-opensource-windows-x86-mingw530-5.7.0(原來用Qt 5.2.1+完成的工程)this

    注:Qt下載連接http://download.qt.io/official_releases/qt/spa

    2、新建工程.net

    見文章開頭的博客連接。注意修改.pro文件  設計

#-------------------------------------------------
#
# Project created by QtCreator 2016-12-03T18:32:29
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = udpEndpoint2
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

    新建好的工程界面以下:code

    

    3、設計ui界面

    進入「widget.ui」,設計以下界面:

    

控件信息見下表:  

控件標號 控件類別 控件名稱(objectName)
1 QLabel label_4
2 QLineEdit lineEdit
3 QLabel label
4 QTextBrowser dataRecv
5 QLabel label_2
6 QTextBrowser fromIP
7 QLabel label_3
8 QTextBrowser fromPort
9 QPushButton pushButton

    4、ui界面的「信號-槽」函數設置

    

    5、代碼編輯

    (一)編輯「widget.h」,編輯以下:

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtNetwork/QUdpSocket>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void processPendingDatagram();
    void on_pushButton_clicked();

    void on_lineEdit_editingFinished();

private:
    QUdpSocket *receiver;
    Ui::Widget *ui;
};

#endif // WIDGET_H

(二)編輯「widget.cpp」,以下:

//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QtNetwork/QUdpSocket"
#include "QtNetwork/QHostAddress"
#include "QTextCodec"

QRegExp regExpIP("((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])[\\.]){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])");
QRegExp regExpNetPort("((6553[0-5])|[655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[0-9])");
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("UDP接收端");
    ui->lineEdit->setValidator\
            (new QRegExpValidator(regExpNetPort,this));
    ui->label->setText("等待接收數據……");
    ui->pushButton->setText("清空");

    receiver = new QUdpSocket(this);
    //receiver->bind(8083);  //端口8083接收
    connect(receiver,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));
}
//接收數據
void Widget::processPendingDatagram()
{
    QHostAddress *sourceIP = new QHostAddress();
    quint16 sourcePort;
    while(receiver->hasPendingDatagrams())
    {
        QByteArray datagram;
        QString dataStr;

        QTextCodec *tutf=QTextCodec::codecForName("UTF-8");  //顯示中文
        datagram.resize(receiver->pendingDatagramSize());
        receiver->readDatagram(datagram.data(),datagram.size(),\
                               sourceIP,&sourcePort);    //接收
        dataStr = tutf->toUnicode(datagram);  //接收數據格式化以便於中文顯示
        ui->label->setText("接收到的數據");
        //ui->dataRecv->setText(dataStr);
        ui->dataRecv->append(dataStr);
        ui->fromIP->setText(sourceIP->toString());  //查看數據源IP地址
        ui->fromPort->setText(QString::number(sourcePort, 10));  //查看數據源端口
    }
    delete sourceIP;
}
Widget::~Widget()
{
    delete ui;
}

//清空數據
void Widget::on_pushButton_clicked()
{
    if(ui->label->text() == "接收到的數據")
        ui->label->setText("已清空,等待新數據到來……");
}

void Widget::on_lineEdit_editingFinished()
{
    QString inputPort = ui->lineEdit->text();
    quint16 local_port;
    receiver->abort();   //復位socket
    //處理端口輸入
    if(inputPort.isEmpty())
        local_port = -1;
    else
        local_port = inputPort.toUInt();   //QString 文本輸入,轉換爲quint16的數字端口
    receiver->bind(local_port);     //綁定輸入端口
}

main.cpp」無需修改

//main.cpp
#include "widget.h"
#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    w.show();
    return a.exec();
}

6、程序運行

運行界面如文章開頭所示,使用實例與開頭所提的博客對應。

用8083端口接收本機8082發送端發過來的數據,本機IP:10.56.195.19。運行前設置

點擊發送後,運行結果

    在Qt 5.2.1中,源IP只顯示IP地址:10.56.195.19,到了5.7.0不知道怎麼會出來::ff:這個東西,有時間再看看,知道緣由的博友也請指教指教。

    到此爲止,UDP發送端和接收端開發已經講完。

    (over)

相關文章
相關標籤/搜索