代码拉取完成,页面将自动刷新
同步操作将从 micoder/NetAssistant 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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());
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。