Multi Threaded Web server question

This is a discussion on Multi Threaded Web server question within the C Programming forums, part of the General Programming Boards category; Hi there I'm currently working on a Multi Threaded webserver. So far I have manged to set up the webserver ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    Multi Threaded Web server question

    Hi there I'm currently working on a Multi Threaded webserver.

    So far I have manged to set up the webserver on an infinate loop waiting for a request at the socket.
    This is where it gets abit weird, Yesterday I managed to find an example of reading a html code into a buffer then sending that buffer to the a browser and it would display fine.

    However I have then tried to Implement the multi thread with the CreateThread function, unfortunalty I was unable to fully understand.. but when I tried to go back to just my single threaded webserver its not working now... I have no idea why after hours of fiddling around with stuff have now decided to look for some help.

    Here is my code... any help would be greatly appreciated. I have commented fairly well so that should help you understand what I'm working towards.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock.h>
    
    int main() {
       int s,n;
       unsigned fd;
       struct sockaddr sin={AF_INET,{31,144,0,0,0,0,0,0,0,0,0,0,0,0}};
       WSADATA wsaData;
       char *header="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
       char data[512];
       char filename[256];
       FILE *f;
       
       WSAStartup(MAKEWORD(1, 1), &wsaData);  // initialise windows sockets
       s=socket(PF_INET,SOCK_STREAM, 0);      // create a socket
       bind(s,&sin, sizeof(sin));             // bind it to port 80
       listen(s,10);                          // allow up to 10 incoming connections 
       while(1) {
          fd=accept(s,NULL,NULL);             // wait for a request 
          n=recv(fd,data,512,0);              // recieve the request using fd
          data[n]=0;                          // NUL terminate it
          sscanf(data,"GET /%s ",filename);   // get the name of the file
          f=fopen(filename,"rb");             // open the file (might be binary)
          send(fd,header,strlen(header),0);   // send the header
                                              //
        	//file size
    		long lSize;
    		fseek (f,0,SEEK_END);
    		lSize = ftell(f);
    		rewind(f);
    		
    		// allocate memory
    		char *buffer;
    		buffer = (char*)malloc(sizeof(char)*lSize);
    		if (buffer == NULL){
    			fputs ("Read error",stderr);
    			exit(1);
    		}
    		
    		//copy file to buffer
    		size_t result;
    		result = fread(buffer,1,lSize,f);
    		if (result != lSize){
    			fputs("Read error",stderr);
    			exit(2);
    		}
    
    		//send buffer content to browser
    		send(fd,buffer,strlen(buffer),0);	
                                              //
          closesocket(fd);                    // close the socket
    		fclose(f); 									//close file
    		free (buffer); 							//empty buffer
    		return 0;                                      // send the file
                                              //
          //closesocket(fd);                    // close the socket
       }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > n=recv(fd,data,512,0); // recieve the request using fd
    > data[n]=0; // NUL terminate it
    If n is an error, or n is a full buffer, where does the \0 go?

    > send(fd,buffer,strlen(buffer),0);
    fread() does NOT append a \0, nor do you have room for one.

    You already have a length anyway, so use it.

    Also, pay attention to the return result. You're assuming (falsely) that send() will always send the WHOLE buffer, no matter how large it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    Im not wanting to append a \0, from what I understand if I append a \0 it will break my while loop which is listening for incoming requests.

    Thank you for your help, I'm still learning alot I would say C is my weakest language so pardon me if I have misunderstood you.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Nope, it will just crash and burn instead.

    It's nothing to do with whether the while loop will continue or not, it's all about accessing memory you don't own.
    Until those issues are fixed, the rest of the problem is irrelevant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    Thank you for your time and help, I understand the problems you have pointed out however because my grasp of these C functions is very limited I am unsure on the solutions for them.

    Any nudge in the right direction would be greatly appreciated, I have been reading alot of information to try and solve this but have been unlucky to date.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Until you can make reliable single-threaded programs, there's no point trying to make it multi-threaded.

    Debugging a MT program is a hell of a lot harder.

    I've suggested several things, try to incorporate them into your program and post the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  2. FTP server question
    By Draco in forum Tech Board
    Replies: 4
    Last Post: 06-01-2006, 10:13 PM
  3. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  4. Web Hosting SQL Server
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 06-14-2002, 07:00 PM
  5. Replies: 1
    Last Post: 06-07-2002, 11:22 AM

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