Socket网络编程

 Step1:首先完成整个界面的设计

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>421</width>
  10. <height>533</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <widget class="QLineEdit" name="IP_Address">
  18. <property name="geometry">
  19. <rect>
  20. <x>30</x>
  21. <y>140</y>
  22. <width>113</width>
  23. <height>27</height>
  24. </rect>
  25. </property>
  26. </widget>
  27. <widget class="QLabel" name="label_4">
  28. <property name="geometry">
  29. <rect>
  30. <x>20</x>
  31. <y>100</y>
  32. <width>171</width>
  33. <height>21</height>
  34. </rect>
  35. </property>
  36. <property name="font">
  37. <font>
  38. <pointsize>10</pointsize>
  39. <italic>true</italic>
  40. <underline>false</underline>
  41. <strikeout>false</strikeout>
  42. </font>
  43. </property>
  44. <property name="cursor">
  45. <cursorShape>BlankCursor</cursorShape>
  46. </property>
  47. <property name="text">
  48. <string>Designed by : mm1994uestc</string>
  49. </property>
  50. <property name="alignment">
  51. <set>Qt::AlignCenter</set>
  52. </property>
  53. </widget>
  54. <widget class="QLabel" name="label_6">
  55. <property name="geometry">
  56. <rect>
  57. <x>0</x>
  58. <y>-10</y>
  59. <width>321</width>
  60. <height>51</height>
  61. </rect>
  62. </property>
  63. <property name="font">
  64. <font>
  65. <pointsize>28</pointsize>
  66. <italic>true</italic>
  67. <underline>false</underline>
  68. <strikeout>false</strikeout>
  69. </font>
  70. </property>
  71. <property name="cursor">
  72. <cursorShape>BlankCursor</cursorShape>
  73. </property>
  74. <property name="text">
  75. <string>Qt interface Demo!</string>
  76. </property>
  77. <property name="alignment">
  78. <set>Qt::AlignCenter</set>
  79. </property>
  80. </widget>
  81. <widget class="QLabel" name="label_5">
  82. <property name="geometry">
  83. <rect>
  84. <x>0</x>
  85. <y>40</y>
  86. <width>171</width>
  87. <height>51</height>
  88. </rect>
  89. </property>
  90. <property name="font">
  91. <font>
  92. <pointsize>16</pointsize>
  93. <italic>true</italic>
  94. <underline>false</underline>
  95. <strikeout>false</strikeout>
  96. </font>
  97. </property>
  98. <property name="cursor">
  99. <cursorShape>BlankCursor</cursorShape>
  100. </property>
  101. <property name="text">
  102. <string>Socket Control</string>
  103. </property>
  104. <property name="alignment">
  105. <set>Qt::AlignCenter</set>
  106. </property>
  107. </widget>
  108. <widget class="QLineEdit" name="Client_Port">
  109. <property name="geometry">
  110. <rect>
  111. <x>200</x>
  112. <y>140</y>
  113. <width>51</width>
  114. <height>27</height>
  115. </rect>
  116. </property>
  117. </widget>
  118. <widget class="QPushButton" name="Client_Button">
  119. <property name="geometry">
  120. <rect>
  121. <x>260</x>
  122. <y>140</y>
  123. <width>151</width>
  124. <height>27</height>
  125. </rect>
  126. </property>
  127. <property name="text">
  128. <string>TCP-Client-Connect</string>
  129. </property>
  130. </widget>
  131. <widget class="QLabel" name="label">
  132. <property name="geometry">
  133. <rect>
  134. <x>10</x>
  135. <y>130</y>
  136. <width>21</width>
  137. <height>41</height>
  138. </rect>
  139. </property>
  140. <property name="text">
  141. <string>IP:</string>
  142. </property>
  143. </widget>
  144. <widget class="QLabel" name="label_2">
  145. <property name="geometry">
  146. <rect>
  147. <x>150</x>
  148. <y>130</y>
  149. <width>51</width>
  150. <height>41</height>
  151. </rect>
  152. </property>
  153. <property name="text">
  154. <string>PORT:</string>
  155. </property>
  156. </widget>
  157. <widget class="QLineEdit" name="Server_Port">
  158. <property name="geometry">
  159. <rect>
  160. <x>60</x>
  161. <y>320</y>
  162. <width>51</width>
  163. <height>27</height>
  164. </rect>
  165. </property>
  166. </widget>
  167. <widget class="QLabel" name="label_3">
  168. <property name="geometry">
  169. <rect>
  170. <x>10</x>
  171. <y>310</y>
  172. <width>51</width>
  173. <height>41</height>
  174. </rect>
  175. </property>
  176. <property name="text">
  177. <string>PORT:</string>
  178. </property>
  179. </widget>
  180. <widget class="QPushButton" name="Server_Button">
  181. <property name="geometry">
  182. <rect>
  183. <x>120</x>
  184. <y>320</y>
  185. <width>161</width>
  186. <height>27</height>
  187. </rect>
  188. </property>
  189. <property name="text">
  190. <string>TCP-Server-Monitor</string>
  191. </property>
  192. </widget>
  193. <widget class="QLabel" name="label_7">
  194. <property name="geometry">
  195. <rect>
  196. <x>10</x>
  197. <y>170</y>
  198. <width>151</width>
  199. <height>41</height>
  200. </rect>
  201. </property>
  202. <property name="text">
  203. <string>Data Ricieve Frame:</string>
  204. </property>
  205. </widget>
  206. <widget class="QLabel" name="label_8">
  207. <property name="geometry">
  208. <rect>
  209. <x>230</x>
  210. <y>170</y>
  211. <width>151</width>
  212. <height>41</height>
  213. </rect>
  214. </property>
  215. <property name="text">
  216. <string>Data Send Frame:</string>
  217. </property>
  218. </widget>
  219. <widget class="QLabel" name="label_9">
  220. <property name="geometry">
  221. <rect>
  222. <x>10</x>
  223. <y>350</y>
  224. <width>151</width>
  225. <height>41</height>
  226. </rect>
  227. </property>
  228. <property name="text">
  229. <string>Data Ricieve Frame:</string>
  230. </property>
  231. </widget>
  232. <widget class="QLabel" name="label_10">
  233. <property name="geometry">
  234. <rect>
  235. <x>230</x>
  236. <y>350</y>
  237. <width>151</width>
  238. <height>41</height>
  239. </rect>
  240. </property>
  241. <property name="text">
  242. <string>Data Send Frame:</string>
  243. </property>
  244. </widget>
  245. <widget class="QLabel" name="Corver">
  246. <property name="geometry">
  247. <rect>
  248. <x>220</x>
  249. <y>40</y>
  250. <width>171</width>
  251. <height>91</height>
  252. </rect>
  253. </property>
  254. <property name="text">
  255. <string> Corver</string>
  256. </property>
  257. </widget>
  258. <widget class="QPushButton" name="pushButton">
  259. <property name="geometry">
  260. <rect>
  261. <x>350</x>
  262. <y>180</y>
  263. <width>51</width>
  264. <height>27</height>
  265. </rect>
  266. </property>
  267. <property name="text">
  268. <string>Send</string>
  269. </property>
  270. </widget>
  271. <widget class="QPushButton" name="ServerSend">
  272. <property name="geometry">
  273. <rect>
  274. <x>350</x>
  275. <y>360</y>
  276. <width>51</width>
  277. <height>27</height>
  278. </rect>
  279. </property>
  280. <property name="text">
  281. <string>Send</string>
  282. </property>
  283. </widget>
  284. <widget class="QPlainTextEdit" name="Client_Recieve">
  285. <property name="geometry">
  286. <rect>
  287. <x>10</x>
  288. <y>210</y>
  289. <width>171</width>
  290. <height>101</height>
  291. </rect>
  292. </property>
  293. </widget>
  294. <widget class="QPlainTextEdit" name="Client_Send">
  295. <property name="geometry">
  296. <rect>
  297. <x>230</x>
  298. <y>210</y>
  299. <width>171</width>
  300. <height>101</height>
  301. </rect>
  302. </property>
  303. </widget>
  304. <widget class="QPlainTextEdit" name="Server_Recieve">
  305. <property name="geometry">
  306. <rect>
  307. <x>10</x>
  308. <y>390</y>
  309. <width>171</width>
  310. <height>101</height>
  311. </rect>
  312. </property>
  313. </widget>
  314. <widget class="QPlainTextEdit" name="Server_Send">
  315. <property name="geometry">
  316. <rect>
  317. <x>230</x>
  318. <y>390</y>
  319. <width>171</width>
  320. <height>101</height>
  321. </rect>
  322. </property>
  323. </widget>
  324. <widget class="QPushButton" name="Client_Clear">
  325. <property name="geometry">
  326. <rect>
  327. <x>150</x>
  328. <y>180</y>
  329. <width>51</width>
  330. <height>27</height>
  331. </rect>
  332. </property>
  333. <property name="text">
  334. <string>Clear</string>
  335. </property>
  336. </widget>
  337. <widget class="QPushButton" name="Server_Clear">
  338. <property name="geometry">
  339. <rect>
  340. <x>150</x>
  341. <y>360</y>
  342. <width>51</width>
  343. <height>27</height>
  344. </rect>
  345. </property>
  346. <property name="text">
  347. <string>Clear</string>
  348. </property>
  349. </widget>
  350. </widget>
  351. <widget class="QMenuBar" name="menuBar">
  352. <property name="geometry">
  353. <rect>
  354. <x>0</x>
  355. <y>0</y>
  356. <width>421</width>
  357. <height>25</height>
  358. </rect>
  359. </property>
  360. </widget>
  361. <widget class="QToolBar" name="mainToolBar">
  362. <attribute name="toolBarArea">
  363. <enum>TopToolBarArea</enum>
  364. </attribute>
  365. <attribute name="toolBarBreak">
  366. <bool>false</bool>
  367. </attribute>
  368. </widget>
  369. <widget class="QStatusBar" name="statusBar"/>
  370. </widget>
  371. <layoutdefault spacing="6" margin="11"/>
  372. <resources/>
  373. <connections/>
  374. </ui>

Ubuntu 16.04 Edition Qt Socket Project                                                                           Windows 10 端的Socket Tool

 

整个界面的样式如图所示,包括了TCP Client以及TCP Server端。

IP表示客户端将要连接的TCP Server的服务器ip地址,PORT填入的是对应的TCP Server的地址开放的监听的端口号

Clear按钮是用来清楚Server或者Client的接收空间,Send是用来发送发送栏当中的数据的。其他按钮用来开启对应的服务。

Step2:相关槽函数已经接收读取函数的实现

  1. void MainWindow::Client_Recieve_Data(void)
  2. {
  3. QByteArray buf;
  4. buf = client->readAll();
  5. if(!buf.isEmpty())
  6. {
  7. QString str = this->ui->Client_Recieve->toPlainText();
  8. str += tr(buf);
  9. this->ui->Client_Recieve->clear();
  10. this->ui->Client_Recieve->appendPlainText(str);
  11. }
  12. buf.clear();
  13. }
  14.  
  15. void MainWindow::on_Client_Button_clicked(bool checked)
  16. {
  17. if(!ClientFlag)
  18. {
  19. QString IP,PORT;
  20. IP = this->ui->IP_Address->text();
  21. PORT = this->ui->Client_Port->text();
  22.  
  23. qDebug() << IP;
  24. qDebug() << PORT;
  25.  
  26. client->connectToHost(QHostAddress(IP),PORT.toInt(),QTcpSocket::ReadWrite);
  27. qDebug() << "Client is Started!";
  28.  
  29. QObject::connect(client,&QTcpSocket::readyRead,this,&MainWindow::Client_Recieve_Data);
  30. this->ui->Client_Button->setStyleSheet("background-color: rgb(85,170,0);");
  31. }
  32. else
  33. {
  34. this->ui->Client_Button->setStyleSheet("background-color: rgb(0,0,0);");
  35. client->close();
  36. qDebug() << "Client is Closed!";
  37. }
  38.  
  39. ClientFlag = !ClientFlag;
  40. }
  41.  
  42. void MainWindow::on_pushButton_clicked(bool checked)
  43. {
  44. if(ClientFlag)
  45. {
  46. QString Send_data;
  47. Send_data = this->ui->Client_Send->toPlainText();
  48. qDebug() << Send_data;
  49. client->write(Send_data.toLatin1());
  50. qDebug() << "Data sended!";
  51. }
  52. }
  53.  
  54. void MainWindow::on_Client_Clear_clicked(bool checked)
  55. {
  56. this->ui->Client_Recieve->clear();
  57. qDebug() << "Client_Recieve_CLear!";
  58. }
  59.  
  60. /************************************************************
  61. * Server Side Programming!
  62. * The Qt Socket Programming have ever finished binding the port!
  63. * The accept func is trigger by a new connection signal!
  64. ***********************************************************/
  65.  
  66. void MainWindow::readClient(void)
  67. {
  68. QByteArray buf;
  69. buf = client_connect->readAll();
  70. if(!buf.isEmpty())
  71. {
  72. QString str = this->ui->Server_Recieve->toPlainText();
  73. str += tr(buf);
  74. this->ui->Server_Recieve->clear();
  75. this->ui->Server_Recieve->appendPlainText(str);
  76. }
  77. buf.clear();
  78. }
  79.  
  80. void MainWindow::accepConnection(void)
  81. {
  82. client_connect = server->nextPendingConnection();
  83. QObject::connect(client_connect,&QTcpSocket::readyRead,this,&MainWindow::readClient);
  84. }
  85.  
  86. void MainWindow::on_Server_Clear_clicked(bool checked)
  87. {
  88. this->ui->Server_Recieve->clear();
  89. qDebug() << "Server_Recieve_Clear!";
  90. }
  91.  
  92. void MainWindow::on_ServerSend_clicked(bool checked)
  93. {
  94. if(ServerFlag)
  95. {
  96. QString SendData;
  97. SendData = this->ui->Server_Send->toPlainText();
  98. client_connect->write(SendData.toLatin1());
  99.  
  100. qDebug() << SendData;
  101. qDebug() << "ServerData send!";
  102. }
  103. }
  104.  
  105. void MainWindow::on_Server_Button_clicked(bool checked)
  106. {
  107. if(!ServerFlag)
  108. {
  109. QString MonitorPort;
  110. MonitorPort = this->ui->Server_Port->text();
  111.  
  112. int PortNum;
  113. PortNum = MonitorPort.toInt();
  114.  
  115. server->listen(QHostAddress::Any,PortNum);
  116.  
  117. QObject::connect(server,&QTcpServer::newConnection,this,&MainWindow::accepConnection);
  118.  
  119. qDebug() << "Server Started!";
  120. qDebug() << "PortNum:" << PortNum;
  121. this->ui->Server_Button->setStyleSheet("background-color: rgb(85,170,0);");
  122. }
  123. else
  124. {
  125. this->ui->Server_Button->setStyleSheet("background-color: rgb(0,0,0);");
  126. client_connect->close();
  127. server->close();
  128. qDebug() << "Server Closed!";
  129. }
  130. ServerFlag = !ServerFlag;
  131. }

Step3:槽函数的声明&Socket对象定义

  1. private slots:
  2. void on_Client_Button_clicked(bool checked);
  3.  
  4. void on_pushButton_clicked(bool checked);
  5.  
  6. void Client_Recieve_Data(void);
  7.  
  8. void accepConnection(void);
  9.  
  10. void readClient(void);
  11.  
  12. void on_Client_Clear_clicked(bool checked);
  13.  
  14. void on_Server_Clear_clicked(bool checked);
  15.  
  16. void on_ServerSend_clicked(bool checked);
  17.  
  18. void on_Server_Button_clicked(bool checked);
  19.  
  20. void TimeOutProcess(void);
  21.  
  22. private:
  23. Ui::MainWindow *ui;
  24.  
  25. QTcpSocket *client;
  26.  
  27. QTcpServer *server;
  28. QTcpSocket *client_connect;
  29.  
  30. QTimer *TimeMonitor;
  31.  
  32. bool ClientFlag = false;
  33. bool ServerFlag = false;

Step4:信号和槽的连接

connect(this->ui->Client_Button,SIGNAL(clicked(bool)),this,SLOT(on_Client_Button_clicked()));
connect(this->ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(on_pushButton_clicked()));

connect(this->ui->Client_Clear,SIGNAL(clicked(bool)),this,SLOT(on_Client_Clear_clicked()));
connect(this->ui->Server_Clear,SIGNAL(clicked(bool)),this,SLOT(on_Server_Clear_clicked()));

connect(this->ui->ServerSend,SIGNAL(clicked(bool)),this,SLOT(on_ServerSend_clicked()));

connect(this->ui->Server_Button,SIGNAL(clicked(bool)),this,SLOT(on_Server_Button_clicked()));

connect(TimeMonitor,&QTimer::timeout,this,&MainWindow::TimeOutProcess);

完成上述操作,基本就完成了Qt Socket程序的功能。

3、Qt Project之Socket网络编程的更多相关文章

  1. windows socket 网络编程

    样例代码就在我的博客中,包含六个UDP和TCP发送接受的cpp文件,一个基于MFC的局域网聊天小工具project,和此小工具的全部执行时库.资源和执行程序.代码的压缩包位置是http://www.b ...

  2. Linux Socket 网络编程

    Linux下的网络编程指的是socket套接字编程,入门比较简单.在学校里学过一些皮毛,平时就是自学玩,没有见识过真正的socket编程大程序,比较遗憾.总感觉每次看的时候都有收获,但是每次看完了之后 ...

  3. Python Socket 网络编程

    Socket 是进程间通信的一种方式,它与其他进程间通信的一个主要不同是:它能实现不同主机间的进程间通信,我们网络上各种各样的服务大多都是基于 Socket 来完成通信的,例如我们每天浏览网页.QQ ...

  4. Python全栈【Socket网络编程】

    Python全栈[socket网络编程] 本章内容: Socket 基于TCP的套接字 基于UDP的套接字 TCP粘包 SocketServer 模块(ThreadingTCPServer源码剖析) ...

  5. python之Socket网络编程

    什么是网络? 网络是由节点和连线构成,表示诸多对象及其相互联系.在数学上,网络是一种图,一般认为专指加权图.网络除了数学定义外,还有具体的物理含义,即网络是从某种相同类型的实际问题中抽象出来的模型.在 ...

  6. Python之路【第七篇】python基础 之socket网络编程

    本篇文章大部分借鉴 http://www.cnblogs.com/nulige/p/6235531.html python socket  网络编程 一.服务端和客户端 BS架构 (腾讯通软件:ser ...

  7. Socket网络编程-基础篇

    Socket网络编程 网络通讯三要素: IP地址[主机名] 网络中设备的标识 本地回环地址:127.0.0.1 主机名:localhost 端口号 用于标识进程的逻辑地址 有效端口:0~65535 其 ...

  8. Socket网络编程--FTP客户端

    Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...

  9. windows下的socket网络编程

    windows下的socket网络编程 windows下的socket网络编程 clinet.c 客户端 server.c 服务器端 UDP通信的实现 代码如下 已经很久没有在windows下编程了, ...

随机推荐

  1. 给Linux增加swap内存

    有时内存不足时, 编译xxx报错cc: 编译器内部错误:已杀死(程序 cc1) Please submit a full bug report, with preprocessed source if ...

  2. swift 实践- 13 -- UIStepper

    import UIKit class ViewController: UIViewController { var stepper: UIStepper! var label: UILabel! ov ...

  3. Confluence 6 配置一个 Confluence 环境

    本部分对你 Confluence 的外部设置进行描述.包括有如何配置 Web 服务器,应用服务器,目录和文件等信息—— Confluence 运行所需要的所有环境.有关在服务器内部对配置进行修改的内容 ...

  4. Confluence 6 导入一个文本文件

    Confluence 允许你从 Confluence 服务器上的一个目录中导入一个文本文件,然后将这个文本文件转换为 Confluence 的页面.每一个文本文件将会在 Confluence 中创建一 ...

  5. cf441 f组合数。。单调指针

    e没学过做不出来.. 处理合法的区间很麻烦,但是处理非合法的区间很容易 答案就是所有的取法-不合法的区间 这题一定要双边界推进处理!!!! 一开始用单边界向右推进,结果后来发现错了,拿样例1就可以证明 ...

  6. ajax之全局函数

    1.全局函数:$.each(array,function(){1,value}),通过$/jQuery直接调用 对象函数:$("#name").val(); jQuery UI:$ ...

  7. tensorflow:验证码的识别(上)

    验证码的识别 主要分成四个部分:验证码的生成.将生成的图片制作成tfrecord文件.训练识别模型.测试模型 使用pyCharm作为编译器.本文先介绍前两个部分 验证码的识别有两种方法: 验证码识别方 ...

  8. Python Day-1 练习

    作业1 要求:1.输入用户密码 2.认真成功后显示欢迎信息 3.输入三次锁定 代码如下: __author__ = 'zhang.ning' username = "zhangning&qu ...

  9. 用C语言实现窗口抖动

    #include "stdafx.h" #include <stdio.h> #include<Windows.h> int main() { ; //休眠 ...

  10. SQLServer 常见高CPU利用率原因

    1.缺失索引: USE AdventureWorks2014 SET STATISTICS TIME ON; SET STATISTICS IO ON ; SELECT per.FirstName,p ...