• (十六)qt-udp,组播


    基本流程

    简单聊天程序

    #include "server.h"
    #include <QApplication>
    #include "client.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        server w;
        w.show();
    
        Client c;
        c.setWindowTitle("client");
        c.show();
    
        return a.exec();
    }
    main.cpp
    #include "server.h"
    #include "ui_server.h"
    
    server::server(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::server)
    {
        ui->setupUi(this);
        ui->sip_port->setText("8888");
        ui->cip_port->setText("9999");
        ui->cip_lineEdit->setText("127.0.0.1");
        udp1 = new QUdpSocket(this);
    
        udp1->bind(ui->sip_port->text().toInt());
        // send msg
        connect(ui->send, &QPushButton::clicked, this, [=](){
            udp1->writeDatagram(ui->msg->toPlainText().toUtf8(),QHostAddress(ui->cip_lineEdit->text()),ui->cip_port->text().toInt());
            ui->log->append("Me say: " + ui->msg->toPlainText());
            ui->msg->clear();
        });
        // read msg
        connect(udp1, &QUdpSocket::readyRead, this, [=](){
            qint64 size = udp1->pendingDatagramSize();
            QByteArray array(size,0);
            udp1->readDatagram(array.data(),size);
            ui->log->append(array);
        });
    }
    
    server::~server()
    {
        delete ui;
    }
    server.cpp
    #ifndef SERVER_H
    #define SERVER_H
    
    #include <QWidget>
    #include <QUdpSocket>
    
    namespace Ui {
    class server;
    }
    
    class server : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit server(QWidget *parent = 0);
        ~server();
    
    private:
        Ui::server *ui;
        QUdpSocket* udp1;
    };
    
    #endif // SERVER_H
    server.h
    #include "client.h"
    #include "ui_client.h"
    
    Client::Client(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Client)
    {
        ui->setupUi(this);
        ui->sip_port->setText("8888");
        ui->cip_port->setText("9999");
        ui->sip_lineEdit->setText("127.0.0.1");
        udp2 = new QUdpSocket(this);
    
        udp2->bind(ui->cip_port->text().toInt());
        // send msg
        connect(ui->send, &QPushButton::clicked, this, [=](){
            udp2->writeDatagram(ui->msg->toPlainText().toUtf8(),QHostAddress(ui->sip_lineEdit->text()),ui->sip_port->text().toInt());
            ui->log->append("Me say: " + ui->msg->toPlainText());
            ui->msg->clear();
        });
        // read msg
        connect(udp2, &QUdpSocket::readyRead, this, [=](){
            qint64 size = udp2->pendingDatagramSize();
            QByteArray array(size,0);
            udp2->readDatagram(array.data(),size);
            ui->log->append(array);
        });
    }
    
    Client::~Client()
    {
        delete ui;
    }
    client.cpp
    #ifndef CLIENT_H
    #define CLIENT_H
    
    #include <QWidget>
    #include <QUdpSocket>
    namespace Ui {
    class Client;
    }
    
    class Client : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Client(QWidget *parent = 0);
        ~Client();
    
    private:
        Ui::Client *ui;
        QUdpSocket* udp2;
    };
    
    #endif // CLIENT_H
    client.h

    组播

    #include "client.h"
    #include "ui_client.h"
    
    Client::Client(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Client)
    {
        ui->setupUi(this);
        ui->sip_port->setText("8888");
        ui->cip_port->setText("9999");
        ui->sip_lineEdit->setText("127.0.0.1");
        udp2 = new QUdpSocket(this);
    
        // if you want to receive must bind port
        //udp2->bind(ui->cip_port->text().toInt());
        udp2->bind(QHostAddress::AnyIPv4,ui->cip_port->text().toInt());
        // 加入到组播的组中
        udp2->joinMulticastGroup(QHostAddress("244.0.0.10"));
        // send msg
        connect(ui->send, &QPushButton::clicked, this, [=](){
            udp2->writeDatagram(ui->msg->toPlainText().toUtf8(),QHostAddress(ui->sip_lineEdit->text()),ui->sip_port->text().toInt());
            ui->log->append("Me say: " + ui->msg->toPlainText());
            ui->msg->clear();
        });
        // read msg
        connect(udp2, &QUdpSocket::readyRead, this, [=](){
            qint64 size = udp2->pendingDatagramSize();
            QByteArray array(size,0);
            udp2->readDatagram(array.data(),size);
            ui->log->append(array);
        });
    }
    
    Client::~Client()
    {
        delete ui;
    }
    client.cpp
    #include "server.h"
    #include "ui_server.h"
    
    server::server(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::server)
    {
        ui->setupUi(this);
        ui->sip_port->setText("8888");
        ui->cip_port->setText("9999");
        ui->cip_lineEdit->setText("127.0.0.1");
        udp1 = new QUdpSocket(this);
    
        udp1->bind(ui->sip_port->text().toInt());
        // send msg
        connect(ui->send, &QPushButton::clicked, this, [=](){
            udp1->writeDatagram(ui->msg->toPlainText().toUtf8(),QHostAddress("240.0.0.10"),ui->cip_port->text().toInt());
            ui->log->append("Me say: " + ui->msg->toPlainText());
            ui->msg->clear();
        });
        // read msg
        connect(udp1, &QUdpSocket::readyRead, this, [=](){
            qint64 size = udp1->pendingDatagramSize();
            QByteArray array(size,0);
            udp1->readDatagram(array.data(),size);
            ui->log->append(array);
        });
    }
    
    server::~server()
    {
        delete ui;
    }
    组播server.cpp
    #include "server.h"
    #include "ui_server.h"
    
    server::server(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::server)
    {
        ui->setupUi(this);
        ui->sip_port->setText("8888");
        ui->cip_port->setText("9999");
        ui->cip_lineEdit->setText("127.0.0.1");
        udp1 = new QUdpSocket(this);
    
        udp1->bind(ui->sip_port->text().toInt());
        // send msg
        connect(ui->send, &QPushButton::clicked, this, [=](){
            udp1->writeDatagram(ui->msg->toPlainText().toUtf8(),QHostAddress("255.255.255.255"),ui->cip_port->text().toInt());
            ui->log->append("Me say: " + ui->msg->toPlainText());
            ui->msg->clear();
        });
        // read msg
        connect(udp1, &QUdpSocket::readyRead, this, [=](){
            qint64 size = udp1->pendingDatagramSize();
            QByteArray array(size,0);
            udp1->readDatagram(array.data(),size);
            ui->log->append(array);
        });
    }
    
    server::~server()
    {
        delete ui;
    }
    广播server.cpp
  • 相关阅读:
    SQL COUNT() 语法
    SQL AVG 函数
    Android开发
    IIf 函数
    励志
    王者归来
    Java基础知识总结(绝对经典)
    LeetCode算法题-Third Maximum Number(Java实现-四种解法)
    LeetCode算法题-Fizz Buzz(Java实现)
    LeetCode算法题-Longest Palindrome(五种解法)
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10790000.html
Copyright © 2020-2023  润新知