QT_Socket_udp

本文介绍了一个使用QTSocket实现的基于UDP的小型应用程序,包括MyWidget、Udp1和Udp2三个组件,分别展示了客户端和服务端的功能,并通过定时器和UDP套接字进行数据的发送和接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个基于QT Socket的udp小程序

有Udp1.cpp,Udp2.cpp,MyWidget.cpp。

下面先看

MyWidget.cpp

#include "MyWidget.h"
#include <QFile>
#include <QApplication>
#include <QDebug>
#include <QBuffer>
#include <QLabel>
#include <QTextStream>
#include <QDataStream>
#include "Udp1.h"
#include "Udp2.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

}

int main(int argc,char** argv)
{
    QApplication app(argc,argv);
    Udp1 udp1;udp1.show();
    Udp2 udp2;udp2.show();
    udp1.setWindowTitle("udp1");
    udp2.setWindowTitle("udp2");
    app.exec();
}


Udp1.cpp


#include "Udp1.h"
#include <QTimer>
#include <QDateTime>

Udp1::Udp1(QWidget *parent) : QWidget(parent)
{
    _udp = new QUdpSocket;
    _udp->bind(10001);

    connect(_udp,SIGNAL(readyRead()),this,SLOT(slotReaydRead()));

    QTimer* timer = new QTimer;
    timer->setInterval(1000);
    timer->start();
    connect(timer,&QTimer::timeout,[&](){
        quint64 timestamp = QDateTime::currentMSecsSinceEpoch();
        QString str = QString::number(timestamp);
#if 0
        _udp->writeDatagram(str.toUtf8(),QHostAddress("127.0.0.1"),10002);
#else
        //广播
        _udp->writeDatagram(str.toUtf8(),QHostAddress::Broadcast,10002);
        //多播
        _udp->writeDatagram(str.toUtf8(),QHostAddress("224.0.0.131"),10002);
#endif

    });

}

void Udp1::slotReaydRead()
{
    while(_udp->hasPendingDatagrams()){
        qint32 datagramSize = _udp->pendingDatagramSize();
        QByteArray buf(datagramSize,0);
        _udp->readDatagram(buf.data(),buf.size());
        qDebug() << "udp1" << buf;
    }

}


Udp2.cpp


#include "Udp2.h"
#include <QTimer>
#include <QDateTime>

Udp2::Udp2(QWidget *parent) : QWidget(parent)
{
    _udp = new QUdpSocket;
    _udp->bind(QHostAddress::AnyIPv4,10002);

    _udp->joinMulticastGroup(QHostAddress("224.0.0.131"));

    connect(_udp,SIGNAL(readyRead()),this,SLOT(slotReaydRead()));

    QTimer* timer = new QTimer;
    timer->setInterval(1000);
    timer->start();
    connect(timer,&QTimer::timeout,[&](){
        quint64 timestamp = QDateTime::currentMSecsSinceEpoch();
        QString str = QString::number(timestamp);
        _udp->writeDatagram(str.toUtf8(),QHostAddress("127.0.0.1"),10001);
    });
}


void Udp2::slotReaydRead()
{
    while(_udp->hasPendingDatagrams()){
        qint32 datagramSize = _udp->pendingDatagramSize();
        QByteArray buf(datagramSize,0);
        _udp->readDatagram(buf.data(),buf.size());
        qDebug() << "udp2" << buf;
    }

}

我- - - - - - - 是- - - - - - - 华- - - - - - - 丽- - - - - - - 的- - - - - - - 分- - - - - - - 割- - - - - - - 线- - - - - - - 


最后,补充一下头文件

Udp1.h

#ifndef UDP1_H
#define UDP1_H
#include <QWidget>
#include <QUdpSocket>
#include <QByteArray>


class Udp1 : public QWidget
{
    Q_OBJECT
public:
    explicit Udp1(QWidget *parent = 0);
    QUdpSocket* _udp;

signals:

public slots:
    void slotReaydRead();
};

#endif // UDP1_H


Udp2.h

#ifndef UDP1_H
#define UDP1_H
#include <QWidget>
#include <QUdpSocket>
#include <QByteArray>


class Udp1 : public QWidget
{
    Q_OBJECT
public:
    explicit Udp1(QWidget *parent = 0);
    QUdpSocket* _udp;

signals:

public slots:
    void slotReaydRead();
};

#endif // UDP1_H




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值