QT开发之旅二TCP调试工具

TCP调试工具顾名思义用来调试TCP通信的,网上这样的工具N多,之前用.NET写过一个,无奈在XP下还要安装个.NET框架才能运行,索性这次用QT重写,发现QTTCP通信比.NET还要便捷一些,运行效率貌似要高,还能识别客户端断开,这个真神奇,除了断电之外。

项目名称:TCP调试工具

开发环境:WIN7+QT4.7+QT CREATOR2.8+MINGW

技术实现:通过QTcpServerQTcpSocket类,解析协议并作出处理

实现功能:ASCII格式和16进制数据收发,支持多个客户端收发消息,可以指定客户端发送消息,动态增加和移除已连接客户端。

运行截图:

 

粗略步骤:

第一步:添加主界面,布局好主界面,并命名好控件,例如服务端的清空按钮命名为btnClearServer,客户端的清空按钮命名为btnClearClient

第二步:编写服务端中客户端通信类,服务端可以接受多个客户端的连接,这里采用了同步通信机制,先编写myTcpClient类,封装了客户端连接断开接收数据的操作。具体代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
myTcpClient.h
 
#ifndef MYTCPCLIENT_H
 
#define MYTCPCLIENT_H
 
  
 
#include <QTcpSocket>
 
  
 
class myTcpClient : public QTcpSocket
 
{
 
    Q_OBJECT
 
public:
 
    explicit myTcpClient(QObject *parent = 0,int clientID=0);
 
  
 
private:
 
    int clientID;
 
     
 
signals:
 
    void ClientReadData(int clientID,QString IP,int Port,QByteArray data);
 
    void ClientDisConnect(int clientID,QString IP,int Port);
 
     
 
private slots:
 
    void ReadData();
 
    void DisConnect();
 
  
 
public slots:
 
  
 
};
 
  
 
#endif // MYTCPCLIENT_H
 
  
 
myTcpClient.cpp
 
#include "mytcpclient.h"
 
#include <QHostAddress>
 
#include "myhelper.h"
 
  
 
myTcpClient::myTcpClient(QObject *parent,int clientID) :
 
    QTcpSocket(parent)
 
{   
 
    this->clientID=clientID;
 
    connect(this,SIGNAL(readyRead()),this,SLOT(ReadData()));//挂接读取数据信号
 
    connect(this,SIGNAL(disconnected()),this,SLOT(DisConnect()));//关闭连接时,发送断开连接信号
 
    //如果关闭连接自动删除,则下次不能再次监听,奇怪的问题
 
    //connect(this,SIGNAL(disconnected()),this,SLOT(deleteLater()));//关闭连接时,对象自动删除
 
}
 
  
 
void myTcpClient::ReadData()
 
{
 
    myHelper::Sleep(100);
 
    //读取完整一条数据并发送信号
 
    QByteArray data=this->readAll();
 
    emit ClientReadData(this->clientID,this->peerAddress().toString(),this->peerPort(),data);
 
}
 
  
 
void myTcpClient::DisConnect()
 
{
 
    //断开连接时,发送断开信号
 
    emit ClientDisConnect(this->clientID,this->peerAddress().toString(),this->peerPort());
 
}

 

 

 

一旦客户端断开则发送ClientDisConnect信号,参数包含IP地址和端口。

 

第三步:编写服务端通信类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
myTcpServer.h
 
#ifndef MYTCPSERVER_H
 
#define MYTCPSERVER_H
 
  
 
#include <QTcpServer>
 
#include "mytcpclient.h"
 
  
 
class myTcpServer : public QTcpServer
 
{
 
    Q_OBJECT
 
public:
 
    explicit myTcpServer(QObject *parent = 0);
 
    void SendData(int clientID, QByteArray data);
 
    void SendDataCurrent(QByteArray data);
 
    void SendDataAll(QByteArray data);
 
  
 
    int ClientCount()const{return clientCount;}
 
    void CloseAllClient();
 
  
 
private:
 
    QList<myTcpClient *> ClientList;
 
    QList<int> ClientID;
 
    myTcpClient *CurrentClient;
 
  
 
    int clientCount;
 
  
 
protected:
 
    void incomingConnection(int handle);
 
     
 
signals:
 
    void ClientReadData(int clientID,QString IP,int Port,QByteArray data);
 
    void ClientConnect(int clientID,QString IP,int Port);
 
    void ClientDisConnect(int clientID,QString IP,int Port);
 
     
 
private slots:   
 
    void DisConnect(int clientID,QString IP,int Port);
 
  
 
public slots:
 
     
 
};
 
  
 
#endif // MYTCPSERVER_H
 
  
 
myTcpServer.cpp
 
#include "mytcpserver.h"
 
#include <QHostAddress>
 
  
 
myTcpServer::myTcpServer(QObject *parent) :
 
    QTcpServer(parent)
 
{
 
    this->clientCount=0;
 
}
 
  
 
void myTcpServer::incomingConnection(int handle)
 
{
 
    myTcpClient *client=new myTcpClient(this,handle);
 
    client->setSocketDescriptor(handle);
 
  
 
    connect(client,SIGNAL(ClientReadData(int,QString,int,QByteArray)),this,SIGNAL(ClientReadData(int,QString,int,QByteArray)));
 
    connect(client,SIGNAL(ClientDisConnect(int,QString,int)),this,SLOT(DisConnect(int,QString,int)));
 
  
 
    emit ClientConnect(handle, client->peerAddress().toString(),client->peerPort());
 
  
 
    ClientList.append(client);//将新的连接添加到客户端列表
 
    ClientID.append(handle);//将新的连接的ID添加到客户端ID列表
 
    clientCount++;
 
  
 
    //存储当前连接
 
    CurrentClient=client;
 
}
 
  
 
void myTcpServer::DisConnect(int clientID,QString IP,int Port)
 
{
 
    for (int i=0;i<clientCount;i++)
 
    {
 
        if (ClientID[i]==clientID)
 
        {
 
            ClientList.removeAt(i);//从列表中移除该连接
 
            ClientID.removeAt(i);
 
            clientCount--;
 
            i--;//不然的话,永远只会移除第一个连接
 
            emit ClientDisConnect(clientID,IP,Port);
 
            break;
 
        }
 
    }
 
}
 
  
 
//指定客户端连接发消息
 
void myTcpServer::SendData(int clientID, QByteArray data)
 
{
 
    for (int i=0;i<clientCount;i++)
 
    {
 
        if (ClientID[i]==clientID)
 
        {
 
            ClientList[i]->write(data);
 
            break;
 
        }
 
    }
 
}
 
  
 
//对当前连接发送数据
 
void myTcpServer::SendDataCurrent(QByteArray data)
 
{
 
    //如果没有一个存在的连接,则不处理
 
    if (clientCount<1){return;}
 
    CurrentClient->write(data);
 
}
 
  
 
//对所有连接发送数据
 
void myTcpServer::SendDataAll(QByteArray data)
 
{
 
    for (int i=0;i<clientCount;i++)
 
    {
 
        ClientList[i]->write(data);
 
    }
 
}
 
  
 
void myTcpServer::CloseAllClient()
 
{
 
    for (int i=0;i<clientCount;i++)
 
    {
 
        ClientList[i]->close();
 
        i--;//不然的话,永远只会断开第一个连接
 
    }
 
}

  

这里封装了指定客户端发消息,对当前连接发消息,对所有客户端发消息三种发送消息方法。

最开始的时候发现直接close停止监听,发现依然可以接收客户端的消息,原因是还没有关闭客户端连接,所以增加了CloseAllClient()方法,用来关闭所有客户端连接,这样的话才是彻底的停止监听。

可执行文件下载地址:http://download.csdn.net/detail/feiyangqingyun/6717009

源码猛点这里:http://download.csdn.net/detail/feiyangqingyun/6717017

posted @   飞扬青云  阅读(5676)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示