QT学习笔记(13) QT下的UDP通信

一、UDP通信

  UDP通信没有明确的服务器端和客户端之分

  TCP通信像是打电话(必须要接通才能通信),UDP通信像是写信(不管能不能收到都发送出去)

  首先需要QUdpSOcket套接字,然后绑定bind()端口号和ip

  如果对方发送过来数据,套接字自动触发readyRead()方法

  套接字QUdpSOcket通过readDatagram()和writeDatagram()方法读取和写入数据

    

二、示例代码如下:

QT_HelloWorld12.pro

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2017-08-31T14:54:40
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       += core gui network
 8 
 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 
11 TARGET = QT_HelloWorld12
12 TEMPLATE = app
13 
14 
15 SOURCES += main.cpp
16         widget.cpp 
17     widget_2.cpp
18 
19 HEADERS  += widget.h 
20     widget_2.h
21 
22 FORMS    += widget.ui 
23     widget_2.ui

main.cpp

 1 #include "widget.h"
 2 #include <QApplication>
 3 #include "widget_2.h"
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     Widget w;
 9     w.show();
10     widget_2 w2;
11     w2.show();
12 
13     return a.exec();
14 }

widget.h

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3 
 4 #include <QWidget>
 5 #include <QUdpSocket>//UDP套接字
 6 
 7 namespace Ui {
 8 class Widget;
 9 }
10 
11 class Widget : public QWidget
12 {
13     Q_OBJECT
14 
15 public:
16     explicit Widget(QWidget *parent = 0);
17     ~Widget();
18 
19     void dealMsg();//槽函数,用来处理传递过来的数据
20 
21 private slots:
22     void on_pushButton_send_clicked();
23 
24 private:
25     Ui::Widget *ui;
26 
27     QUdpSocket *udpSocket;
28 };
29 
30 #endif // WIDGET_H

widget_2.h

 1 #ifndef WIDGET_2_H
 2 #define WIDGET_2_H
 3 
 4 #include <QWidget>
 5 #include <QUdpSocket>//UDP套接字
 6 namespace Ui {
 7 class widget_2;
 8 }
 9 
10 class widget_2 : public QWidget
11 {
12     Q_OBJECT
13 
14 public:
15     explicit widget_2(QWidget *parent = 0);
16     ~widget_2();
17     void dealMsg();//槽函数,用来处理传递过来的数据
18 
19 private slots:
20     void on_pushButton_send_clicked();
21 
22 private:
23     Ui::widget_2 *ui;
24     QUdpSocket *udpSocket;
25 };
26 
27 #endif // WIDGET_2_H

widget.cpp

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QUdpSocket>
 4 #include <QHostAddress>
 5 
 6 Widget::Widget(QWidget *parent) :
 7     QWidget(parent),
 8     ui(new Ui::Widget)
 9 {
10     ui->setupUi(this);
11     setWindowTitle("8888");
12     //分配空间,指定父对象
13     udpSocket = new QUdpSocket(this);
14     //绑定 端口号
15     udpSocket->bind(8888);
16 
17     //当对方成功发送数据过来,自动触发readyRead()
18     connect(udpSocket,&QUdpSocket::readyRead,this,&Widget::dealMsg);
19 }
20 
21 Widget::~Widget()
22 {
23     delete ui;
24 }
25 void Widget::dealMsg()
26 {
27     //读取对方发送的内容
28     char buf[1024];
29     QHostAddress address;//对方地址
30     quint16 port;//对方端口
31     qint64 len = udpSocket->readDatagram(buf,sizeof(buf),&address,&port);
32     if(len>0)
33     {
34         //格式化 [127.0.0.1:8888] *******
35         QString str = QString("[%1:%2] %3")
36                 .arg(address.toString().arg(port).arg(buf));
37         //将内容放到编辑区
38         ui->textEdit_write->setText(str);
39     }
40 }
41 
42 void Widget::on_pushButton_send_clicked()
43 {
44     //获取对方的IP和端口
45     QString ip = ui->lineEdit_ip->text();
46     qint16 port = ui->lineEdit_port->text().toInt();
47     //获取编辑区内容
48     QString str = ui->textEdit_write->toPlainText();
49     //给指定的IP发送数据
50     udpSocket->writeDatagram(str.toUtf8(),QHostAddress(ip),port);
51 
52 }

widget_2.cpp

 1 #include "widget_2.h"
 2 #include "ui_widget_2.h"
 3 
 4 widget_2::widget_2(QWidget *parent) :
 5     QWidget(parent),
 6     ui(new Ui::widget_2)
 7 {
 8     ui->setupUi(this);
 9     setWindowTitle("9999");
10     //分配空间,指定父对象
11     udpSocket = new QUdpSocket(this);
12     //绑定 端口号
13     //udpSocket->bind(9999);
14     udpSocket->bind(QHostAddress::AnyIPv4,8888);//如果是组播,则不能直接只绑定端口号(网段所有地址)
15     //加入某个组播
16     //组播地址是D类地址
17     udpSocket->joinMulticastGroup(QHostAddress("224.0.0.2"));
18     //udpSocket->leaveMulticastGroup(QHostAddress("224.0.0.2"));//离开组播
19 
20     //当对方成功发送数据过来,自动触发readyRead()
21     connect(udpSocket,&QUdpSocket::readyRead,this,&widget_2::dealMsg);
22 }
23 
24 widget_2::~widget_2()
25 {
26     delete ui;
27 }
28 void widget_2::dealMsg()
29 {
30     //读取对方发送的内容
31     char buf[1024];
32     QHostAddress address;//对方地址
33         //如果地址是255.255.255.255,则表示广播,在整个局域网内
34     quint16 port;//对方端口
35     qint64 len = udpSocket->readDatagram(buf,sizeof(buf),&address,&port);
36     if(len>0)
37     {
38         //格式化 [127.0.0.1:8888] *******
39         QString str = QString("[%1:%2] %3")
40                 .arg(address.toString().arg(port).arg(buf));
41         //将内容放到编辑区
42         ui->textEdit_write->setText(str);
43     }
44 }
45 
46 void widget_2::on_pushButton_send_clicked()
47 {
48     //获取对方的IP和端口
49     QString ip = ui->lineEdit_ip->text();
50     qint16 port = ui->lineEdit_port->text().toInt();
51     //获取编辑区内容
52     QString str = ui->textEdit_write->toPlainText();
53     //给指定的IP发送数据
54     udpSocket->writeDatagram(str.toUtf8(),QHostAddress(ip),port);
55 }

widget.ui和widget_2.ui的界面设计

原文地址:https://www.cnblogs.com/blog-ccs/p/7459333.html