int main(int argc, char **argv) { threadpool tp; tp = create_threadpool(2); fprintf(stdout, "**main** dispatch 3\n"); dispatch(tp, dispatch_to_me, (void *) 3); fprintf(stdout, "**main** dispatch 6\n"); dispatch(tp, dispatch_to_me, (void *) 6); fprintf(stdout, "**main** dispatch 7\n"); dispatch(tp, dispatch_to_me, (void *) 7); fprintf(stdout, "**main** done first\n"); sleep(20); fprintf(stdout, "\n\n"); fprintf(stdout, "**main** dispatch 3\n"); dispatch(tp, dispatch_to_me, (void *) 3); fprintf(stdout, "**main** dispatch 6\n"); dispatch(tp, dispatch_to_me, (void *) 6); fprintf(stdout, "**main** dispatch 7\n"); dispatch(tp, dispatch_to_me, (void *) 7); fprintf(stdout, "**main done second\n"); sleep(20); exit(-1); }
int main(int argc , char *argv[]) { int socket_desc , client_sock , c; struct sockaddr_in server , client; //Create socket socket_desc = create_socket(); //Prepare the sockaddr_in structure setup_server(&server); bind_server_to_socket(socket_desc, &server); //Listen listen(socket_desc , LISTEN_QUEUE_SIZE); //Accept and incoming connection c = sizeof(struct sockaddr_in); threadpool_t *pool = create_threadpool(); while((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) { if( threadpool_add(pool, connection_handler, (void*) &client_sock, 0)) { perror("could not create thread"); return 1; } //Now join the thread , so that we dont terminate before the threa } if (client_sock < 0) { perror("accept failed"); return 1; } return 0; }
int main(int argc, char **argv) { char buf[1000]; int socket_listen; int socket_talk; int dummy, len; threadpool pool; struct timeval then, now, diff; if (argc != 2) { fprintf(stderr, "(SERVER): Invoke as './server socknum'\n"); fprintf(stderr, "(SERVER): for example, './server 4434'\n"); exit(-1); } /* * Set up the 'listening socket'. This establishes a network * IP_address:port_number that other programs can connect with. */ pool = create_threadpool(POOL_SIZE,NULL); socket_listen = setup_listen(argv[1]); /* * Here's the main loop of our program. Inside the loop, the * one thread in the server performs the following steps: * * 1) Wait on the socket for a new connection to arrive. This * is done using the "accept" library call. The return value * of "accept" is a file descriptor for a new data socket associated * with the new connection. The 'listening socket' still exists, * so more connections can be made to it later. * * 2) Read a request off of the listening socket. Requests * are, by definition, REQUEST_SIZE bytes long. * * 3) Process the request. * * 4) Write a response back to the client. * * 5) Close the data socket associated with the connection */ gettimeofday(&then, NULL); while (1) { socket_talk = saccept(socket_listen); // step 1 if (socket_talk < 0) { fprintf(stderr, "An error occured in the server; a connection\n"); fprintf(stderr, "failed because of "); perror(""); exit(1); } dispatch(pool, request_client, (void *) socket_talk); count_dispatches++; if (count_dispatches == 10) { double seconds; //elapsed time in seconds between then and now gettimeofday(&now, NULL); timersub(&now, &then, &diff); seconds = 1000000*diff.tv_sec + diff.tv_usec; fprintf(stdout, "Loops: %6d Threads: %2d Dispatches/sec: %10.3f\n", NUM_LOOPS, POOL_SIZE, seconds/1000); } } }