Qt---基于UDP的网络广播程序

这里写图片描述

服务器端

这里写图片描述

初始化中创建QUdpSocket、QTimer,当计时器超时,向客户端发送文本框中的数据。计时器每隔1s重启一次。

.pro

QT       += network

udpserver.h

#ifndef UDPSERVER_H
#define UDPSERVER_H

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

class UdpServer: public QDialog
{
    Q_OBJECT

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

public 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

udpserver.cpp

#include "udpserver.h"
#include <QHostAddress>

UdpServer::UdpServer(QWidget *parent, Qt::WindowFlags f)
{
    setWindowTitle(tr("UDP Server"));

    TimerLabel = new QLabel(tr("计时器:"));
    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()), this, SLOT(StartBtnClicked()));

    isStarted = false;
    port = 5555;
    udpSocket = new QUdpSocket(this);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
}

UdpServer::~UdpServer()
{

}

void UdpServer::StartBtnClicked()
{
    if(!isStarted)
    {
        StartBtn->setText(tr("停止"));
        timer->start(1000);  //每隔1秒启动一次
        isStarted = true;
    }
    else
    {
        StartBtn->setText(tr("开始"));
        isStarted = false;
        timer->stop();
    }
}

//向端口发送广播信息
void UdpServer::timeout()
{
    QString msg = TextLineEdit->text();
    int length = 0;
    if(msg == "")
        return;
    if((length == udpSocket->writeDatagram(msg.toLatin1(),
        msg.length(), QHostAddress::Broadcast, port)) != msg.length())
        return;
}

main.cpp

#include "udpserver.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    UdpServer w;
    w.show();

    return a.exec();
}

客户端

这里写图片描述

初始化后中创建QUdpSocket,绑定端口,当接收到数据时,显示到文本框中

.pro

QT       += network

udpclient.h

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>

class UdpClient:public QDialog
{
    Q_OBJECT

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

public slots:
    void CloseBtnClicked();
    void dataReceived();

private:
    QTextEdit *ReceiveTextEdit;
    QPushButton *CloseBtn;
    QVBoxLayout *mainLayout;

    int port;
    QUdpSocket *udpSocket;
};

#endif // UDPCLIENT_H

udpserver.cpp

#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>

UdpClient::UdpClient(QWidget *parent, Qt::WindowFlags f):QDialog(parent)
{
    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(dataReceived()));

    bool result = udpSocket->bind(port);
    if(!result)
    {
        QMessageBox::information(this, "error", "udp socket create error!");
        return;
    }
}

UdpClient::~UdpClient()
{

}

void UdpClient::CloseBtnClicked()
{
    close();
}

void UdpClient::dataReceived()
{
    while(udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());

        udpSocket->readDatagram(datagram.data(), datagram.size());
        ReceiveTextEdit->insertPlainText(datagram.data());
    }
}

main.cpp

#include "udpclient.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    UdpClient w;
    w.show();

    return a.exec();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值