Qt-利用QUdpSocket实现UDP点对点发送char*(QtToQt)
相关资料:
https://download.csdn.net/download/zhujianqiangqq/47534759 代码包下载
udpsend.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include5 #include 6 7 QT_BEGIN_NAMESPACE 8 namespace Ui { class MainWindow; } 9 QT_END_NAMESPACE 10 11 struct WeaponInfo 12 { 13 unsigned int type; 14 unsigned int quantity; 15 }; 16 struct PlaneInfo 17 { 18 double longitude; 19 double latitude; 20 double height; 21 WeaponInfo weapons[10]; 22 }; 23 struct PlaneInfos 24 { 25 unsigned int planeNumber; 26 PlaneInfo info [8]; 27 }; 28 29 30 class MainWindow : public QMainWindow 31 { 32 Q_OBJECT 33 34 public: 35 MainWindow(QWidget *parent = nullptr); 36 ~MainWindow(); 37 38 private slots: 39 void on_pushButton_clicked(); 40 void on_Timer(); 41 void on_pushButton_2_clicked(); 42 43 private: 44 Ui::MainWindow *ui; 45 QTimer *m_pTimer = NULL; 46 }; 47 #endif // MAINWINDOW_H
udpsend.cpp主函数
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 #include4 #include 5 #include 6 #include 7 8 const quint16 PORT = 2333; 9 10 MainWindow::MainWindow(QWidget *parent) 11 : QMainWindow(parent) 12 , ui(new Ui::MainWindow) 13 { 14 ui->setupUi(this); 15 16 m_pTimer = new QTimer(this); 17 m_pTimer->setSingleShot(false); 18 m_pTimer->start(1000); 19 connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_Timer); 20 } 21 22 MainWindow::~MainWindow() 23 { 24 delete ui; 25 } 26 27 void MainWindow::on_pushButton_clicked() 28 { 29 // 初始化UDP对象 30 QUdpSocket oUDPSend; 31 // 发送信息 32 QDateTime sysDateTime; 33 QByteArray msg = (sysDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+" \n").toLatin1(); 34 oUDPSend.writeDatagram(msg, QHostAddress("10.0.1.45"), PORT);// 需要先帮定127.0.0.1或10.0.1.45,都是本机IP 35 // 输出信息(提示用,没有实际作用) 36 ui->textEdit->insertPlainText(QStringLiteral("1")); 37 } 38 39 void MainWindow::on_Timer() 40 { 41 // on_pushButton_clicked(); 42 } 43 44 void MainWindow::on_pushButton_2_clicked() 45 { 46 PlaneInfos oPlaneInfos; 47 oPlaneInfos.planeNumber = 2; 48 49 PlaneInfo oPlaneInfo1; 50 oPlaneInfo1.longitude = 177; 51 oPlaneInfo1.latitude = 23; 52 WeaponInfo oWeaponInfo1; 53 oWeaponInfo1.type = 11111; 54 oWeaponInfo1.quantity =1; 55 oPlaneInfo1.weapons[0] = oWeaponInfo1; 56 57 PlaneInfo oPlaneInfo2; 58 oPlaneInfo2.longitude = 113; 59 oPlaneInfo2.latitude = 34; 60 WeaponInfo oWeaponInfo2; 61 oWeaponInfo2.type = 2222; 62 oWeaponInfo2.quantity =1; 63 oPlaneInfo2.weapons[0] = oWeaponInfo2; 64 65 oPlaneInfos.info[0] = oPlaneInfo1; 66 oPlaneInfos.info[1] = oPlaneInfo2; 67 68 // 初始化UDP对象 69 QUdpSocket oUDPSend; 70 // 发送信息 71 oUDPSend.writeDatagram((char*)&oPlaneInfos, sizeof(PlaneInfos), QHostAddress("10.0.1.45"), PORT);// 需要先帮定127.0.0.1或10.0.1.45,都是本机IP 72 // 输出信息(提示用,没有实际作用) 73 ui->textEdit->insertPlainText(QStringLiteral("char*\n")); 74 }
udpReceiver.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include5 6 #include 7 #include 8 9 const quint16 PORT = 2333; 10 11 struct WeaponInfo 12 { 13 unsigned int type; 14 unsigned int quantity; 15 }; 16 struct PlaneInfo 17 { 18 double longitude; 19 double latitude; 20 double height; 21 WeaponInfo weapons[10]; 22 }; 23 struct PlaneInfos 24 { 25 unsigned int planeNumber; 26 PlaneInfo info [8]; 27 }; 28 29 QT_BEGIN_NAMESPACE 30 namespace Ui { class MainWindow; } 31 QT_END_NAMESPACE 32 33 class MainWindow : public QMainWindow 34 { 35 Q_OBJECT 36 37 public: 38 MainWindow(QWidget *parent = nullptr); 39 ~MainWindow(); 40 41 public slots: 42 void receive(); 43 44 45 private: 46 QUdpSocket *m_pUDPReceiver; 47 48 private: 49 Ui::MainWindow *ui; 50 }; 51 #endif // MAINWINDOW_H
udpReceiver.cpp主函数
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 m_pUDPReceiver = new QUdpSocket; 11 m_pUDPReceiver->bind(QHostAddress("10.0.1.45"), PORT);// 第一步:需要先帮定127.0.0.1或10.0.1.45,都是本机IP 12 connect(m_pUDPReceiver, SIGNAL(readyRead()), this, SLOT(receive()));//第二步:帮定读取事件 13 } 14 15 MainWindow::~MainWindow() 16 { 17 delete ui; 18 delete m_pUDPReceiver; 19 } 20 21 void MainWindow::receive() 22 { 23 QByteArray ba; 24 PlaneInfos oPlaneInfos; 25 while(m_pUDPReceiver->hasPendingDatagrams()) 26 { 27 qDebug()<< "receive"; 28 ba.resize(m_pUDPReceiver->pendingDatagramSize()); 29 m_pUDPReceiver->readDatagram(ba.data(), ba.size()); 30 memcpy(&oPlaneInfos, ba.data(), sizeof(PlaneInfos)); 31 ui->textEdit->insertPlainText("Number" + QString::number(oPlaneInfos.planeNumber)); 32 ui->textEdit->append(""); 33 ui->textEdit->append("longitude" + QString::number(oPlaneInfos.info[0].longitude)); 34 ui->textEdit->append("latitude" + QString::number(oPlaneInfos.info[0].latitude)); 35 ui->textEdit->append("type" + QString::number(oPlaneInfos.info[0].weapons[0].type)); 36 ui->textEdit->append("quantity" + QString::number(oPlaneInfos.info[0].weapons[0].quantity)); 37 38 ui->textEdit->append("longitude" + QString::number(oPlaneInfos.info[1].longitude)); 39 ui->textEdit->append("latitude" + QString::number(oPlaneInfos.info[1].latitude)); 40 ui->textEdit->append("type" + QString::number(oPlaneInfos.info[1].weapons[0].type)); 41 ui->textEdit->append("quantity" + QString::number(oPlaneInfos.info[1].weapons[0].quantity)); 42 ui->textEdit->append(""); 43 } 44 }