static void *thread_func(void*) {
    while(getStopIssued() == 0) {
        write_buffer_to_log();
    }
    if (logFile != NULL) {
        fprintf(logFile, "Logger thread is terminating...\n");
    }
    return 0;
}
Exemplo n.º 2
0
void* StartServer(void* vp)
{
	int client_sock , c , *new_sock;
	struct sockaddr_in server , client;

	//Create socket
	socket_desc = socket(AF_INET , SOCK_STREAM , 0);
	if (socket_desc == -1)
	{
		printf("Could not create socket");
	}
	puts("Socket created");

	//Prepare the sockaddr_in structure
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;
	server.sin_port = htons(80);

	//Bind
	if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
	{
		//print the error message
		perror("bind failed. Error");

		return 0;
	}

	puts("bind done");

	//Listen
	listen(socket_desc , 3);

	//Accept and incoming connection

	c = sizeof(struct sockaddr_in);
	client_sock=1;

	int abortState = getStopIssued();
	loadAPIs();
	initializeCache();

	while( client_sock>0 && abortState!=1)
	{
		abortState = getStopIssued();
		puts("Waiting for incoming connections...");
		client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);

		puts("Connection accepted");

		pthread_t sniffer_thread;
		/* pthread_attr_t threadAttribute;
        int res = pthread_attr_init(&threadAttribute);
          if (res != 0) {
              perror("Attribute init failed");
              exit(EXIT_FAILURE);
          }
          res = pthread_attr_setdetachstate(&threadAttribute, PTHREAD_CREATE_DETACHED); // The worker threads are detached.
          if (res != 0) {
              perror("Setting detached state failed");
              exit(EXIT_FAILURE);
          }*/

		new_sock = malloc(sizeof(new_sock));
		*new_sock = client_sock;

		if(pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
		{
			perror("could not create thread");
		}
		puts("Handler assigned");
	}

	free(new_sock);

	if (client_sock < 0)
	{
		perror("accept failed");
	}

	if(abortState ==1)
	{
		puts("Abort signal received.");
	}

	freeAPIs();
	cleanCache();
	puts("Server Stopped");
	return 0;
}