/** *函数介绍:初始化客户端 *输入参数:无 *返回值:无 */ void Network::initClient() { //写入系统日志 //Global::systemLog->append(QString(tr("初始化网络客户端:%1.....")).arg(clientName), clientName + tr("客户端初始化."), SystemLog::LOG); //新建一个客户端socket clientSocket = new TcpSocket(); //初始化它的永久描述符,在断开后,未删除前仍有效 clientSocket->setSocketDescriptorEternal(clientSocket->socketDescriptor()); //信号槽:跟踪clientSocket状态 connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(getClientSocketState(QAbstractSocket::SocketState))); //信号槽: 客户端接收远程计算机数据 connect(clientSocket, SIGNAL(haveReadDataSignal(QByteArray, int)), this, SLOT(clientReceiveData(QByteArray, int))); //信号槽: 客户端连接服务器成功 connect(clientSocket, SIGNAL(connectedSignal(int)), this, SLOT(clientSocketConnectSuccess(int))); //信号槽: 客户端关闭与服务器连接 connect(clientSocket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(clientSocketDisconnect(int))); //信号槽: 客户端socket出现错误 connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(clientSocketError(QAbstractSocket::SocketError))); connect(clientSocket, SIGNAL(disconnected()), this, SLOT(disconnectSlot()) ); //写入系统日志 //Global::systemLog->append(QString(tr("初始化网络客户端:%1结束.")).arg(clientName), clientName + tr("客户端初始化结束."), SystemLog::LOG); }
FileTransferServer::~FileTransferServer() { if (m_state != IDLE) { qDebug() << "Still runnin'"; disconnectSlot(); } qDebug() << "file off"; delete m_server; }
void FileTransferServer::addNewClient() { QTcpSocket *client; if(m_server->hasPendingConnections()) { client = m_server->nextPendingConnection(); m_socket = new ServerClient(client, m_file, m_sender); m_state = CONNECTED; connect(&m_serverThread, SIGNAL(finished()), this, SLOT(threadFinished())); m_socket->sendInit(); if (m_sender == true) m_socket->sendFile(); connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnectSlot())); } }
void ClientConnection::disconnectFromAliasSynch() { if (!m_isConnected) { return; } m_isConnected = false; disconnect(m_socket, SIGNAL(disconnected()), this, SLOT(socketDisconnectedSlot())); QEventLoop loop; connect(m_socket, SIGNAL(disconnected()), &loop, SLOT(quit())); QTimer::singleShot(0, this, SLOT(disconnectSlot())); loop.exec(); }
ClientConnection::~ClientConnection() { if (m_isConnected) { qDebug() << "Destroying object with opened connection"; disconnect(m_socket, SIGNAL(disconnected()), this, SLOT(socketDisconnectedSlot())); QEventLoop loop; connect(m_socket, SIGNAL(disconnected()), &loop, SLOT(quit())); QTimer::singleShot(0, this, SLOT(disconnectSlot())); loop.exec(); } if (m_socket != 0L) { QTimer::singleShot(0, m_socket, SLOT(deleteLater())); } }