void boundedServerLoop(int socketFD) { int connectionFD; #ifdef DEBUG printf("Conexions: [%d/%d]\n",NUM_CONECTIONS, MAX_CLIENTS); #endif if(NUM_CONECTIONS >= MAX_CLIENTS) { #ifdef DEBUG printf("Num maxim de conexions, pause\n"); #endif while (waitpid(-1,NULL,0) > 0); } connectionFD = acceptNewConnections (socketFD); if (connectionFD < 0) { perror ("Error establishing connection \n"); deleteSocket(socketFD); exit (1); } doServiceFork(connectionFD); }
void unboundedServerLoop(int socketFD) { int connectionFD; connectionFD = acceptNewConnections (socketFD); if (connectionFD < 0) { perror ("Error establishing connection \n"); deleteSocket(socketFD); exit (1); } doServiceFork(connectionFD); }
void ClientListener::handleUnknownClient(const Event&, void* vclient) { ClientProxyUnknown* unknownClient = reinterpret_cast<ClientProxyUnknown*>(vclient); // we should have the client in our new client list assert(m_newClients.count(unknownClient) == 1); // get the real client proxy and install it ClientProxy* client = unknownClient->orphanClientProxy(); bool handshakeOk = true; if (client != NULL) { // handshake was successful m_waitingClients.push_back(client); m_events->addEvent(Event(m_events->forClientListener().connected(), this)); // watch for client to disconnect while it's in our queue m_events->adoptHandler(m_events->forClientProxy().disconnected(), client, new TMethodEventJob<ClientListener>(this, &ClientListener::handleClientDisconnected, client)); } else { handshakeOk = false; } // now finished with unknown client m_events->removeHandler(m_events->forClientProxyUnknown().success(), client); m_events->removeHandler(m_events->forClientProxyUnknown().failure(), client); m_newClients.erase(unknownClient); PacketStreamFilter* streamFileter = dynamic_cast<PacketStreamFilter*>(unknownClient->getStream()); IDataSocket* socket = NULL; if (streamFileter != NULL) { socket = dynamic_cast<IDataSocket*>(streamFileter->getStream()); } delete unknownClient; if (m_useSecureNetwork && !handshakeOk) { deleteSocket(socket); } }
void recycle_sockets() { SOCKET_DATA *dsock; LIST_ITERATOR *sock_i = newListIterator(socket_list); ITERATE_LIST(dsock, sock_i) { if (dsock->lookup_status != TSTATE_CLOSED) continue; /* remove the socket from the main list */ listRemove(socket_list, dsock); propertyTableRemove(sock_table, dsock->uid); /* close the socket */ close(dsock->control); /* stop compression */ compressEnd(dsock, dsock->compressing, TRUE); /* delete the socket from memory */ deleteSocket(dsock); } deleteListIterator(sock_i); }
main (int argc, char *argv[]) { int socketFD; int connectionFD; char buffer[80]; int ret; int port; if (argc != 2) { strcpy (buffer, "Usage: ServerSocket PortNumber\n"); write (2, buffer, strlen (buffer)); exit (1); } port = atoi(argv[1]); socketFD = createServerSocket(port); if (socketFD < 0) { perror ("Error creating socket\n"); exit (1); } while (1) { connectionFD = acceptNewConnections (socketFD); if (connectionFD < 0) { perror ("Error establishing connection \n"); deleteSocket(socketFD); exit (1); } doServiceFork(connectionFD); } }
main (int argc, char *argv[]) { int connectionFD; int ret; int r; char buff[80]; char buff2[80]; char *hostname; int port; int i,num_it; int msec_elapsed; struct timeval init_t, end_t; gettimeofday(&init_t, NULL); if (argc != 4) { sprintf (buff, "Usage: %s num_it hostname port\n", argv[0]); write (2, buff, strlen (buff)); exit (1); } num_it = atoi(argv[1]); hostname = argv[2]; port = atoi (argv[3]); connectionFD = clientConnection (hostname, port); if (connectionFD < 0) { perror ("Error establishing connection\n"); exit (1); } for (i=0; i<num_it; i++) { ret = write (connectionFD, "hola ",5); if (ret < 0) { perror ("Error writing to connection\n"); exit (1); } ret = read (connectionFD, buff, sizeof (buff)); if (ret < 0) { perror ("Error reading on connection\n"); exit (1); } buff[ret] = '\0'; sprintf (buff2, "Client [%d] received: %s\n",getpid(), buff); write(1,buff2,strlen(buff2)); } gettimeofday(&end_t, NULL); msec_elapsed = (end_t.tv_sec*1000)+(end_t.tv_usec/1000)- (init_t.tv_sec*1000)+(init_t.tv_usec/1000); sprintf (buff2, "\nClient [%d] finishes\n", getpid()); write(1,buff2,strlen(buff2)); sprintf(buff2, "Time %d\n",msec_elapsed); write(2,buff2,strlen(buff2)); deleteSocket (connectionFD); }