• qt中的udp编程


    UDP
    QUdpSocket ---> upd socket

    1.创建
    QUdpSocket *p = new QUdpSocket();

    2.最先接收数据一方 调用bind-> ip/port
    bool QAbstractSocket::bind(const QHostAddress & address, quint16 port = 0, BindMode mode = DefaultForPlatform)
    p->bind();

    connect( ,SIGNAL(readyRead()),,SLOT(r));
    3.接收数据
    当 QUdpSocket 对象收到别一方发来的数据会发出信号 readyRead()
    qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
    r()
    {
    p->readDatagram();

    }
    4.发数据
    qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)
    p->writeDatagram();

    recv.h
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include<QHostAddress>
    #include<QUdpSocket>
    #include<QByteArray>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
        void getmsg();
        void on_pushButton_2_clicked();
    
    private:
        Ui::Dialog *ui;
        QUdpSocket *up;
        QHostAddress *host;
        quint16 port,port1;
    };
    
    #endif // DIALOG_H
    recv.cpp
    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    
    up = new QUdpSocket(this);
        port = 6501;
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
     up->bind(QHostAddress("192.168.1.30"),6501);
    connect(up,SIGNAL(readyRead()),this,SLOT(getmsg()));
    }
    void Dialog::getmsg()
    {//qDebug()<<host<<port;
        QByteArray buf;
      buf.resize(up->pendingDatagramSize());
        QHostAddress host1;
       // quint16 port1;
    //qDebug()<<host<<port;
        up->readDatagram(buf.data(),buf.size(),&host1,&port1);
       // qDebug()<<buf.data();
        QString tmp(buf.data());
        ui->listWidget->addItem(tmp);
    }
    
    void Dialog::on_pushButton_2_clicked()
    {//qDebug()<<host<<port;
    host = new QHostAddress("192.168.1.30");
    
        QString tmp = ui->lineEdit->text();
        qDebug()<<tmp;
    
    up->writeDatagram(tmp.toLatin1(),tmp.size(),*host,port1);
    //qDebug()<<host<<port;
    
    }
    ui
    /********************************************************************************
    ** Form generated from reading UI file 'dialog.ui'
    **
    ** Created by: Qt User Interface Compiler version 5.5.0
    **
    ** WARNING! All changes made in this file will be lost when recompiling UI file!
    ********************************************************************************/
    
    #ifndef UI_DIALOG_H
    #define UI_DIALOG_H
    
    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QDialog>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QLineEdit>
    #include <QtWidgets/QListWidget>
    #include <QtWidgets/QPushButton>
    
    QT_BEGIN_NAMESPACE
    
    class Ui_Dialog
    {
    public:
        QPushButton *pushButton;
        QPushButton *pushButton_2;
        QListWidget *listWidget;
        QLabel *label;
        QLineEdit *lineEdit;
    
        void setupUi(QDialog *Dialog)
        {
            if (Dialog->objectName().isEmpty())
                Dialog->setObjectName(QStringLiteral("Dialog"));
            Dialog->resize(400, 300);
            pushButton = new QPushButton(Dialog);
            pushButton->setObjectName(QStringLiteral("pushButton"));
            pushButton->setGeometry(QRect(270, 100, 99, 27));
            pushButton_2 = new QPushButton(Dialog);
            pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
            pushButton_2->setGeometry(QRect(290, 250, 99, 27));
            listWidget = new QListWidget(Dialog);
            listWidget->setObjectName(QStringLiteral("listWidget"));
            listWidget->setGeometry(QRect(20, 50, 221, 141));
            label = new QLabel(Dialog);
            label->setObjectName(QStringLiteral("label"));
            label->setGeometry(QRect(60, 20, 121, 17));
            lineEdit = new QLineEdit(Dialog);
            lineEdit->setObjectName(QStringLiteral("lineEdit"));
            lineEdit->setGeometry(QRect(80, 250, 141, 27));
    
            retranslateUi(Dialog);
    
            QMetaObject::connectSlotsByName(Dialog);
        } // setupUi
    
        void retranslateUi(QDialog *Dialog)
        {
            Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0));
            pushButton->setText(QApplication::translate("Dialog", "bind", 0));
            pushButton_2->setText(QApplication::translate("Dialog", "send", 0));
            label->setText(QApplication::translate("Dialog", "recv", 0));
        } // retranslateUi
    
    };
    
    namespace Ui {
        class Dialog: public Ui_Dialog {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    
    #endif // UI_DIALOG_H
    send.h
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include<QDebug>
    #include<QUdpSocket>
    #include<QHostAddress>
    #include<QByteArray>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
        void getmsg();
    private:
        Ui::Dialog *ui;
        QUdpSocket *up;
        QHostAddress *host,host1;
        quint16 port,port1;
    };
    
    #endif // DIALOG_H
    send.cpp
    #include
    "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); // up = new QUdpSocket; port = 6501; host = new QHostAddress("192.168.1.30"); up = new QUdpSocket(); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { QString tmp = ui->lineEdit->text(); qDebug()<<tmp; up->writeDatagram(tmp.toLatin1(),*host,port); connect(up,SIGNAL(readyRead()),this,SLOT(getmsg())); } void Dialog::getmsg() { QByteArray buf; buf.resize(up->pendingDatagramSize()); QHostAddress host1; quint16 port1; up->readDatagram(buf.data(),buf.size(),&host1,&port1); //qDebug()<<buf.data(); QString tmp(buf.data()); ui->listWidget->addItem(tmp); }
  • 相关阅读:
    halcongen_region_contour_xld轮廓转化成区域
    fit_circle_contour_xld拟合圆
    关于.Net Core下因客户端主动取消导致Request请求[FromBody]模型绑定读取异常BadHttpRequestException
    Java有了synchronized,为什么还要提供Lock
    源码详解数据结构Linked List
    CVE20213129:Laravel远程代码漏洞复现分析
    缓存最关心的问题是什么
    如何使用Java AWT 创建一个简易计算器
    聊聊数仓中TPCDDS&TPCH与查询性能的那些事儿
    6张图为你分析Kafka Producer 消息缓存模型
  • 原文地址:https://www.cnblogs.com/defen/p/5344077.html
Copyright © 2020-2023  润新知