Qt下實現簡單的UDP通信

来源:http://www.cnblogs.com/wulizhiwen/archive/2017/03/14/6551183.html
-Advertisement-
Play Games

本人呢還是小實習生一枚,剛一腳踏進社會大母親的懷抱,不想找工作的時候碰到的全是培訓機構。。。 不過還是幸運的進了一家。。。咳咳,國企?!好吧,其實是國企下麵的一個分出來的小公司(正在起步中,算是創業公司)。有人收留是好,可惜,與小白我的專業不是非常的合拍!沒辦法,還得邊學邊做。 天哪嚕,鬼知道我第一 ...


天哪嚕,鬼知道我第一天被告知用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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • namespace 員工信息維護{ public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //列表,用於保存 SE 對象 public List<SE> programmerList = n ...
  • //程式員類(員工類) amespace MyOffice { //程式員類(員工類) public class SE { // 工號 public string ID { get; set; } // 年齡 public int Age { get; set; } ///姓名 public str ...
  • using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace MyOffice{ public class SE { ...
  • 突然意識到文字的重要性,於是開始寫了第一個篇博客,博客目的緊緊為記錄,以便溫故。 同時也希望拋磚能達到引玉的作用,歡迎各位來發表自己的感想與想法,以此達到相互學習促進! 背景: 在做百度地圖電子圍欄的時候,一條圍欄內包含人員的設置是直接通過更新圍欄表(Fence)內FenceUser欄位(數據格式: ...
  • 可以在處理Post方法的Action添加一個特性:[ValidateInput(false)],這樣處理就更加有針對性,提高頁面的安全性。 [HttpPost][ValidateInput(false)]public ActionResult CatalogEdit(Catalog model){r ...
  • 第一篇隨筆,以後會陸續的把剛開始工作時的知識點都記錄下來,畢竟現在用WebForm的不多了~ AutoGenerateColumns MSDN 說明 : 獲取或設置一個值,該值指示是否為數據源中的每個欄位自動創建綁定的欄位。 預設值為true 當AutoGenerateColumns=true時,那 ...
  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.... ...
  • RID 是什麼? RID 是運行時標識符的縮寫。 RID 用於標識其中將運行應用程式或資產(即程式集)的目標操作系統。 其外觀類似如下:“ubuntu.14.04-x64”、“win7-x64”、“osx.10.11-x64”。 對於具有本機依賴項的包,它將指定在其中可以還原包的平臺。 Window ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...