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

  • SEARCH
  • Home
  • 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 6886959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:49:31+00:00 2026-05-27T05:49:31+00:00

I’m trying to write a simple HTTP web server using c, but I keep

  • 0

I’m trying to write a simple HTTP web server using c, but I keep running into problems when trying to create it. Everything about creating the server socket seems to work fine; basically I just don’t understand how to send stuff to the browser. Here is the excerpt of my code in which the server loops to create socket connections with the client and then send stuff to the webpage:

for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        clntLen = sizeof(clntAddr);

        /* Wait for a client to connect */
        if ((clntSock = accept(servSock, (struct sockaddr *) &clntAddr,
                               &clntLen)) < 0)
            DieWithError("accept() failed");

        /* clntSock is connected to a client! */

        printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr));

        FILE *clientInput = fdopen(clntSock, "r");
        char input[1000];
        char html[BUFSIZE];

        fgets(input, 1000, clientInput);

        char  *token_separators = "\t \r \n";
        char *method = strtok(input, token_separators);
        char *requestURI = strtok(NULL, token_separators);
        char *httpVersion = strtok(NULL, token_separators);
        ...
        char requestMessage[] = "HTTP/1.0 200 OK\r\n\r\n";
        send(clntSock, requestMessage, strlen(requestMessage) + 1, 0);
        ...  

}

I’m pretty sure that my code works for the socket creation and connections, it’s just when I try to send this response header my webpage just keeps trying to download the html. When I don’t send the response header and I jsut try to send normal html, my page works.

I guess what I don’t understand is why my response header will not work.

  • 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-05-27T05:49:31+00:00Added an answer on May 27, 2026 at 5:49 am

    It is highly unusual to see FILE * with sockets. It is more typical to use read/recv and write/send. This allows you to do error handling a bit more easily.

    That said, without a header the browser will think your server is running HTTP/0.9 — and everything is HTML. In order to get things working right, add another:

    HTTP/1.0 200 OK
    Content-Type: text/html;charset=iso-8859-1
    
    <html>
    ...
    

    Of course, this is still missing some pieces, but it’s a start and most browsers should accept it without complaints. You can look through the RFC and find the request methods you need to support (GET and HEAD) and the response headers you are required to produce (Server, Date). (I hope you’re doing this for learning purposes, and not for production.)

    You can test responses with netcat fairly easily:

    $ cat file
    HTTP/1.0 200 OK
    Content-Type: text/plain
    
    Hello, World
    $ nc -l 8000 < file
    

    Then browse to http://localhost:8000 . Note the above works with BSD netcat, if you have GNU netcat you will need to read the man page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am currently running into a problem where an element is coming back from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.