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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:18:14+00:00 2026-05-16T11:18:14+00:00

I am trying to make a simple HTTP server that would be able to

  • 0

I am trying to make a simple HTTP server that would be able to parse client requests and send responses back.

Now I have a problem. I have to read and handle one line at a time in the request, and I don’t know if I should:

  • read one byte at a time, or
  • read chunks of N bytes at a time, put them in a buffer, and then handle the bytes one by one, before reading a new chunk of bytes.

What would be the best option, and why?

Also, are there some alternative solutions to this? Like a function that would read a line at a time from the socket or something?

  • 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-16T11:18:14+00:00Added an answer on May 16, 2026 at 11:18 am

    The short answer to your question is that I would go with reading a single byte at a time. Unfortunately its one of those cases where there are pros and cons for both cases.

    For the use of a buffer is the fact that the implementation can be more efficient from the perspective of the network IO. Against the use of a buffer, I think that the code will be inherently more complex than the single byte version. So its an efficiency vs complexity trade off. The good news is that you can implement the simple solution first, profile the result and “upgrage” to a buffered approach if testing shows it to be worthwhile.

    Also, just to note, as a thought experiment I wrote some pseudo code for a loop that does buffer based reads of http packets, included below. The complexity to implement a buffered read doesn’t seem to bad. Note however that I haven’t given much consideration to error handling, or tested if this will work at all. However, it should avoid excessive “double handling” of data, which is important since that would reduce the efficiency gains which were the purpose of this approach.

    #define CHUNK_SIZE 1024
    
    nextHttpBytesRead = 0;
    nextHttp = NULL;
    while (1)
    {
      size_t httpBytesRead = nextHttpBytesRead;
      size_t thisHttpSize;
      char *http = nextHttp;
      char *temp;
      char *httpTerminator;
    
      do
      {
        temp = realloc(httpBytesRead + CHUNK_SIZE);
        if (NULL == temp)
          ...
        http = temp;
    
        httpBytesRead += read(httpSocket, http + httpBytesRead, CHUNK_SIZE);
        httpTerminator = strstr(http, "\r\n\r\n");
      }while (NULL == httpTerminator)
    
      thisHttpSize = ((int)httpTerminator - (int)http + 4; // Include terminator
      nextHttpBytesRead = httpBytesRead - thisHttpSize;
    
      // Adding CHUNK_SIZE here means that the first realloc won't have to do any work
      nextHttp = malloc(nextHttpBytesRead + CHUNK_SIZE);
      memcpy(nextHttp,  http + thisHttpSize, nextHttpSize);
    
      http[thisHttpSize] = '\0';
      processHttp(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 make simple http server, that can be pause and resume,, I've
I am trying to make a very simple XML RPC Server with Python that
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
I have another simple question about Node. I'm trying to make a simple http
I'm trying to make a basic client-server application for windows phone 7 (using Mango
I am trying to create a client server application. I am going to make
im currently trying to make a simple IRC Gui Client. Im using the SmartIrc4net
I'm trying to follow http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ And have created a very simple example http://jsfiddle.net/6wbZQ/ It's
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make simple website with content background combined from 3 images: top

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.