/*! * \brief Server::incomingConnection * \param socketDescriptor */ void Server::incomingConnection(qintptr socketDescriptor) { qDebug() << "Connecting to " << socketDescriptor; ConnectionHandler *handler = new ConnectionHandler(socketDescriptor, this); clientList.append(handler); connect(handler, SIGNAL(finished()), handler, SLOT(deleteLater())); handler->start(); }
void NetworkManager::incomingConnection(qintptr handle) { qDebug() << "Client connected"; QTcpSocket* socket = new QTcpSocket(); socket->setSocketDescriptor(handle); mConnections[handle] = socket; ConnectionHandler* connection = new ConnectionHandler(handle, socket); connect(connection, SIGNAL(networkReceivedData(int,unsigned char*,int)), this, SLOT(networkReceivedData(int,unsigned char*,int))); connect(connection, SIGNAL(clientDisconnected(int)), this, SLOT(clientDisconnect(int))); connection->start(); }
// Listen for incoming connections void Server::run() { // Timeout value struct timeval timeoutValue; timeoutValue.tv_sec = SOCKET_TIMEOUT; // Set for select() fd_set sockets; // Activity cout << "Listening for connections..." << endl; // Start listening for connections status = listen(listenSocket, 5); if (status == -1) cout << "listen error" << endl ; assert(status != -1); // Socket descriptor for incoming connection int newSocket; struct sockaddr_storage their_addr; socklen_t addr_size = sizeof(their_addr); while(!stopRequested) { // Clear the socket set and add the listen socket to the set FD_ZERO(&sockets); FD_SET(listenSocket, &sockets); select(listenSocket + 1, &sockets, NULL, NULL, &timeoutValue); if(FD_ISSET(listenSocket, &sockets)) { // Accept incoming connection and set timeout value (RCVTIMEO) newSocket = accept(listenSocket, (struct sockaddr *)&their_addr, &addr_size); status = setsockopt(newSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeoutValue, sizeof(struct timeval)); if (newSocket == -1) cout << "Accept returned " << errno << endl; else { ConnectionHandler* newConnection = new ConnectionHandler(newSocket); newConnection->start(); } } } // Close the socket descriptor and free the memory used by the host info list freeaddrinfo(host_info_list); close(listenSocket); }