QUdpSocket realizes chat function (time filter, enter to send chat content)

1, QUdpSocket is used to realize the similar wechat chat function. After entering the chat information, press enter to send the chat content. In this paper, the method of registering event filter is used. There is no difference between the server and the client in socket communication. In order to distinguish the demonstration, the server and the client are used in the name selection of the control. In fact, their code implementation is the same, but the port number they listen to is different.

2, Renderings

3, Core code

#include "WidgetUdpClient.h"
#include "ui_WidgetUdpClient.h"
#include <QtNetwork/QHostAddress>

WidgetUdpClient::WidgetUdpClient(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetUdpClient)
{
    ui->setupUi(this);

    // Register event filters
    ui->textEditInput->installEventFilter(this);

    // Initialize udpSocket and bind listening port number
    m_pUdpSocket = new QUdpSocket(this);
    m_pUdpSocket->bind(9999);

    connect(m_pUdpSocket,SIGNAL(readyRead()),this,SLOT(dealRecvMsg()));
}

WidgetUdpClient::~WidgetUdpClient()
{
    if(m_pUdpSocket != NULL)
    {
        m_pUdpSocket->close();
        delete m_pUdpSocket;
        m_pUdpSocket = NULL;
    }
    delete ui;
}

//************************************
// Function name:
// Function Description: event filter override
// Return value: void
// Author: ISmileLi
// Date of establishment:
// Modification record:
//************************************
bool WidgetUdpClient::eventFilter(QObject *object, QEvent *event)
{
    if(object == ui->textEditInput)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if(keyEvent->key() == Qt::Key_Return)
        {
            QString ip = ui->lineEditIP->text();
            quint16 port = ui->lineEditPort->text().toInt();
            QString strData = ui->textEditInput->toPlainText();
            m_pUdpSocket->writeDatagram(strData.toUtf8(),(QHostAddress)ip,port);
            ui->textEditRecv->setTextColor("red");
            if(!strData.isEmpty())
                ui->textEditRecv->append("client:"+strData);
            qDebug() << "client.......";
            ui->textEditInput->clear();
            return true;
        }
        return false;
    }
    else
        return WidgetUdpClient::eventFilter(object, event);
}

//************************************
// Function name:
// Function Description: receiving data
// Return value: void
// Author: ISmileLi
// Date of establishment:
// Modification record:
//************************************
void WidgetUdpClient::dealRecvMsg()
{
    char tempBuf[1024] = {0};
    QHostAddress hostAddress;
    quint16 port;
    qint64 length = m_pUdpSocket->readDatagram(tempBuf,sizeof(tempBuf),&hostAddress,&port);
    if(length > 0)
    {
        QString strMsg = QString("[%1-%2]:%3").arg(hostAddress.toString()).arg(port).arg(tempBuf);
        ui->textEditRecv->setTextColor("cyan");
        ui->textEditRecv->append("server:"+strMsg);
    }
}

Note: because the code is the same, another copy of the code (that is, the server-side) is not pasted here. If you need source code, please download:
http://download.csdn.net/download/toby54king/10163410.

Tags: socket Qt

Posted on Fri, 08 May 2020 07:39:56 -0700 by jdavila28