Web server need help

This is a discussion on Web server need help within the Networking/Device Communication forums, part of the General Programming Boards category; Hi i have a problem with my code, if anyone here can help. ok i create a thread that executes ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    Web server need help

    Hi i have a problem with my code, if anyone here can help.
    ok i create a thread that executes a function that receives a browser request and put it in a buffer. I create another thread that read the request from the buffer and sends the correct answer.
    The problem is that he never executes the function of read_answer, he passes that function and the prog terminates.

    Code:
    (void *) receive() { 
    Here i create the socket, make him accept a connection and put the request in a buffer. } 
    
    (void *) read_answer() { 
    Here i read the request and then send the answer. 
    }
    
     main() { 
    pthread_t thread,thread1 
    pthread_create(&thread, NULL, (void *)&receive,NULL); 
    
    while(1){ 
    pthread_create(&thread1, NULL,(void *)&read_answer,NULL);
     }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Can you post your real code? (I assume you're not trying to compile that.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <netinet/in.h>
    #include <time.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <pthread.h>
    
    #define PORT 80
    #define MAX_DATA 1000000
    #define MAX 250
    #define N_REQUESTS 5
    
    char *buffer[N_REQUESTS];
    int sockfd, new_sockfd;
    	struct sockaddr_in servaddr; 
    
    int nbytes_readen = 0;
    char file[MAX];
    int dim = 0;
    
    char directoryMAX];
    
    void *receive() {
    	int posistion = 0;
    	int i = 0;
    	char request_aux[MAX];
    
    	sockfd = socket(AF_INET,SOCK_STREAM,0);
    		if (sockfd<0) {
    			perror("Socket error\n");
    			exit(-1);
    		}
    		
    		bzero(&servaddr, sizeof(servaddr));	
    		servaddr.sin_family = AF_INET;
    		servaddr.sin_addr.s_addr = htonl(INADDR_ANY);	
    		servaddr.sin_port = htons(PORT);
    	
    		if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
    	  		  exit(-1);
    		}
    
    	
    		listen(sockfd, 10);	
    	
    	while (1) {
    		new_sockfd = accept(sockfd, (struct sockaddr *) NULL, NULL);	
    		
    		nbytes_readen = read(new_sockfd, request_aux, sizeof(request_aux));
    		char request[nbytes_readen];
    		while(request_aux[i]!='\n'){ 
    			request[i]=request_aux[i];
    			i++;
    		}
    		request[i+1]='\0';
    	//	sem_wait(put_elements);
    		buffer[position] = request;
    		position = (peosition+1) % N_REQUESTS; 
    	//	sem_sign(can_take);
    	}
    }
    
    void *Read_answer() {
    	time_t hours; 
    	char answer[MAX]; 
    
    	int position = 0;
    	char protocol[9];	
    	char type[6];	
    	char extension[6];  
    	
    	char *point= NULL;
    	
    	char* file_content[MAX_DATA]; 
    	long int bytes_read;
    	
    	char directoria[MAX];
    
    	dim = nbytes_readen- 13;
    	char file[dim];
    	
    	while(1) {
    //	sem_wait(can_take);
    	char *request= buffer[positions];
    	positions = (positions + 1) % N_REQUESTS; 
    //	sem_sign(put_elements);
    	sscanf(request,"GET %s %s",file,protocol);
    		 	
    	point = strchr(ficheiro,'.');
    			
    	if (ponto == NULL)
    		strcpy(extensao,"txt");
    	else {
    		switch(*(ponto+1)) {	
    			case 't' : strcpy(extension,"txt"); break;
    			case 'j' : strcpy(extension,"jpg"); break;
    			default  : exit(0);
    			} 
    		}
    
    		if (strcmp(extension, "txt")==0)
    			strcpy(tipo,"text");
    		else
    			strcpy(tipo,"image");
    			 
    		hours= time(NULL);
    			
    		if (file[0] == '/')
    			sprintf(directoria,"%s%s",directory,file);	
    		else 
    			sprintf(directoria, "%s/%s",directory, file);
    	
    	int fd = open(directoria, O_RDWR, S_IRWXU);
    	if (fd<0) {
    		int bytes_write;	
    		bytes_write = sprintf(answer,"\n\n%s 404 Not found\nDate: %s\nContent-Type: %s/%s\n\n",protocol,ctime(&hours),type,extension);
    		answer[strlen(answer)-1]='\0';
    		write(new_sockfd, answer, bytes_write);
    		close(new_sockfd);
    	}
    	else {
    		int bytes_written = 0;
    		bytes_read = read(fd, file_content, sizeof(file_content));			bytes_written = sprintf(answer,"\n\n%s 200 OK\nDate: %sContent-Lenght: %ld\nContent-type: %s/%s\n\n",protocol,ctime(&hours),bytes_read,type,extension);
    		close(fd);
    		write(new_sockfd, answer, bytes_written);
    		write(new_sockfd, file_content, bytes_read);	
    		close(new_sockfd);
    	}
    }}
    
    
    int main(int argc, char **argv) {
    	
    	
    	if (argc != 2) {  
            printf("Invalid arguments\n"); 
    		exit(0);
    	}
    	else {
    		strcpy(directory, argv[1]);
    		
    		pthread_t thread,thread1;
    		
    		pthread_create(&thread, NULL, (void *)&receive,NULL);
    	
    		while(1){
    		pthread_create(&thread1, NULL,(void *)&read_answer,NULL);
    		}	
    	}
    	for(;;);
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    		while(1){
    		pthread_create(&thread1, NULL,(void *)&read_answer,NULL);
    		}
    You program is not "terminating", it is crashing. The code I quoted above will overflow your stack in a matter of seconds (or less).

    Why are you starting a new thread to call your read_answer function? That just doesn't make any sense.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Here is probably what you meant to do:
    Code:
    int main(void)
    {
       /* bind server socket here */
       server_socket = bind(...);
       for(;;)
       {
          new_socket = accept(...server_socket...);
    
          /* Start a new thread to read the browser request, and send a response.
           * pass this new thread the client socket (new_socket).  */
          pthread_create(...);
       }
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    You program is not "terminating", it is crashing. The code I quoted above will overflow your stack in a matter of seconds (or less).

    Why are you starting a new thread to call your read_answer function? That just doesn't make any sense.
    The problem is that i have to do that way a thread to receive and a thread to read and answer(this is an assignement).

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I don't think you understand the assignment correctly. As it stands, your receive thread will be created, and immediately after, your Read_answer thread will be created. Even if you didn't overflow the stack with that loop, you would still have the receive thread and Read_answer thread running at the same time! Before new_sockfd gets created in receive, you will be trying to write to it in Read_answer. Obviously this won't work at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can i force connecting to local web server via internet network ?
    By umen242 in forum Networking/Device Communication
    Replies: 5
    Last Post: 04-29-2008, 09:21 AM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 10:21 PM
  3. SWEBS Web Server
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-22-2003, 02:46 AM
  4. Web Server in Router Network...
    By Aidman in forum Tech Board
    Replies: 15
    Last Post: 01-17-2003, 09:24 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21