void SensingServer::newClient() { QTcpSocket *client = nextPendingConnection(); if (client == 0) return; addNewClient(client); }
/// \fn NetConnection::findOrCreate(const NetAddress& client) /// \brief Finds or creates an address based on the given client info /// \return the clientid of the client or INVALID_CLIENTID when not found (e.g. when not accepting) NetAddress& NetConnection::findOrCreate(const NetAddress& client) { NetAddress* pclient = mClients.find(client); if ( pclient == NULL ) { if ( IS_SET(mFlags, eAccept) ) { pclient = &addNewClient(client); } } ASSERT_PTR(pclient); return *pclient; }
/// \fn NetConnection::connect(const std::string& server, int port) /// \brief Set up this connection for communication with a server. /// \returns clientid of remote connection int NetConnection::connect(const String& serverName, int port) { int clientid = -1; NetAddress address; if ( mSocket.resolve(address, serverName) && mSocket.connect(address, port) ) { NetAddress& client = addNewClient(address); SET_FLAG(mFlags, eConnected); clientid = client.index; } return clientid; }
/* Check the message to see if it's an update to our list of clients */ static void checkForClientUpdates(xPL_MessagePtr theMessage) { int clientPort = -1; int clientHeartbeatInterval = -1; Bool clientSigningOff = FALSE; hubClientPtr theClient = NULL; /* See if this is a heaertbeat message */ if (!isHeartbeatMessage(theMessage)) return; /* See if this is a local message */ if (!isLocalClient(theMessage)) return; /* See if this is a signoff message or not */ clientSigningOff = isSignoffMessage(theMessage); /* Get the port #, if any */ if ((clientPort = getClientPort(theMessage)) == -1) return; /* Get the interval for updates */ clientHeartbeatInterval = getHeartbeatInterval(theMessage); /* Lookup client */ if ((theClient = findClientByPort(clientPort)) == NULL) { /* If this is a signoff, we are done with it (signoff of */ /* an unknown client is of no interest to us */ if (clientSigningOff) return; /* New client -- add to the list */ addNewClient(theMessage, clientPort, clientHeartbeatInterval); return; } /* Check for client ident change */ updateClientIdent(theMessage, theClient); /* See if heartbeat interval has changed */ if (clientHeartbeatInterval != theClient->heartbeatInterval) { xPL_Debug("Changing heartbeat interval for client on port %s from %s minutes to %d", theClient->clientPort, theClient->heartbeatInterval, clientHeartbeatInterval); theClient->heartbeatInterval = clientHeartbeatInterval; } /* Update last heart from time */ theClient->lastHeardFromAt = time(NULL); }
void FileTransferServer::startListening() { m_server = new QTcpServer(); connect(m_server, SIGNAL(newConnection()), this, SLOT(addNewClient())); for (quint16 i = MIN_PORT; i<MAX_PORT; ++i) { if (m_server->listen(QHostAddress::Any, i)) { qDebug() << i; m_state = LISTENING; m_port = i; m_file = new QFile(m_fileName); emit listening(); return; } else qDebug() << "Cannot bind port " << i; } exit(1); }
int main() { char sendLine[MAX_MESSAGE_LENGTH]; bzero(&serverAddress, sizeof(serverAddress)); serverAddress.sin_family = AF_INET; serverAddress.sin_port = htons(SERVER_PORT); serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { perror(NULL); exit(1); } else { printf("Sock fd:%d\n", sockfd); } if (bind(sockfd, (struct sockaddr*) &serverAddress, sizeof(serverAddress)) < 0) { perror(NULL); close(sockfd); exit(1); } while (1) { clilen = sizeof(clientAddress); if ((n = recvfrom(sockfd, sendLine, MAX_MESSAGE_LENGTH - 1, 0, (struct sockaddr*)&clientAddress, &clilen)) < 0) { perror(NULL); close(sockfd); exit(1); } if (firstMessageFrom(clientAddress)) { addNewClient(clientAddress, sendLine); } else { int clientId = getClientId(clientAddress); sendToChat(sendLine, clientId); } printf("receive %s from %s\n", sendLine, clients[getClientId(clientAddress)].clientNick); } return 0; }