示例#1
0
文件: main.c 项目: buzali/pop3srv
int main(int args, char **argv){
    
	 if(args != 4){
		printf("USAGE: pop3 [port] [maildir path] [log file]\n");
		printf("NOTE: the maildir path must end with an '/'\n");
		exit(1);
	}
	
	int sockfd, newSockfd; 	
	
	//Process command line arguments
	int c;
	for (c = 1; c < args; c++){
		if (c == 1){
			sockfd = socket_setup(atoi(argv[c])); 
		}
		else if (c == 2){
			dir_set_path(argv[c]); 
		}
		else if (c == 3){
			log_setUp (argv[c]); 
		}
	}
    
	//Creation of thread 
    pthread_t thread_id[100];
    int c_tnum = 0;
 
	//Listen for new connections and create a new thread for every new 
	//connection 
    log_write("Main" ,"POP3 Server Started" , "", "");
    while(1){
        newSockfd = socket_get_new_connection(sockfd);
        int rc;
        rc = pthread_create(&thread_id[c_tnum], NULL, 
                            (void *)pop_protocol, (void *)newSockfd);
        
        log_write("Main" ,"New Connection" , "", "");
        c_tnum++;
    }
		
}
示例#2
0
int main(int args, char **argv){
	
	int portno, sockfd, newsockfd;
	char *maildir;
	char *logfile;

	if(args != 4){
		printf("USAGE: pop3 [port] [maildir path] [log file]\n");
		printf("NOTE: the maildir path must end with an '/'\n");
		exit(1);
	} 
	
	portno = atoi(argv[1]);
	maildir = argv[2];
	logfile = argv[3];	
	
	//initial setup
	log_setUp(logfile);
	dir_set_path(maildir);

	//we need to initialise our struct of threads
	
	int i;
	for(i=0; i<MAX_THREADS; i++)
	{
		thread_list[i].thread = malloc(sizeof(pthread_t));
		thread_list[i].used = 0;
		hand_list[i].threadid = i;
		hand_list[i].newsocket = 0;
	}

	//setup our listener socket
	sockfd = socket_setup(portno);
	log_write("Main", "POP3 Server Started", "", "");
	
	while(1)
	{
	//	fprintf(stderr, "new thread\n");
		newsockfd = socket_get_new_connection(sockfd);
		//pthread_t *t;
		//t = malloc(sizeof(pthread_t));

		//we must find a free process.
		//this is a very bad linear search for now
		i = 0;
		while(i<MAX_THREADS)
		{
			if(thread_list[i].used == 0)
			{
				fprintf(stderr, "Handing off thread %i\n", i);
				thread_list[i].used = 1;
				//this is a struct that we handover which contains required info
				hand_list[i].newsocket = newsockfd;
				hand_list[i].threadid = i;		
				pthread_create(thread_list[i].thread, NULL, work_function,
				&hand_list[i]);
				break;
			}
			
			i++;
		}
		//no free connection, close connection

		if(i>=MAX_THREADS)
		{
			socket_write(newsockfd, "\nServer is overcapacity.\nPlease try again later\n\n");
			fprintf(stderr, "connection rejected, tubes clogged\n");
			close_connection(newsockfd);
		}	

	}
	//closes listener socket
	close_socket(sockfd);
	fprintf(stderr,"DONE!\n");
	
	return 0;
}