龙空技术网

Qt-udp(组播)

QT教程 150

前言:

现时各位老铁们对“netty udp组播”可能比较重视,兄弟们都想要知道一些“netty udp组播”的相关知识。那么小编同时在网络上收集了一些关于“netty 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();}
#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;}

点击领取Qt学习资料+视频教程~「链接」

#ifndef SERVER_H#define SERVER_H#include <QWidget>#include <QUdpSocket>namespace Ui {class server;}class server : public QWidget{    Q_OBJECTpublic:    explicit server(QWidget *parent = 0);    ~server();private:    Ui::server *ui;    QUdpSocket* udp1;};#endif // 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;}
#ifndef CLIENT_H#define CLIENT_H#include <QWidget>#include <QUdpSocket>namespace Ui {class Client;}class Client : public QWidget{    Q_OBJECTpublic:    explicit Client(QWidget *parent = 0);    ~Client();private:    Ui::Client *ui;    QUdpSocket* udp2;};#endif // 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;}
#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;}
#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;}

标签: #netty udp组播 #c语言udp组播