(十六)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

 

posted @ 2019-04-29 14:00  狂奔~  阅读(1195)  评论(0编辑  收藏  举报