1 Star 0 Fork 6

RadiumGitee/NetAssistant

forked from micoder/NetAssistant 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TcpServer.cpp 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
shikedj 提交于 2021-01-08 14:25 +08:00 . Format code.
#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());
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/radiumgitee/NetAssistant.git
git@gitee.com:radiumgitee/NetAssistant.git
radiumgitee
NetAssistant
NetAssistant
master

搜索帮助