Qt——UDP服务端与客户端

UDP服务端与客户端

编写一个 UDP服务器 和 UDP客户端。实现服务端不断广播信息,客户端接收信息,并将信息显示在客户端的文本框内。

实现效果

在这里插入图片描述

代码实现

1. 服务端 UdpServer

udpserver.h

#ifndef UDPSERVER_H
#define UDPSERVER_H

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

class UdpServer : public QDialog
{
    Q_OBJECT

public:
    UdpServer(QWidget *parent = nullptr);
    ~UdpServer();

public slots:
    void startBtnClicked();
    void timeout();

private:

    QLabel * timeLabel;
    QLineEdit * timeLineEdit;
    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)
    : QDialog(parent)
{
    setWindowTitle(tr("Udp Server"));

    timeLabel = new QLabel(tr("计时器:"));
    timeLineEdit = new QLineEdit;
    startBtn = new QPushButton(tr("开始"));

    mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(timeLabel);
    mainLayout->addWidget(timeLineEdit);
    mainLayout->addWidget(startBtn);

    connect(startBtn,SIGNAL(clicked()),this,SLOT(startBtnClicked()));

    port = 5555;
    isStarted = false;
    udpSocket = new QUdpSocket(this);
    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("开始"));
        timer->stop();
        isStarted = false;
    }
}

void UdpServer::timeout(){
    QString msg = timeLineEdit->text();
    if(msg == ""){
        return;
    }

    if(udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port) != msg.length()){
        return;
    }
}

UdpServer::~UdpServer()
{
}

main.cpp

#include "udpserver.h"

#include <QApplication>

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

2. 客户端 UdpClient

udpclient.h

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

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


class UdpClient : public QDialog
{
    Q_OBJECT

public:
    UdpClient(QWidget *parent = nullptr);
    ~UdpClient();


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


private:
   QTextEdit * receiveTextEidt;
   QPushButton * closeBtn;
   QVBoxLayout * mainLayout;

   int port;
   QUdpSocket * udpSocket;

};
#endif // UDPCLIENT_H


udpclient.cpp

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

UdpClient::UdpClient(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Udp Client"));

    receiveTextEidt = new QTextEdit;
    closeBtn = new QPushButton(tr("Close"));
    mainLayout = new QVBoxLayout(this);

    mainLayout->addWidget(receiveTextEidt);
    mainLayout->addWidget(closeBtn);

    connect(closeBtn,SIGNAL(clicked()),this,SLOT(closeBtnClicked()));

    port = 5555;
    udpSocket = new QUdpSocket(this);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));

    bool flag = udpSocket->bind(port);
    if(!flag){
        QMessageBox::warning(this,tr("error"),tr("UdpSocket create error!"));
        return;
    }
}

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

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

        udpSocket->readDatagram(datagram.data(),datagram.size());
        QString msg = datagram.data();
        receiveTextEidt->insertPlainText(msg);
    }
}

UdpClient::~UdpClient()
{
}



main.cpp

#include "udpclient.h"

#include <QApplication>

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


3. 设置允许运行多个项目

在这里插入图片描述

最后分别运行 服务端 和 客户端即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值