zoukankan      html  css  js  c++  java
  • qt udp 聊天

    一、知识点总结

    udpSocket->bind(8888); //绑定端口

    connect(udpSocket,&QUdpSocket::readyRead,this,&UdpA::dealMsg); //收到对方的消息,立即触发

    char buf[1024] = {0};//缓存

    QHostAddress peerIp; //对方IP

    quint16 peerPort; //对方端口

    qint16 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);// 读到多少个字符

    QString("[%1:%2] %3").arg(peerIp.toString()).arg(peerPort).arg(buf);  //字符串模板占位

    QString ip = ui->ip->text();       

    qint16 port = ui->port->text().toInt();   //端口号

    QString msg = ui->msg->toPlainText();  //输入框内容

    udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);// 写出字符串

    二、案例

    1、UdpA

    UdpA.h

    #ifndef UDPA_H
    #define UDPA_H
    
    #include <QWidget>
    #include <QUdpSocket>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class UdpA; }
    QT_END_NAMESPACE
    
    class UdpA : public QWidget
    {
        Q_OBJECT
    
    public:
        UdpA(QWidget *parent = nullptr);
        ~UdpA();
        void dealMsg(); //槽函数,处理对方发过来的消息
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::UdpA *ui;
        QUdpSocket * udpSocket;
    };
    #endif // UDPA_H

    UdpA.cpp

    #include "UdpA.h"
    #include "ui_UdpA.h"
    
    UdpA::UdpA(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::UdpA)
    {
        ui->setupUi(this);
        setWindowTitle("UdpA:8888");
    
        //分配空间,指定父对象,自动释放内存
        udpSocket = new QUdpSocket(this);
        //绑定端口
        udpSocket->bind(8888);
    
        //收到对方的消息,立即触发
        connect(udpSocket,&QUdpSocket::readyRead,this,&UdpA::dealMsg);
    }
    
    UdpA::~UdpA()
    {
        delete ui;
    }
    
    //处理对方发过来的内容
    void UdpA::dealMsg()
    {
        char buf[1024] = {0}; //缓存
        QHostAddress peerIp; //对方IP
        quint16 peerPort; //对方端口
        qint16 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);//读到多少个字符
        if(length>0){
            QString msgTxt = QString("[%1:%2] %3")
                    .arg(peerIp.toString())
                    .arg(peerPort)
                    .arg(buf); //字符串模板占位
            ui->msg->setText(msgTxt);
        }
    }
    
    
    void UdpA::on_pushButton_clicked()
    {
        //获取对方的IP和端口
        QString ip = ui->ip->text();
        qint16 port = ui->port->text().toInt();
        QString msg = ui->msg->toPlainText();
        udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);
    }

    2、UdpB

    UdpB.h

    #ifndef UDPB_H
    #define UDPB_H
    
    #include <QWidget>
    #include <QUdpSocket>
    
    namespace Ui {
    class UdpB;
    }
    
    class UdpB : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit UdpB(QWidget *parent = nullptr);
        ~UdpB();
    
    private slots:
        void on_pushButton_clicked();
    
        void dealMsg();
    
    private:
        Ui::UdpB *ui;
        QUdpSocket * udpSocket;
    };
    
    #endif // UDPB_H

    UdpB.cpp

    #include "UdpB.h"
    #include "ui_UdpB.h"
    
    UdpB::UdpB(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::UdpB)
    {
        ui->setupUi(this);
        setWindowTitle("UdpB:9999");
    
        udpSocket = new QUdpSocket(this);
        udpSocket->bind(9999);
    
        connect(udpSocket,&QUdpSocket::readyRead,this,&UdpB::dealMsg);
    }
    
    UdpB::~UdpB()
    {
        delete ui;
    }
    
    void UdpB::dealMsg(){
        QHostAddress peerIp ;
        quint16 peerPort ;
        char buf[1024] = {0};
        qint64 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);
        if(length > 0){
            QString msgTxt = QString("[%1:%2] %3")
                    .arg(peerIp.toString())
                    .arg(peerPort)
                    .arg(buf);
            ui->msg->setText(msgTxt);
        }
    }
    
    void UdpB::on_pushButton_clicked()
    {
        //获取对方的IP和端口
        QString ip = ui->ip->text();
        qint16 port = ui->port->text().toInt();
        QString msg = ui->msg->toPlainText();
        udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);
    }
  • 相关阅读:
    在Postgre中设置max_connections时,为什么需要使用连接池 (译)
    既然python的多线程是"伪多线程",那么多线程访问共享资源的时候,还需要线程锁吗
    Debugging: MISCONF Redis is configured to save RDB snapshots(译)
    MySQL集群故障转移的时候在基于python的客户端连接层自动failover实现
    exec: "com.docker.cli": executable file not found in %PATH%
    windows 快捷操作系列
    Docker Build时查看当前镜像目录树
    vs2019生成的dockerfile 手动 build
    Python Records库使用举例
    Jupyter Notebook安装扩充插件与常见问题
  • 原文地址:https://www.cnblogs.com/spqin/p/15501557.html
Copyright © 2011-2022 走看看