<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kelvin Kemboi  on Medium]]></title>
        <description><![CDATA[Stories by Kelvin Kemboi  on Medium]]></description>
        <link>https://medium.com/@kemboikelvin185?source=rss-a5cd20063144------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VdBnQhVOeDdHDf7u</url>
            <title>Stories by Kelvin Kemboi  on Medium</title>
            <link>https://medium.com/@kemboikelvin185?source=rss-a5cd20063144------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:56:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kemboikelvin185/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What Building an HTTP Server From Raw Sockets Taught Me About Systems]]></title>
            <link>https://medium.com/@kemboikelvin185/what-building-an-http-server-from-raw-sockets-taught-me-about-systems-7da378d731b4?source=rss-a5cd20063144------2</link>
            <guid isPermaLink="false">https://medium.com/p/7da378d731b4</guid>
            <category><![CDATA[parsing-string]]></category>
            <category><![CDATA[raw-posix]]></category>
            <category><![CDATA[http-server]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <dc:creator><![CDATA[Kelvin Kemboi ]]></dc:creator>
            <pubDate>Fri, 31 Jul 2026 18:19:13 GMT</pubDate>
            <atom:updated>2026-07-31T18:19:13.757Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hddz28sFF7O4FrSj_Yy9wQ.png" /><figcaption>My index page</figcaption></figure><p>A lot of us treat the internet like magic. You open a browser, type localhost:8080 or google.com, hit Enter, and a fully formed webpage pops up on your screen.</p><p>When you start learning web development, modern frameworks make this feel even more like magic. Express.js, Django, or Spring Boot let you spin up a server in a few lines of code. You write @GetMapping(&quot;/hello&quot;), return a string, and call it a day. Yay.</p><p>But a few weeks ago, I decided to pull back the curtain and see what happens under the hood before Express or Spring gets to touch a request. Inspired by a few low-level projects I stumbled across on GitHub, I decided to build my own C++ HTTP web server from scratch using nothing but raw sockets.</p><p>Here’s what that journey taught me about low-level systems, string manipulation, error handling, and how the web actually works.</p><h3>1. Demystifying the Socket: The Internet’s Phone Call</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nNS8qdpXNF622WnzSvOg9w.png" /><figcaption>The socket flow</figcaption></figure><p>Before writing code, I had to understand what a <strong>socket</strong> actually is. I spent an afternoon engaging with <em>Beej’s Guide to Network Programming</em> to figure out how system-level networking fits together.</p><p>To help you understand what I learned, let me frame it as if it were a mail delivery. If you want to send a letter to a friend, you need their address. In networking, a computer’s address is its <strong>IP address</strong>, and the specific apartment unit inside that building is the <strong>Port</strong>. A socket is essentially the door through which data travels between your program and the network. Simple enough, right?</p><p>If not, think of opening a socket like picking up a telephone landline:</p><ol><li><strong>socket()</strong>: You pick up the phone.</li><li><strong>bind()</strong>: You assign your phone number (Port 8080 on IP 127.0.0.1).</li><li><strong>listen()</strong>: You wait for the phone to ring.</li><li><strong>accept()</strong>: You answer the incoming call.</li></ol><p>Once the call is answered, a connection is established, and the <strong>Server Cycle</strong> begins.</p><h3>2. The Anatomy of a Server Cycle</h3><p>High-level web frameworks hide the lifecycle of a request, but at the socket level, every single request follows an uncompromising, mechanical flow:</p><p>Open Connection -&gt; Receive(Recv) -&gt; Process/Create Response</p><p>When a user visits your site in a browser, your server steps through this exact flow:</p><ol><li><strong>Accepting the Connection:</strong> The browser calls your server, and the OS gives you a <em>client socket file descriptor. (</em>think of a temporary straw connected directly to that specific user)</li><li><strong>Receiving Data (</strong><strong>recv):</strong> You read raw bytes coming through the straw into a memory buffer.</li><li><strong>Creating the Response:</strong> You read those bytes, figure out what the user wants (like an index.html file or a .png image), and craft an HTTP response string.</li><li><strong>Sending Data (</strong><strong>send):</strong> You push your response bytes back through the straw to the browser.</li><li><strong>Closing Connection:</strong> You hang up the phone (or keep it open if it’s a keep-alive connection).</li></ol><p>It sounds simple on paper, but the moment you start dealing with raw bytes, reality hits you fast.</p><h3>3. Array Byte Sizes and The Illusion of “Strings”</h3><p>When you’re learning higher-level languages like Python or JavaScript, text is just text. You call request.text, and it works.</p><p>In C++, when data comes over a raw socket via recv(), <strong>it isn’t text, but a bucket of raw bytes</strong>.</p><p>I initialized a buffer in memory to catch incoming requests:</p><pre>char buffer[1024]; // A fixed array of 1024 bytes</pre><p>But why 1024 bytes? Why not a round number like 1,000?</p><p>In Computer Science, <strong>1024 is a power of two</strong>. Because binary hardware, CPU registers, and virtual memory systems manage data using binary addressing (bits and bytes), allocating memory in powers of two, ie 512 or 4096, fits cleanly into standard CPU cache lines and memory pages without leaving wasted alignment gaps.</p><p>That fixed 1024-byte array immediately forced me to confront the reality of <strong>data sizing</strong>:</p><ul><li><strong>If a request is small (say, 300 bytes):</strong> Only 300 bytes of the array get filled. What happens to the remaining 724 bytes? If you aren’t careful to zero out your buffer, you risk reading leftover “garbage” data from previous operations.</li><li><strong>If a request is huge (say, a 5,000-byte POST request or photo upload):</strong> A single 1024-byte array can’t hold it. The request gets chopped in half mid-sentence!</li></ul><p>Managing buffer sizes, zeroing out memory, and figuring out where a request actually ends was my first real trial in systems programming.</p><h3>4. String Parsing: Deconstructing HTTP by Hand</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5hoYzsRyAXIA6sXIceFOA.png" /><figcaption>HTTP deconstruction</figcaption></figure><p>Once you capture those raw bytes into a array, you have to read them. An HTTP request looks like plain text, but it follows strict formatting rules:</p><pre>GET /index.html HTTP/1.1<br>Host: localhost:8080<br>User-Agent: Mozilla/5.0...<br>Accept: text/html</pre><p>Because I wasn’t using a framework, I had to parse this string manually in C++.</p><p>I had to write code that would:</p><ul><li>Find the first line (the <strong>Request Line</strong>).</li><li>Extract the verb (GET), the path (/index.html), and the protocol (HTTP/1.1).</li><li>Look for double line breaks (\r\n\r\n) to figure out where the request headers end and the body begins.</li></ul><p>I had to navigate C++ string methods like std::string, std::stringstream, find(), and substr() .</p><h3>5. Error Handling: Expecting Everything to Break</h3><p>In high-level languages, an error throws an exception, and a framework catches it to return a nice 500 Internal Server Error page.</p><p>In low-level socket programming, <strong>errors don’t raise neat exceptions but instead return negative integers.</strong></p><ul><li>If socket() fails, it returns -1.</li><li>If bind() fails (maybe port 8080 is already in use by Spotify or Docker), it returns -1.</li><li>If recv() returns 0, it means the client disconnected mid-transfer.</li></ul><p>I had to wrap every single line of networking code in error-checking conditionals cause if you don’t explicitly check if a socket read failed, your server will <strong>segfault </strong>and crash the moment two requests hit it simultaneously.</p><h3>6. Rendering Files: From HTML to Images (MIME Types)</h3><p>The most satisfying part of working on this project was getting my server to read an index.html file off my hard drive and serve it to Chrome.</p><p>When you open http://localhost:8080, your server sends back an HTTP response header that looks like this:</p><pre>HTTP/1.1 200 OK<br>Content-Type: text/html<br>Content-Length: 450</pre><pre>&lt;html&gt;...&lt;/html&gt;</pre><p>The browser reads Content-Type: text/html, knows it&#39;s a webpage, and renders it.</p><p><strong>Then came the next bug.</strong> My HTML page referenced a CSS file and a .png image. When the browser loaded the HTML, it immediately opened <em>two new socket connections</em> to request style.css and logo.png.</p><p>My server broke. Why? Because I was sending Content-Type: text/html for <em>everything</em>! The browser tried to render raw binary image bytes as HTML text, leading to broken images and unstyled pages.</p><p>I had to map file extensions to their correct <strong>MIME Types</strong>:</p><ul><li>.html as text/html</li><li>.css astext/css</li><li>.png asimage/png</li><li>.jpeg as image/jpeg</li></ul><p>For text files (.html, .css), reading them as standard strings worked fine. But for binary files like images, I had to read the files directly into byte buffers without string conversions.</p><h3>Final Thoughts: Why You Should Build Low-Level Projects</h3><p>Navigating C++ syntax, headers like (&lt;sys/socket.h&gt;, &lt;netinet/in.h&gt;), byte arrays, and pointer manipulation was intimidating. There were moments where I was genuinely about to give up and go back to having Express handle everything for me.</p><p>I won’t be using my raw C++ HTTP server in production; Express exists for a very good reason. However, building it completely changed how I look at backend engineering.</p><p>If you want to really understand systems, step away from the frameworks for a weekend. Grab C++, C, or Rust, pick up raw sockets, and build something from the ground up. It’s hard, it’s frustrating, but it’s easily one of the best learning experiences you can have as an engineer. It certainly was for me!</p><p>Read my code at: <a href="https://github.com/KelvinKemboi/Mini-HTTP-server">https://github.com/KelvinKemboi/Mini-HTTP-server</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7da378d731b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>