《Qt Cerator》UDP 双向通信

《Qt Cerator》UDP 双向通信_第1张图片

文章目录

  • UDPchat.pro
  • UDPA.h
  • UDPB.h
  • UDPB.cpp

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 
#include 
#include 
#include 
#include 
#include 
#include 

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+=("土豪老肖:\r\n"+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 
#include 
#include 
#include 
#include 
#include 
#include 

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);
    }

}

你可能感兴趣的:(Qt,Cerator,C++,UDP,网络通信)