UDP发送端和服务端的收发数据

发送端:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QUdpSocket;
class QPushButton;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void onBroadcastButtonClicked();
    void receiveMessageShow();
private:
    QUdpSocket *sender_;
    QPushButton *broadcast_button_;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QPushButton>
#include <QtNetwork>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(800,600);
    broadcast_button_ = new QPushButton(this);
    broadcast_button_->setText(QString::fromLocal8Bit("开始广播"));

    sender_ = new QUdpSocket(this);
    connect(broadcast_button_, &QPushButton::clicked, this, &MainWindow::onBroadcastButtonClicked);
    connect(sender_, &QUdpSocket::readyRead, this, &MainWindow::receiveMessageShow);
}

MainWindow::~MainWindow()
{
}

void MainWindow::onBroadcastButtonClicked()
{
    QByteArray datagram = "hello world!";
    //发送size大小的数据报data到地址为QHostAddress::Broadcast的主机的45454端口
    //成功返回发送的字节数,失败返回-1
    //如果数据报过大,这个函数将会返回-1,而且error()函数会返回DatagramTooLargeError错误信息
    sender_->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);
}

//等待接受服务端的数据
void MainWindow::receiveMessageShow()
{
    while(sender_->waitForReadyRead()){
        QByteArray datagram;
        if (sender_->hasPendingDatagrams()) {
            datagram.resize(sender_->pendingDatagramSize());
            sender_->readDatagram(datagram.data(), datagram.size());
            qDebug() << datagram <<"1211111111";
            break;
        }
    }
}

服务端:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QUdpSocket;
class QLabel;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void processPendingDataGram();
private:
    QUdpSocket *receiver_;
    QLabel *content_show_label_;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"

#include <QtNetwork>
#include <QLabel>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(800,600);
    content_show_label_ = new QLabel(this);
    content_show_label_->setGeometry(0, 0, 200,40);

    receiver_ = new QUdpSocket(this);
    //绑定Ip地址和端口号,默认支持所有IPv4的IP地址
    //其中第二个参数绑定模式,表明允许其他服务器绑定到相同的地址和端口上
    receiver_->bind(45454,QUdpSocket::ShareAddress);
    //每当有数据报到来,QUdpSocket都会发射readyRead信号
    connect(receiver_, &QUdpSocket::readyRead, this, &MainWindow::processPendingDataGram);
}

MainWindow::~MainWindow()
{
}

void MainWindow::processPendingDataGram()
{
    QHostAddress sender_address;
    quint16 sender_port;
    //使用hasPendingDatagrams来判断是否还有等待读取的数据报
    while(receiver_->hasPendingDatagrams()){
        QByteArray datagram;
        //让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据
        datagram.resize(receiver_->pendingDatagramSize());

        //接收数据报,将其存放到datagram中
        receiver_->readDatagram(datagram.data(), datagram.size(), &sender_address, &sender_port);
        content_show_label_->setText(datagram);
        
        //处理数据
        
        //像发送端返回数据
        QByteArray response = "Hello, client!";
        receiver_->writeDatagram(response, sender_address, sender_port);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值