Qt UDPDemo

在这里插入图片描述
udp的连接相对简单,不需要特别区分客户端和服务端,只需要指定ip和port就行

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>
#include <QUdpSocket>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pbtn_connect_clicked();

    void on_pbtn_disconnect_clicked();

    void on_pbtn_clear_clicked();

    void on_pbtn_exit_clicked();

    void on_pbtn_findip_clicked();

    QString getlocalip();

    void readPendingDatagrams();


    void on_pbtn_send_clicked();

    void on_pbtn_broadcast_clicked();

private:
    Ui::MainWindow *ui;
    QUdpSocket *udpsocket;

signals:
    void readyRead();
};
#endif // MAINWINDOW_H

函数实现:

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    udpsocket = new QUdpSocket(this);
}

MainWindow::~MainWindow()
{
    delete ui;
    udpsocket->abort();
    delete udpsocket;
}

void MainWindow::on_pbtn_connect_clicked()
{
    QString ip = ui->lineEdit_ip->text();
    quint16 port = ui->bind_port->text().toUShort();
    udpsocket->bind(port);//绑定IP地址和端口号
    connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));//接收数据
    ui->textEdit_display->append("连接成功");
}


void MainWindow::on_pbtn_disconnect_clicked()
{
    udpsocket->abort();//断开连接
    ui->textEdit_display->append("断开连接");
}


void MainWindow::on_pbtn_clear_clicked()
{
    ui->textEdit_display->clear();//清空显示区域
}


void MainWindow::on_pbtn_exit_clicked()
{
    exit(0);//退出程序
}


void MainWindow::on_pbtn_findip_clicked()
{
    ui->textEdit_display->append(getlocalip());//显示本机IP地址
    ui->lineEdit_ip->setText(getlocalip());
}

QString MainWindow::getlocalip()
{
    QString localip;
    //获取本机IPv4地址
    QString localHostName = QHostInfo::localHostName();//本地主机名
    QHostInfo info = QHostInfo::fromName(localHostName);//主机信息
    QList<QHostAddress> list = info.addresses();//地址列表
    foreach(QHostAddress address, list){
        if(address.protocol() == QAbstractSocket::IPv4Protocol){
            localip = address.toString();
        }
    }
    return localip;
}

void MainWindow::readPendingDatagrams()
{
    while(udpsocket->hasPendingDatagrams()){
        QByteArray datagram;
        datagram.resize(udpsocket->pendingDatagramSize());
        QHostAddress peerAddr;
        quint16 peerPort;
        udpsocket->readDatagram(datagram.data(),datagram.size(),&peerAddr,&peerPort);
        QString str = datagram.data();
        
        QString peer = "[From " + peerAddr.toString() + ":" + QString::number(peerPort) + "]";

        ui->textEdit_display->append(peer + str);
                
    }
}

void MainWindow::on_pbtn_send_clicked()
{
    QString message = ui->lineEdit_message->text();
    QString ip = ui->lineEdit_ip->text();
    quint16 port = ui->global_port->text().toUShort();
    QByteArray datagram = message.toUtf8();
    udpsocket->writeDatagram(datagram,QHostAddress(ip),port);
    ui->textEdit_display->append("发送成功");
}


void MainWindow::on_pbtn_broadcast_clicked()
{
    QString message = ui->lineEdit_message->text();
    quint16 port = ui->global_port->text().toUShort();
    QByteArray datagram = message.toUtf8();
    udpsocket->writeDatagram(datagram,QHostAddress::Broadcast,port);
    ui->textEdit_display->append("广播成功");
}


github地址:

https://github.com/ggstar00/QtUdpDemo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值