124 lines
3.0 KiB
C++
124 lines
3.0 KiB
C++
|
#include "tcpserver.h"
|
||
|
|
||
|
TcpServer::TcpServer(QObject *parent) : QObject(parent)
|
||
|
{
|
||
|
m_server = NULL;
|
||
|
m_curSocket = NULL;
|
||
|
m_clientList.clear();
|
||
|
}
|
||
|
|
||
|
TcpServer::~TcpServer()
|
||
|
{
|
||
|
if(m_server != NULL)
|
||
|
{
|
||
|
delete m_server;
|
||
|
m_server = NULL;
|
||
|
}
|
||
|
|
||
|
if(m_curSocket != NULL)
|
||
|
{
|
||
|
delete m_curSocket;
|
||
|
m_curSocket = NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotNewConnect()
|
||
|
{
|
||
|
m_curSocket = m_server->nextPendingConnection();//获取客户端连接
|
||
|
//获取远端客户端ip和端口号
|
||
|
QString ip = m_curSocket->peerAddress().toString();
|
||
|
int port = m_curSocket->peerPort();
|
||
|
emit siNewClient(ip, port, 1);
|
||
|
|
||
|
CTcpClient client;
|
||
|
client.ip = ip;
|
||
|
client.port = port;
|
||
|
client.socket = m_curSocket;
|
||
|
m_clientList.append(client);
|
||
|
|
||
|
connect(m_curSocket, &QTcpSocket::readyRead, this, &TcpServer::slotRecvData);
|
||
|
connect(m_curSocket, &QTcpSocket::disconnected, this, &TcpServer::slotDisConnect);
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotDisConnect()
|
||
|
{
|
||
|
for (QList<CTcpClient>::iterator it = m_clientList.begin(); it != m_clientList.end(); ++it)
|
||
|
{
|
||
|
if (QAbstractSocket::UnconnectedState == it->socket->state())
|
||
|
{
|
||
|
emit siDisConncet(it->ip, it->port, -1);
|
||
|
it = m_clientList.erase(it, it + 1);
|
||
|
it--;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotInit()
|
||
|
{
|
||
|
m_server = new QTcpServer();
|
||
|
if (!m_server->isListening())
|
||
|
{
|
||
|
m_server->listen(QHostAddress::AnyIPv4, PORT);//监听IPV4的任意地址
|
||
|
connect(m_server, &QTcpServer::newConnection, this, &TcpServer::slotNewConnect);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotRecvData()
|
||
|
{
|
||
|
for (QList<CTcpClient>::iterator it = m_clientList.begin(); it != m_clientList.end(); ++it)
|
||
|
{
|
||
|
if ((it->socket)->isReadable())
|
||
|
{
|
||
|
QByteArray buf = (it->socket)->readAll();
|
||
|
if (!buf.isEmpty())
|
||
|
{
|
||
|
//获取客户端socket的ip和端口号
|
||
|
QString ip = (it->socket)->peerAddress().toString();
|
||
|
int port = (it->socket)->peerPort();
|
||
|
CommMsg msg;
|
||
|
msg.buf = buf;
|
||
|
msg.ip = ip;
|
||
|
msg.port = port;
|
||
|
emit siNewMsg(msg);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotSendData(CommMsg msg)
|
||
|
{
|
||
|
QString ip = msg.ip;
|
||
|
int port = msg.port;
|
||
|
for (QList<CTcpClient>::iterator it = m_clientList.begin(); it != m_clientList.end(); ++it)
|
||
|
{
|
||
|
if (it->ip == ip && it->port == port)
|
||
|
{
|
||
|
it->socket->write(msg.buf);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotStopMonitor()
|
||
|
{
|
||
|
if (m_server->isListening())
|
||
|
{
|
||
|
m_server->disconnect();
|
||
|
m_server->close();
|
||
|
m_server->deleteLater();
|
||
|
m_server = NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TcpServer::slotStartMonitor()
|
||
|
{
|
||
|
if(m_server == NULL)
|
||
|
{
|
||
|
m_server = new QTcpServer();
|
||
|
}
|
||
|
if (!m_server->isListening())
|
||
|
{
|
||
|
m_server->listen(QHostAddress::AnyIPv4, PORT);//监听IPV4的任意地址
|
||
|
connect(m_server, &QTcpServer::newConnection, this, &TcpServer::slotNewConnect);
|
||
|
}
|
||
|
}
|