QUdp通信简单实例

一、效果展示
在这里插入图片描述
Udp网络通信,需要在.pro文件中添加QT += network
二、UdpServer

.h

#ifndef UDPSERVER_H
#define UDPSERVER_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>

class UdpServer : public QWidget
{
    Q_OBJECT

public:
    UdpServer(QWidget *parent = 0,Qt::WindowFlags f=0);
    ~UdpServer();
private slots:
    void startBtnClicked();
    void timeout();
private:
    QLabel* TimerLabel;
    QLineEdit* textLineEdit;
    QPushButton* startBtn;
    QVBoxLayout* mainLayout;
    int port;
    bool isStarted;
    QUdpSocket* udpSocket;
    QTimer* timer;

};

#endif // UDPSERVER_H

.cpp

#include "udpserver.h"
#include <QHostAddress>
UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f)
    : QWidget(parent,f)
{
    setWindowTitle("UdpServer");
    TimerLabel = new QLabel("计时器",this);
    textLineEdit = new QLineEdit(this);
    startBtn = new QPushButton(tr("开始"),this);

    mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(TimerLabel);
    mainLayout->addWidget(textLineEdit);
    mainLayout->addWidget(startBtn);

    connect(startBtn,SIGNAL(clicked(bool)),this,SLOT(startBtnClicked()));
    port = 5555;
    isStarted=false;
    udpSocket = new QUdpSocket;
    timer = new QTimer(this);

    //定时发送广播信息
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
}
void UdpServer::startBtnClicked()
{
    if(!isStarted)
    {
        startBtn->setText(tr("停止"));
        timer->start(1000);
        isStarted=true;
    }
    else
    {
        startBtn->setText(tr("开始"));
        isStarted=false;
        timer->stop();
    }
}
void UdpServer::timeout()
{
    QString str = textLineEdit->text();
    int length=0;
    if(str=="")
    {
        return;
    }
    length==udpSocket->writeDatagram(str.toLatin1(),str.length(),QHostAddress::Broadcast,port);
    if(length!=str.length())
    {
        return;
    }
}
UdpServer::~UdpServer()
{

}

三、udpClient
.h’

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include <QWidget>
#include <QUdpSocket>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QPushButton>
class UdpClient : public QWidget
{
    Q_OBJECT

public:
    UdpClient(QWidget *parent = 0,Qt::WindowFlags f=0);
    ~UdpClient();

private slots:
    void closeBtnClicked();
    void dataReceive();

private:
    int port;
    QUdpSocket* udpSocket;
    QTextEdit* receiveTextEdit;
    QPushButton* closeBtn;
    QVBoxLayout* mainLayout;
};

#endif // UDPCLIENT_H

.cpp

#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f)
    : QWidget(parent,f)
{
    setWindowTitle(tr("Udp Client"));
    receiveTextEdit = new QTextEdit(this);
    closeBtn = new QPushButton(tr("close"),this);
    mainLayout= new QVBoxLayout(this);
    mainLayout->addWidget(receiveTextEdit);
    mainLayout->addWidget(closeBtn);
    connect(closeBtn,SIGNAL(clicked()),this,SLOT(closeBtnClicked()));
    port=5555;
    udpSocket = new QUdpSocket(this);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceive()));
    bool result = udpSocket->bind(port);
    if(!result)
    {
        QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
        return;
    }
}
void UdpClient::closeBtnClicked()
{
    close();
}
void UdpClient::dataReceive()
{
    while(udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(),datagram.size());
        QString msg = datagram.data();
        receiveTextEdit->insertPlainText(msg);
    }
}
UdpClient::~UdpClient()
{

}

仅作为个人学习记录

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值