QtUDP简单实现

目录

1、Udp服务器

1.1、头文件

1.2、定义

2、Udp客户端

2.1 头文件

2.2 定义

3、显示


1、Udp服务器

1.1、头文件

#ifndef UDPSEVER_H
#define UDPSEVER_H

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

class UDPSever : public QDialog
{
    Q_OBJECT

public:
    UDPSever(QWidget *parent = 0);
    ~UDPSever();
private:
    QLabel *TimerLabel;
    QLineEdit *TextLineEdit;
    QPushButton *StartBtn;
    QVBoxLayout *mainLayout;

private slots:
    void StartBtnFunc();
    void timeOut();
private:
    int port;
    bool isStarted;
    QUdpSocket *udpSocket;
    QTimer *timer;
};

#endif // UDPSEVER_H

1.2、定义

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

UDPSever::UDPSever(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UDP Serser"));

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

    //设置UDP的端口号参数,服务器定时向此端口发送广播
    port = 5555;
    isStarted = false;
    udpSocket = new QUdpSocket(this);
    timer = new QTimer(this);

    //计时器输出信号
    connect(timer,SIGNAL(timeout()),this,SLOT(timeOut()));
}

UDPSever::~UDPSever()
{

}

void UDPSever::StartBtnFunc()
{
    if(!isStarted)
    {
        StartBtn->setText(tr("停止"));
        //计时器设置,一秒输出一次信号
        timer->start(1000);
        isStarted = true;
    }
    else
    {
        StartBtn->setText(tr("开始"));
        isStarted = false;
        timer->stop();
    }
}

void UDPSever::timeOut()
{
    //获取文本框内容
    QString msg = TextLineEdit->text();
    int ilength = 0;
    if("" == msg)
    {
        return;
    }
    //数据(Latin-1,8位字符串),长度,主机地址,端口号
    if((ilength = udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))
            != msg.length())
    {
        return;
    }
}

2、Udp客户端

2.1 头文件

#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);
    ~UdpClient();

private:
    QTextEdit *ReceiveText;
    QPushButton *CloseBtn;
    QVBoxLayout *mainLayout;
public slots:
    void CloseBtnFunc();
    void dataRecieved();
private:
    int port;
    QUdpSocket *UdpSocket;
};

#endif // UDPCLIENT_H

2.2 定义

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

UdpClient::UdpClient(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UDP Client"));
    ReceiveText = new QTextEdit(this);
    CloseBtn = new QPushButton(this);

    mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(ReceiveText);
    mainLayout->addWidget(CloseBtn);

    connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnFunc()));

    port = 5555;
    UdpSocket = new QUdpSocket(this);
    //QUdpSocket是I/O设备,当有数据到达时,发出readyRead()信号
    connect(UdpSocket,SIGNAL(readyRead()),this,SLOT(dataRecieved()));


    //绑定端口
    bool bResult = UdpSocket->bind(port);
    if(!bResult)
    {
        QMessageBox::information(this,tr("error"),tr("udp socket create error"));
        return;
    }
}

UdpClient::~UdpClient()
{

}
void UdpClient::CloseBtnFunc()
{
    close();
}
void UdpClient::dataRecieved()
{
    //当有数据报是 返回true
    while(UdpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(UdpSocket->pendingDatagramSize());
        //读取数据报
        UdpSocket->readDatagram(datagram.data(),datagram.size());

        QString msg = datagram.data();
        //显示数据报内容(无格式的按顺序在文本中显示内容)
        ReceiveText->insertPlainText(msg);
    }
}

3、显示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章