《Qt Cerator》UDP 双向通信

在这里插入图片描述

UDPchat.pro

# 手动添加“network”网络库
QT += widgets gui network
HEADERS += \
    UdpA.h \
    UdpB.h
SOURCES += \
    main.cpp \
    UdpA.cpp \
    UdpB.cpp

UDPA.h

#ifndef UDPRECEIVER_H
#define UDPRECEIVER_H

#include <QWidget>
#include <QUdpSocket>
#include <QTextBrowser>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

class UdpA : public QWidget
{
    Q_OBJECT
public:
    explicit UdpA(QWidget *parent = nullptr);
private:
//	套接字
    QUdpSocket* _Socket;
//	发送消息的按钮
    QPushButton* _ButSendTo;
//	编辑消息
    QLineEdit* _EditMessage;
//	浏览消息
    QTextBrowser* _MessageView;

signals:

public slots:
//	发送消息
    void SlotSnedTo();
//	准备读取
    void SlotReadyRead();
};

#endif // UDPRECEIVER_H

#UDPA.cpp

#include "UdpA.h"

UdpA::UdpA(QWidget *parent) : QWidget(parent)
{
//	创建套接字
    this->_Socket=new QUdpSocket;
//	绑定端口
    this->_Socket->bind(QHostAddress("127.0.0.1"),8858);
//	准备读取报文
    connect(this->_Socket,SIGNAL(readyRead()),SLOT(SlotReadyRead()));

//	控件布局
    QVBoxLayout* V_Layout(new QVBoxLayout);
    V_Layout->addWidget(this->_MessageView=new QTextBrowser);

    QHBoxLayout* H_Layout(new QHBoxLayout);
    H_Layout->addWidget(this->_EditMessage=new QLineEdit);
    H_Layout->addWidget(this->_ButSendTo=new QPushButton("Send"));

    V_Layout->addLayout(H_Layout);

    this->setLayout(V_Layout);
	
    connect(this->_ButSendTo,SIGNAL(clicked()),this,SLOT(SlotSnedTo()));
    connect(this->_EditMessage,SIGNAL(returnPressed()),this,SLOT(SlotSnedTo()));
 
}

void UdpA::SlotSnedTo()
{
    QString Str(this->_EditMessage->text());

    if(Str.isEmpty())
    {
        return;
    }
    else
    {

        Str.clear();
        Str+=("<img src=VIP.png><font color=red>土豪老肖:\r\n</font>"+this->_EditMessage->text());
        this->_Socket->writeDatagram(Str.toUtf8(),QHostAddress("127.0.0.1"),8848);
        this->_EditMessage->clear();
        Str.clear();
    }
}

void UdpA::SlotReadyRead()
{
    while (this->_Socket->hasPendingDatagrams())
    {
        qint64 DatagramSize=this->_Socket->pendingDatagramSize();
        QByteArray Reced(DatagramSize,0);
        this->_Socket->readDatagram(Reced.data(),Reced.size());
        this->_MessageView->append(Reced);
    }

}

UDPB.h

#ifndef UDPSENDER_H
#define UDPSENDER_H

#include <QWidget>
#include <QUdpSocket>
#include <QTextBrowser>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

class UdpB : public QWidget
{
    Q_OBJECT
public:
    explicit UdpB(QWidget *parent = nullptr);

private:
    QUdpSocket* _Socket;
    QPushButton* _ButSendTo;
    QLineEdit* _EditMessage;
    QTextBrowser* _MessageView;

signals:

public slots:
    void SlotSnedTo();
    void SlotReadyRead();
};
#endif // UDPSENDER_H

UDPB.cpp

#include "UdpB.h"

UdpB::UdpB(QWidget *parent) : QWidget(parent)
{
    this->_Socket=new QUdpSocket;
    this->_Socket->bind(QHostAddress("127.0.0.1"),8848);
    connect(this->_Socket,SIGNAL(readyRead()),SLOT(SlotReadyRead()));

    QVBoxLayout* V_Layout(new QVBoxLayout);
    V_Layout->addWidget(this->_MessageView=new QTextBrowser);


    QHBoxLayout* H_Layout(new QHBoxLayout);
    H_Layout->addWidget(this->_EditMessage=new QLineEdit);
    H_Layout->addWidget(this->_ButSendTo=new QPushButton("Send"));

    V_Layout->addLayout(H_Layout);

    this->setLayout(V_Layout);

    connect(this->_ButSendTo,SIGNAL(clicked()),this,SLOT(SlotSnedTo()));
    connect(this->_EditMessage,SIGNAL(returnPressed()),this,SLOT(SlotSnedTo()));
}

void UdpB::SlotSnedTo()
{
    QString Str(this->_EditMessage->text());
    if(Str.isEmpty())
    {
        return;
    }
    else
    {

        Str.clear();
        Str+=("屌丝老王:"+this->_EditMessage->text());
        this->_Socket->writeDatagram(Str.toUtf8(),QHostAddress("127.0.0.1"),8858);
        this->_EditMessage->clear();
        Str.clear();
    }

}

void UdpB::SlotReadyRead()
{

    while (this->_Socket->hasPendingDatagrams())
    {
        qint64 DatagramSize=this->_Socket->pendingDatagramSize();
        QByteArray Reced(DatagramSize,0);
        this->_Socket->readDatagram(Reced.data(),Reced.size());
        this->_MessageView->append(Reced);
    }

}

猜你喜欢

转载自blog.csdn.net/baidu_41905806/article/details/86063253