06_QT网络编程之UDP通信

QT网络编程之UDP通信

udp编程

​ udp不分客户端和服务器,只需要使用一个类QUdpSocket。

代码

Udp.pro

#-------------------------------------------------
#
# Project created by QtCreator 2024-04-13T23:07:41
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Udp
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        widget.cpp

HEADERS += \
        widget.h

FORMS += \
        widget.ui

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QUdpSocket>
#include <QString>
#include <QMessageBox>
#include <QHostAddress>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

    QUdpSocket *udpSocket;

private slots:
    void on_openBt_clicked();
    void readyRead_slot();

    void on_sendBt_clicked();

    void on_closeBt_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    udpSocket = new QUdpSocket(this);


}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_openBt_clicked()
{
    bool flag;
    flag = udpSocket->bind(ui->localPort->text().toUInt());
    if(flag)
    {
        QMessageBox::information(this, "提示", "成功");
    }else
    {
        QMessageBox::information(this, "提示", "失败");
    }
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_slot()));
}

void Widget::readyRead_slot()
{
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray array;
        array.resize(udpSocket->pendingDatagramSize()); //将array调整到和udpsocket中没读完的数据一样大小

        udpSocket->readDatagram(array.data(), array.size());

        ui->receiveEdit->appendPlainText(array.data());
    }
}

void Widget::on_sendBt_clicked()
{
    QString buff;
    QHostAddress address;

    buff = ui->sendEdit->text();
    address.setAddress(ui->targetIp->text());
    udpSocket->writeDatagram(buff.toLocal8Bit().data(), buff.length(), address, ui->targetPort->text().toUInt());
}

void Widget::on_closeBt_clicked()
{
    udpSocket->close();
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>451</width>
    <height>481</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QGroupBox" name="groupBox">
       <property name="title">
        <string>接收框</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPlainTextEdit" name="receiveEdit">
       <property name="readOnly">
        <bool>true</bool>
       </property>
      </widget>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QLabel" name="label">
         <property name="text">
          <string>本地端口:</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLineEdit" name="localPort"/>
       </item>
       <item>
        <widget class="QLabel" name="label_2">
         <property name="text">
          <string>目标端口:</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLineEdit" name="targetPort"/>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <widget class="QLabel" name="label_3">
         <property name="text">
          <string>目标IP:</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLineEdit" name="targetIp"/>
       </item>
      </layout>
     </item>
     <item>
      <widget class="QGroupBox" name="groupBox_2">
       <property name="title">
        <string>发送框</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="sendEdit"/>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QPushButton" name="openBt">
         <property name="text">
          <string>打开</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="closeBt">
         <property name="text">
          <string>关闭</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer_2">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="sendBt">
         <property name="text">
          <string>发送</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

posted @ 2024-04-17 17:05  爱吃冰激凌的黄某某  阅读(16)  评论(0编辑  收藏  举报