1、UDP通訊服務器
UDP通訊沒有明確的服務器端和客戶端之分app
TCP通訊像是打電話(必需要接通才能通訊),UDP通訊像是寫信(無論能不能收到都發送出去)函數
首先須要QUdpSOcket套接字,而後綁定bind()端口號和ipui
若是對方發送過來數據,套接字自動觸發readyRead()方法this
套接字QUdpSOcket經過readDatagram()和writeDatagram()方法讀取和寫入數據spa
2、示例代碼以下:設計
QT_HelloWorld12.procode
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.hblog
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的界面設計