Thread: Web Server Issues

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    Web Server Issues

    I am having an issue sending information to my browser on a valid request.
    This is the header file we create on a valid file, printed out to the console.
    Message:
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Content-Length: 139
    Connection: Close

    0�5
    My socket is set up to listen, and here's my valid request code:
    Code:
    int return_result(int fd, char *content_type, char *buf, int numbytes) {
      FILE *fp;
      fp=fdopen(fd, "w+");
      if (fp==NULL) {
        perror("Couldn't open file");
        return -1;
      }
      char* message;
      message=malloc(sizeof(char)*MSGSIZE+sizeof(buf));
      int count=sprintf(message, "HTTP/1.1 200 OK\n\rContent-Type: %s\n\rContent-Length: %d\n\rConnection: Close\n\n%s\n\r",content_type, numbytes, &buf);
       printf("Message:\n%s\n",message);
       write(fd, message, strlen(message));
       fflush(fp); //flush
       close(fp);
       return 0;
    I don't know what I'm doing wrong.

    Thanks for any help.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The malloc()'d memory maybe insufficient, as sizeof(buf) returns size of the pointer instead of the array.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Pretty sure it's \r\n and not \n\r. And you need TWO of them between the header and the body.

    Ie. HEADERS /r/n/r/n BODY
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    You guys were both right, thank you.
    Last edited by TIMBERings; 05-03-2010 at 02:03 PM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    Is there a different way to load .gif and .jpeg as opposed to .txt or .html? If not, we can't load the images.

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, 11: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, 10:24 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM