Qt下实现简单的UDP通信

天哪噜,鬼知道我第一天被告知用QT做一个UDP通信的小玩意的时候,是种怎样的心情?!

哈哈,好了,言归正传。下面就是一个这样的小东西。

(原博主:http://blog.csdn.net/jdh99,因为我是初学者,也是看了原博主的博文,才知道怎么做的。分享好资源,并附上原博主未附上的ui文件)

.pro文件

这个只需要添加一行QT     += network就行。

main.cpp文件

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

widget.ui文件

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>Widget</class>
  4  <widget class="QWidget" name="Widget">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>610</width>
 10     <height>552</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>Widget</string>
 15   </property>
 16   <widget class="QTextEdit" name="txt_ip">
 17    <property name="geometry">
 18     <rect>
 19      <x>110</x>
 20      <y>60</y>
 21      <width>91</width>
 22      <height>31</height>
 23     </rect>
 24    </property>
 25   </widget>
 26   <widget class="QTextEdit" name="txt_port_tx">
 27    <property name="geometry">
 28     <rect>
 29      <x>350</x>
 30      <y>60</y>
 31      <width>81</width>
 32      <height>31</height>
 33     </rect>
 34    </property>
 35   </widget>
 36   <widget class="QTextEdit" name="txt_port_rx">
 37    <property name="geometry">
 38     <rect>
 39      <x>110</x>
 40      <y>150</y>
 41      <width>91</width>
 42      <height>31</height>
 43     </rect>
 44    </property>
 45   </widget>
 46   <widget class="QTextEdit" name="txt_tx">
 47    <property name="geometry">
 48     <rect>
 49      <x>40</x>
 50      <y>270</y>
 51      <width>391</width>
 52      <height>81</height>
 53     </rect>
 54    </property>
 55   </widget>
 56   <widget class="QTextEdit" name="txt_rx">
 57    <property name="geometry">
 58     <rect>
 59      <x>40</x>
 60      <y>420</y>
 61      <width>391</width>
 62      <height>81</height>
 63     </rect>
 64    </property>
 65   </widget>
 66   <widget class="QLabel" name="label">
 67    <property name="geometry">
 68     <rect>
 69      <x>40</x>
 70      <y>70</y>
 71      <width>54</width>
 72      <height>12</height>
 73     </rect>
 74    </property>
 75    <property name="text">
 76     <string>发送IP:</string>
 77    </property>
 78   </widget>
 79   <widget class="QLabel" name="label_2">
 80    <property name="geometry">
 81     <rect>
 82      <x>260</x>
 83      <y>70</y>
 84      <width>54</width>
 85      <height>12</height>
 86     </rect>
 87    </property>
 88    <property name="text">
 89     <string>发送端口:</string>
 90    </property>
 91   </widget>
 92   <widget class="QLabel" name="label_3">
 93    <property name="geometry">
 94     <rect>
 95      <x>40</x>
 96      <y>160</y>
 97      <width>54</width>
 98      <height>12</height>
 99     </rect>
100    </property>
101    <property name="text">
102     <string>接收端口:</string>
103    </property>
104   </widget>
105   <widget class="QLabel" name="label_4">
106    <property name="geometry">
107     <rect>
108      <x>40</x>
109      <y>240</y>
110      <width>54</width>
111      <height>12</height>
112     </rect>
113    </property>
114    <property name="text">
115     <string>发送框</string>
116    </property>
117   </widget>
118   <widget class="QLabel" name="label_5">
119    <property name="geometry">
120     <rect>
121      <x>40</x>
122      <y>390</y>
123      <width>54</width>
124      <height>12</height>
125     </rect>
126    </property>
127    <property name="text">
128     <string>接收框</string>
129    </property>
130   </widget>
131   <widget class="QPushButton" name="btn_cfg">
132    <property name="geometry">
133     <rect>
134      <x>260</x>
135      <y>160</y>
136      <width>75</width>
137      <height>23</height>
138     </rect>
139    </property>
140    <property name="text">
141     <string>配置</string>
142    </property>
143   </widget>
144   <widget class="QPushButton" name="btn_tx">
145    <property name="geometry">
146     <rect>
147      <x>350</x>
148      <y>230</y>
149      <width>75</width>
150      <height>23</height>
151     </rect>
152    </property>
153    <property name="text">
154     <string>发送</string>
155    </property>
156   </widget>
157  </widget>
158  <layoutdefault spacing="6" margin="11"/>
159  <resources/>
160  <connections/>
161 </ui>
View Code

widget.cpp文件

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 
 4 Widget::Widget(QWidget *parent) :
 5     QWidget(parent),
 6     ui(new Ui::Widget)
 7 {
 8     ui->setupUi(this);
 9 
10     udp_socket_tx = new QUdpSocket(this);
11     udp_socket_rx = new QUdpSocket(this);
12 
13     ui->btn_tx->setEnabled(false);
14 }
15 
16 Widget::~Widget()
17 {
18     delete ui;
19 }
20 
21 void Widget::rx_udp()
22 {
23     qDebug() << "rx";
24 
25     while(udp_socket_rx->hasPendingDatagrams())
26     {
27         QByteArray datagram;
28         datagram.resize(udp_socket_rx->pendingDatagramSize());
29 
30         QHostAddress sender;
31         quint16 senderPort;
32 
33         udp_socket_rx->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
34 
35         ui->txt_rx->append(datagram);
36     }
37 }
38 
39 
40 void Widget::on_btn_tx_clicked()
41 {
42     QByteArray datagram = ui->txt_tx->toPlainText().toUtf8();
43     udp_socket_tx->writeDatagram(datagram, datagram.size(), Ip_Tx, Port_Tx);
44 
45     QString message = datagram;
46     ui->txt_rx->insertPlainText(message);
47 
48 }
49 
50 
51 void Widget::on_btn_cfg_clicked()
52 {
53     bool ok;
54     int port_rx = 0;
55 
56     Ip_Tx = QHostAddress(ui->txt_ip->toPlainText());
57     Port_Tx = ui->txt_port_tx->toPlainText().toInt(&ok);
58 
59     port_rx = ui->txt_port_rx->toPlainText().toInt(&ok);
60     udp_socket_rx->bind(QHostAddress::Any, port_rx);
61 
62     connect(udp_socket_rx, SIGNAL(readyRead()),this, SLOT(rx_udp()));
63 
64     ui->btn_tx->setEnabled(true);
65 }
View Code

widget.h文件

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3 
 4 #include <QWidget>
 5 #include <QUdpSocket>
 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 private:
20     Ui::Widget *ui;
21 
22     QUdpSocket *udp_socket_tx;
23     QUdpSocket *udp_socket_rx;
24     QHostAddress Ip_Tx;
25     int Port_Tx;
26 
27 private slots:
28     void on_btn_cfg_clicked();
29     void on_btn_tx_clicked();
30     void rx_udp();
31 
32 };
33 
34 #endif // WIDGET_H
View Code

 

posted @ 2017-03-14 22:39  初闻  阅读(1834)  评论(0)    收藏  举报