-
Notifications
You must be signed in to change notification settings - Fork 110
/
TcpServer.cpp
94 lines (81 loc) · 3.2 KB
/
TcpServer.cpp
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
#include "TcpServer.h"
#include <QTcpSocket>
/*==============================================================*/
TcpServer::TcpServer(QObject *parent): QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(acceptNewClient()));
}
/*==============================================================*/
//accept a new client and set the SIGNAL and SLOT
void TcpServer::acceptNewClient()
{
int socketDescriptor;
QTcpSocket *tcpClientSocket = nextPendingConnection();
socketDescriptor = tcpClientSocket->socketDescriptor();
connect(tcpClientSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(tcpClientSocket, SIGNAL(disconnected()), tcpClientSocket, SLOT(deleteLater()));
connect(tcpClientSocket, SIGNAL(readyRead()), this, SLOT(clientDataReceived()));
tcpClientSocketList.append(tcpClientSocket); //add client
//send the client address and descriptor to NetAssistWidget
QString peerPortStr = QString::number(tcpClientSocket->peerPort());
QString rdClientAddress_Port = tcpClientSocket->peerAddress().toString() + ":" + peerPortStr;
emit addClientLink(rdClientAddress_Port, socketDescriptor);
}
/*==============================================================*/
//send data to a client or all client
void TcpServer::sendDataToClient(char *msg, int length, int socketDescriptor, int socketDescriptorEx)
{
for(int i = 0; i < tcpClientSocketList.count(); i++)
{
QTcpSocket *item = tcpClientSocketList.at(i);
if(socketDescriptor == 0) {
if(item->socketDescriptor() != socketDescriptorEx) {
if(item->write(msg, length) != length)
{
continue;
}
}
} else {
if(item->socketDescriptor() == socketDescriptor) {
if(item->write(msg, length) != length)
{
break;
}
}
}
}
}
/*==============================================================*/
//send disconnect a valid client tcpsocket
void TcpServer::clientDisconnected()
{
for(int i = 0; i < tcpClientSocketList.count(); i++)
{
QTcpSocket *item = tcpClientSocketList.at(i);
if(item->state() == 0)
{
QString peerPortStr = QString::number(item->peerPort());
QString rdClientAddress_Port = item->peerAddress().toString() + ":" + peerPortStr;
//send the client address and descriptor to NetAssistWidget
emit removeClientLink(rdClientAddress_Port, item->socketDescriptor());
tcpClientSocketList.removeAt(i); //remove Client
break;
}
}
}
/*==============================================================*/
//processe a client data
void TcpServer::clientDataReceived()
{
for(int i = 0; i < tcpClientSocketList.count(); i++)
{
QTcpSocket *item = tcpClientSocketList.at(i);
while(item->bytesAvailable() > 0)
{
QByteArray datagram;
datagram.resize(item->bytesAvailable());
item->read(datagram.data(), datagram.size());
emit updateTcpServer((char *)datagram.data(), datagram.size(), item->socketDescriptor());
}
}
}