#QT(网络编程-UDP)

1.IDE:QTCreator


2.实验:UDP

不分客户端和服务端


3.记录

(1)做一个UI界面

 (2)编写open按钮代码进行测试(用网络调试助手测试)

 

(3)完善其他功能测试


4.代码

pro

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QUdpSocket>         //
#include <QString>           //
#include <QHostAddress>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QUdpSocket *udpsocket;    //
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_open_pb_clicked();
    void readyRead_slot();
    void on_close_pb_clicked();

    void on_send_pb_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>        //
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    udpsocket = new QUdpSocket(this);   //
}

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

void Widget::on_open_pb_clicked()     //打开按钮按下处理函数
{
    if(udpsocket->bind(ui->local_port->text().toUInt())==true)      //连接成功
     {
        QMessageBox::information(this,"提示","连接成功");
     }
    else
     {
        QMessageBox::critical(this,"警告","连接失败");
     }
    connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_slot()));
}

void Widget::readyRead_slot()         //准备读关联函数
{
    while (udpsocket->hasPendingDatagrams()) {
        QByteArray array;
        array.resize(udpsocket->pendingDatagramSize());
        udpsocket->readDatagram(array.data(),array.size());
        QString buf;
        buf=array.data();
        ui->receive_line->appendPlainText(buf);
    }
}


void Widget::on_close_pb_clicked()         //关闭按钮按下时
{
    udpsocket->close();
}


void Widget::on_send_pb_clicked()          //发送按钮按下时
{
    quint16 port;
    QString sendbuff;
    QHostAddress address;
    address.setAddress(ui->des_ip->text());      //设置目标IP
    sendbuff=ui->send_line->text();     //发送内容存入数组
    port=ui->des_port->text().toUInt();                  //目标端口赋值
    udpsocket->writeDatagram(sendbuff.toLocal8Bit().data(),sendbuff.length(),address,port);    //udp向指定的IP地址的指定端口发送数据
}

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值