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 8185999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:02:59+00:00 2026-06-07T02:02:59+00:00

i am creating a simple server that does nothing but to give an existing

  • 0

i am creating a simple server that does nothing but to give an existing file /home/john/www at port 2222. my www folder has three files: index.html, server.c, client.c.

running the server, my firefox can now request for a page thru:

localhost:2222/server.c

the problem is, instead of showing the contents of server.c on my firefox, the file is downloaded instead. (firefox prompts to view or save the file.) same as with index.html and client.c.

i was able to show the contents of server.c days ago on my browser. but i can no longer remember what changes i made to make the browser behave like that.

here’s the code snippet where i am doing my modification:

#define LINE_LEN 128
#define MAX_LEN 1024
...
int process_request(int fd, char *www_path) {
    char cmessage[MAX_LEN];
    char *r;
    int n, read_size;
    FILE *file;
    char line[LINE_LEN];
    struct stat sb;
    if((n = read(fd, cmessage, MAX_LEN)) <= 0) {
        return 6;
    }
    cmessage[n] = '\0';
    // assume GET method
    r = cmessage + 4;
    n = 0;
    while(r[n] != ' ') {
        n++;
    }
    int len = strlen(www_path);
    char req_file[len+n+1];
    char temp[n+1];
    strncpy(temp, r, n);
    temp[n] = '\0';
    sprintf(req_file, "%s%s", www_path, temp);
    if(stat(req_file, &sb) == -1) {
        perror("stat");
        return 7;
    }
    char size[10];
    sprintf(size, "%d", sb.st_size);
    file = fopen(req_file, "rb");
    write(fd, "HTTP/1.1 200 OK\r\nContent-length: ", 33);
    write(fd, size, strlen(size));
    write(fd, "\r\n\r\n", 4);
    while((read_size = fread(line, sizeof(char), LINE_LEN, file)) != 0) {
        write(fd, line, read_size);
    }
    fclose(file);
    return 0;
}

or is this just a configuration that can be set in my web browser’s setting?

more details:

The socket was created using

socket(AF_INET, SOCK_STREAM, 0);

listen(), bind(), and accept() are successful. select() statement is used to wait for file descriptors that are ready for reading. The file descriptors of clients connected are not closed until the read() to a certain file descriptor returns -1.

Thank you for taking the time to read this. 🙂
Hoping for a helpful answer to why this is happening.

  • 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-07T02:03:00+00:00Added an answer on June 7, 2026 at 2:03 am

    You need to give the browser a proper content type as part of your server’s response. This might be something you either hardcode, or perhaps derive from the file name. For instance:

    Content-Type: text/plain
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experiencing some performance problems when creating a very simple Python HTTP server.
I'm creating a simple file walker to list some files and need to omit
I'm creating a simple HTTP proxy that will sit between mobile apps and a
i am creating a simple SOAP web service. i am to ensure that it
I have a server application (singleton, simple .NET console application) that talks to a
I have a simple Express server that is serving up some static files. Here
I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application and I'm using the MSDN code to
I'm creating simple example on JSF. It's small Login application. My problem is duplicate
I am creating simple app on Silverlight 4. In folder of View I have
i am creating simple call filter application which restrict unwanted calls. i use following

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.