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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:29:05+00:00 2026-06-13T11:29:05+00:00

I am trying to write a simple Webserver in C. As of now I

  • 0

I am trying to write a simple Webserver in C. As of now I can receive connections and receive messages in full. However, in accordance with the HTTP/1.0 protocol, I want to be able to send information back to the client when the “\r\n\r\n” sequence is encountered. However, when using Telnet to test my server, when I enter “\r\n\r\n”, the server does nothing until I hit “^]” on the client. I tested this against Apache and Apache does not have this problem. So I was hoping to some information on how to mimic the Apache behavior. My code is added below but please keep in mind I am no where near done nor have I implemented a lot of error checking.
Thanks!

main(){
        int sock_fd = 0;
        int client_fd = 0;
        struct sockaddr_in socket_struct;
        /*Creates the socket*/
        if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
                fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
                exit(EXIT_FAILURE);
        }/*Ends the socket creation*/

        /*Populates the socket address structure*/
        socket_struct.sin_family = AF_INET;
        socket_struct.sin_addr.s_addr=INADDR_ANY;
        socket_struct.sin_port =htons(port);

        if (bind(sock_fd, (struct sockaddr*) &socket_struct, sizeof(socket_struct)) < 0)
        {
                fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
                exit(EXIT_FAILURE);
        }//Ends the binding.

        if (listen(sock_fd, 5) <0)
        {
                fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
                exit(EXIT_FAILURE);
        }//Ends the listening function

        if ( (client_fd = accept(sock_fd, NULL, NULL)) <0)
        {
                fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
                exit(EXIT_FAILURE);
        }//Ends the accepting.
        while ( (size = read(client_fd, msg, 1024)) > 0)
        {
                //size = recv(client_fd, msg, 1024, MSG_PEEK|MSG_WAITALL);
                if ( (msg[size-4] == 13) && (msg[size-3] == 10)&&(msg[size-2] == 13) && (msg[size-1] == 10) )
                {
                        char* buffer = (char *)malloc(sizeof("The msg was: ")+ sizeof(msg));
                        sprintf(buffer, "The msg was: %s", msg);
                        send(client_fd, buffer, sizeof("The msg was: ")+ sizeof(msg), MSG_OOB);
                }

        }//ends the while loop for receiving data
        close(client_fd);
}
  • 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-13T11:29:06+00:00Added an answer on June 13, 2026 at 11:29 am

    First of all, this line won’t work:

    while ( (size = read(client_fd, msg, 1024)) > 0)
    

    This will receive chunks of messages, and every chunk you receive will overwrite the last chunk. Do something like this:

    char msg[1024];
    size_t pos = 0;
    while (1) {
        assert(pos < sizeof(msg));
        ssize_t amt = read(client_fd, msg + pos, sizeof(msg) - pos);
        if (amt < 0)
            err(1, "read failed");
        if (amt == 0)
            break; // EOF
        pos += amt;
        char *e = strstr(msg, "\r\n\r\n");
        if (e) {
            size_t msglen = e - msg;
            /* Handle a complete message here */
        }
    }
    

    This way, as you receive chunks of the message, they get written into your buffer. Once you have the "\r\n\r\n" sequence, then you can deal with the whole message, even though you might get it in chunks.

    Key lesson: In TCP, packet boundaries and message boundaries might be completely different, and the sizes you get from read might be different still. You have to find the end of a message by looking at the data itself, not by looking at how much data is returned from read() (except for EOF, which is signaled when read() returns 0).

    Footnote: I don’t think out-of-band data does what you think it does.

    send(client_fd, buffer, sizeof("The msg was: ")+ sizeof(msg), MSG_OOB);
    

    Out-of-band data is a quirky feature of TCP that should almost certainly be avoided in modern protocols. Its implementation differs from platform to platform in such a way that it is only safe to send one byte of out-of-band data.

    It is used for some things by the Telnet protocol (and by extension, FTP, which is built on Telnet), but has no purpose in HTTP.

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

Sidebar

Related Questions

I'm trying to write a simple http web server, that (among other features), can
I am trying to write header file. I can write simple headers like add(int
I'm trying to write a simple server that .NET clients can connect to to
I'm trying to write a simple SNPP (Simple Network Paging Protocol) client using sockets.
Trying to write simple program on Fraction operations. Can't compile the code. Getting an
I am trying to write a simple IMAP client, and I want to fetch
I'm trying to write a simple HTTP web server using c, but I keep
I'm trying to write simple test. My problem is, that i want to wait
I'm trying write simple notify app in bash. I want to read output from
I'm trying to write simple proxy server for some purpose. In it I use

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.