multi-threaded web server problem!!

This is a discussion on multi-threaded web server problem!! within the Windows Programming forums, part of the Platform Specific Boards category; can anyone please help me for my problem. My codes is the base single threaded web server. what about change ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Question multi-threaded web server problem!!

    can anyone please help me for my problem. My codes is the base single threaded web server. what about change to be a multi-threaded, which another web-browser should have to wait for the first one finish? I think will use 'CreateThread',but where should I put the 'CreateThread' function, I had tried put 'send' inside 'CreateThread', like CreateThread(NULL,0,send,fd,sbuffer,strlen(sbuffer ),0,0,&id), but it doesn't work. It seem like the 'send' can't have more than one parameter. Please have a look. Thank you very much.
    Code:
    #include <stdio.h>
    #include <winsock.h>
    #include <windows.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 *sbuffer;
       char filename[256];
       long filesize;
       
       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
                                              //
                                              // send the file
                if (f==NULL) {
    	            printf("File not exit.\r\n");
    	            }
    	         else
    	          {
                 // obtain file size.
                 fseek (f,0,SEEK_END);
                 filesize = ftell (f);
                 rewind (f);
                 // allocate memory to contain the whole file.
                 sbuffer = (char*) malloc (filesize);
                 
                 if (sbuffer == NULL) {
    	             printf("memory error.\r\n");
    	             }
                  
                 else{
                    printf("Sending %s, size: %ld.\r\n",filename,filesize);
                    while (!feof(f)){
    							 fread (sbuffer,1,filesize,f);
    							 send(fd,sbuffer,strlen(sbuffer),0);					
    							 }//end of while
                    printf("Completed.\r\n");
                     }//end of else
                 } //end of else
          closesocket(fd); // close the socket
       }//end of while
    //fclose(f);
    }
    Last edited by Salem; 08-11-2006 at 10:13 AM. Reason: Now with added [code][/code] tags for extra flavour - use them in future

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    There's a whole bunch of issues in there which are dealt with by various FAQ entries.

    For a start, there are a number of functions whose status result is completely ignored. Perhaps checking some of those would help.

    And fread() does NOT append a \0, so your following strlen() is "up the creek without a paddle"
    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
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You should make a separate function for the thread, and if it requires more than one parameter, you should package the parameters into a structure (dynamically allocated), so that you can pass the address of that structure to CreateThread, which only accepts one input to the thread start function.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 10:45 AM
  2. Server recv data problem! Please help!
    By hyaku_ in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-28-2005, 01:35 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