-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytcpserver.cpp
More file actions
39 lines (32 loc) · 940 Bytes
/
Copy pathmytcpserver.cpp
File metadata and controls
39 lines (32 loc) · 940 Bytes
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
#include "mytcpserver.h"
MyTcpServer::MyTcpServer(QObject *parent) : QTcpServer(parent)
{
}
MyTcpServer& MyTcpServer::getInstance()
{
static MyTcpServer instance;
return instance;
}
void MyTcpServer::incomingConnection(qintptr socketDescriptor)
{
qDebug() << "a new connect";
MyTcpSocket *ptcpSocket = new MyTcpSocket;
ptcpSocket->setSocketDescriptor(socketDescriptor); // 将现有的套接字描述符关联到QTcpSocket对象上
m_tcpSocketList.append(ptcpSocket);
connect(ptcpSocket, SIGNAL(offline(MyTcpSocket*))
,this, SLOT(deleteSocket(MyTcpSocket*)));
}
void MyTcpServer::deleteSocket(MyTcpSocket* addr)
{
QList<MyTcpSocket*>::iterator iter = m_tcpSocketList.begin();
while(iter != m_tcpSocketList.end())
{
if(*iter == addr)
{
delete *iter;
m_tcpSocketList.erase(iter);
*iter = NULL;
break;
}
}
}