Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:17:44+00:00 2026-06-12T00:17:44+00:00

I’m trying to write a basic HTTP file server using sockets in C and

  • 0

I’m trying to write a basic HTTP file server using sockets in C and have two main problems.

First, when I try to access “ip:port/” through a browser, I get the index.html file but it’s in complete gibberish. However, when I access “ip:port/not_in_directory” my little custom 404 message comes back just fine. If I set the default file to a existing pdf file, gibberish comes back as well (length of pdf gibberish).

Secondly, when I try to access “ip:port/file_that_exists”, I get my 404 instead of the correct file. While debugging, I do see that scratch_pad has the correct file name, with no excess or missing characters, but !access(scratch_pad, R_OK) still comes out to be 0. Is there some necessary cast even though access() takes a char* for the file name?

EDIT: Second issue has been solved (I was using a pointer to deallocated buffer as file name). However, everything that’s picked up by the browser is gibberish. I’m removing more non-essential code.

#define FILE_NOT_FOUND "HTTP/1.0 404 FILE NOT FOUND\r\n"
#define FILE_FOUND "HTTP/1.0 200 OK\r\n"
#define SERVER_NAME "Server: Test Server\r\n"
#define CONTENT_TYPE "Content-Type: text/html\r\n"
#define DATE "Date: "
#define MESSAGE_BREAK "\r\n"
#define NOT_FOUND_HTML "<!DOCTYPE html>\n<html>\n<title>404 Not Found</title>\n<body>\n<p>Nope</p>\n</body>\n</html>\r\n"

  if(!access(pulled_name, R_OK | W_OK)) {
      printf("file found on server\n");
      server_file = fopen(pulled_name, "rb+");
      fseek(server_file, 0L, SEEK_END);
      file_size = ftell(server_file);
      fseek(server_file, 0l, SEEK_SET);
      printf("opened successfully and set file_size to %i\n", file_size);
      getting_time = time(NULL);
      c_time = localtime(&getting_time);
      current_time = asctime(c_time);
      printf("set time for message\n");

  message = malloc((int) strlen(FILE_FOUND)
                + (int) strlen(SERVER_NAME)
                + (int) strlen(CONTENT_TYPE)
                + (int) strlen(DATE)
                + (int) strlen(current_time)
                + (int) strlen(MESSAGE_BREAK));

  printf("malloc'ed message\n");

  message_size = (int)(strlen(message)*sizeof(char));     
  memset(message, 0, message_size);
  strcpy(message, FILE_FOUND);
  strcat(message, SERVER_NAME);
  strcat(message, CONTENT_TYPE);
  strcat(message, DATE);
  strcat(message, current_time);
  strcat(message, MESSAGE_BREAK);
  printf("built message\n");

  printf("set message_size to %i\n", message_size);
  while(total_sent < message_size) {
    n = send(new_socket_fd, message, message_size, 0);
    if (n==-1) {
      printf("error sending message\n");
      break;
    }
    total_sent += n;
    printf("sent %i\n", total_sent);
  }
  free(message);

  total_sent = 0;
  while(total_sent < file_size) {
    n = send(new_socket_fd, server_file, file_size, 0);
    if (n==-1) {
      printf("error sending file\n");
      break;
    }
    total_sent += n;
    printf("sent %i\n", total_sent);
  }
  fclose(server_file);
  close(new_socket_fd);
  printf("closed client connection!\n");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:17:45+00:00Added an answer on June 12, 2026 at 12:17 am

    There several instances of this mistake:

    memset(buffer, '\0', sizeof(buffer));
    

    when the type of the first argument is a char*, which means the buffer will not be filled with null characters but only the first sizeof(char*) bytes (typically 4 or 8). In this case, it means that buffer may not be null terminated (as recv() does not append null characters). Both printf() and strtok() require string arguments to be null terminated. It is common to see garbage characters printed after a string with a call to printf("%s", p); with a non-null terminated string.

    Fix all memset() calls by explicitly specifying the size of the buffer pointed to by the first argument:

    const size_t BUFFER_SIZE      = 512;
    const size_t SCRATCH_PAD_SIZE = 256;
    buffer                        = malloc(BUFFER_SIZE);     /*Check return value*/
    scratch_pad                   = malloc(SCRATCH_PAD_SIZE);/*to ensure success.*/
    memset(buffer,      0, BUFFER_SIZE);
    memset(scratch_pad, 0, SCRATCH_PAD_SIZE);
    

    The sizeof(char) is guaranteed to be 1 so it can be omitted from the malloc() argument.


    Other misuse of memset():

    memset(message, '0', sizeof(message)); 
    
    • '0' is a the character zero, not null
    • sizeof(message) is the sizeof(char*)

    change to:

    memset(message, 0, /* the actual size of message*/ );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.