/** *函数介绍:有请求连接,新建一个socket *输入参数:int socketId,新建socket的SocketDescriptor *返回值:无 */ void TcpServer::incomingConnection(int socketId) { TcpSocket *socket = new TcpSocket(socketId,this); //设置socket描述符 socket->setSocketDescriptor(socketId); //写入系统日志 //Global::systemLog->append(tr("网络通信服务端有新的连接请求"), QString(tr("服务端接收新的连接请求,socket描述符 = %1.")).arg(socketId), SystemLog::INFO); qDebug()<<"服务端有新的连接,socketDescriptor:"<<socket->socketDescriptor(); //新建了一个socket连接 addServerSocket(socketId); //获得客户端数据 connect(socket, SIGNAL(haveReadDataSignal(QByteArray,int)), this, SLOT(getClientMessage(QByteArray,int))); //信号槽:跟踪服务端Socket状态 connect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(getServerSocketState(QAbstractSocket::SocketState))); //信号槽:服务端socket出现错误 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverSocketError(QAbstractSocket::SocketError))); //与客户端断开某socket连接,服务端状态发生改变 connect(socket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(removeServerSocket(int))); //与客户端断开连接,输出服务器当前连接 //connect(socket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(getAllConnection())); //与客户端连接上后,输出服务器当前连接 //connect(socket, SIGNAL(connectedForServerSignal(int)), this, SLOT(getAllConnection())); //与客户端连接上后,输出服务器当前连接 //connect(socket, SIGNAL(connectedForServerSignal(int)), this, SLOT(addServerSocket(int))); }
void Worker::newSocket(qintptr socket) { //qDebug() << m_name << " is handling a new request; thread id" << thread()->currentThreadId(); TcpSocket* s = new TcpSocket(this); s->m_id = static_cast<unsigned int>(rand()); connect(s, SIGNAL(readyRead()), this, SLOT(readClient())); connect(s, SIGNAL(disconnected()), this, SLOT(discardClient())); s->setSocketDescriptor(socket); s->setTimeout(1000*60*2); #ifndef NO_LOG sLog() << m_name << " receive a new request from ip:" << s->peerAddress().toString(); #endif }
void TcpServer::incomingConnection(qintptr handle) { TcpSocket *sock; if (!m_socks.empty()) { sock = m_socks.back(); m_socks.pop_back(); sock->resetSocket(); } else { sock = new TcpSocket(m_wsgi, this); sock->engine = m_engine; static QString serverAddr = serverAddress().toString(); sock->serverAddress = serverAddr; connect(sock, &QIODevice::readyRead, m_proto, &Protocol::readyRead); connect(sock, &TcpSocket::finished, this, &TcpServer::enqueue); } sock->setSocketDescriptor(handle); sock->start = QDateTime::currentMSecsSinceEpoch(); }