您的位置:首页 > 编程语言 > Qt开发

QT_Socket_udp

2016-03-17 23:34 429 查看
这是一个基于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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ qt