<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Garvit Singla</title>
      <link>/blog</link>
      <description>Systems programming, network architecture, and engineering thoughts by Garvit Singla.</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="/blog/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 04 Oct 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Implementing nslookup from scratch</title>
          <pubDate>Sun, 04 Oct 2026 00:00:00 +0000</pubDate>
          <author>Garvit Singla</author>
          <link>/blog/nslookup/</link>
          <guid>/blog/nslookup/</guid>
          <description xml:base="/blog/nslookup/">&lt;p&gt;There is a very famous inteview question, what happens when you type google.com in your search bar
The first and the most obvious jargon is DNS. DNS is a translation system which converts human readable domain names to computer understandable IP addresses.&lt;/p&gt;
&lt;p&gt;I started with the concept of DNS and how it works.
then came to know about UNIX utilities like &lt;code&gt;nslookup&lt;/code&gt; and &lt;code&gt;dig&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This blog is whole about my learning and building nslookup from scratch.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;1-intro-and-understanding-10-sept-2026&quot;&gt;1. Intro and understanding (10 sept 2026)&lt;/h2&gt;
&lt;p&gt;I started with reading about DNS headers and the DNS query process.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;DNS is the Domain Name System, a hierarchical naming system for computers and services on the Internet.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;DNS is based on UDP sockets but , if there is a big DNS query, it is split into multiple packets and transmitted with the help of TCP.&lt;/p&gt;
&lt;h2 id=&quot;2-understanding-dns-query-process-11-sept-2026&quot;&gt;2. Understanding DNS query process (11 sept 2026)&lt;/h2&gt;
&lt;p&gt;Next thing was to understand what is exactly DNS query process.
For that we have to understand &lt;strong&gt;Types of DNS servers&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Root DNS servers&lt;/strong&gt;: The first level of DNS servers that are responsible for resolving the top-level domain (TLD) names.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TLD DNS servers&lt;/strong&gt;: The second level of DNS servers that are responsible for resolving the second-level domain (SLD) names.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authoritative DNS servers&lt;/strong&gt;: The third level of DNS servers that are responsible for resolving the domain names.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recursive DNS servers&lt;/strong&gt;: The fourth level of DNS servers that are responsible for resolving the domain names.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;if you want more detail refer : &lt;a rel=&quot;external&quot; href=&quot;https://www.cloudflare.com/learning/dns/dns-server-types/&quot;&gt;Cloudflare docs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So we are just quering the Recursive DNS server and not recursively finding the authoritative DNS servers.
some of them are google’s DNS &lt;code&gt;8.8.8.8&lt;/code&gt; and cloudflare’s DNS &lt;code&gt;1.1.1.1&lt;/code&gt; or provided by your ISP.&lt;/p&gt;
&lt;h2 id=&quot;3-making-dns-request-yourself-13-sept-2026&quot;&gt;3. Making DNS request yourself (13 sept 2026)&lt;/h2&gt;
&lt;p&gt;I used nslookup and wireshark to actually make DNS requests and understand the process.
&lt;img src=&quot;https://github.com/garvittsingla/nslookup/raw/main/docs/dnsPacketStructure.png&quot; alt=&quot;query&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;4-dns-header-structure&quot;&gt;4. DNS header structure&lt;/h2&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;struct&lt;/span&gt;&lt;span&gt; dns_question_format&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; qtype&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; type of question&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; qclass&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; class of question&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    char&lt;/span&gt;&lt;span&gt; qname&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;256&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; name of question&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;struct&lt;/span&gt;&lt;span&gt; dns_resource_record_format&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; type&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; type of resource record&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; class&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; class of resource record&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; ttl&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; time to live&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; rdlength&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; length of rdata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    char&lt;/span&gt;&lt;span&gt; rdata&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;256&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; rdata of resource record&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;struct&lt;/span&gt;&lt;span&gt; dns_request_format&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; for a application for multiple queries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; flags&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; some flags that indicate the type of query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; qdcount&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; number of questions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; ancount&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; number of answers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; nscount&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; number of name servers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; arcount&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; number of additional records&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; dns_question_format &lt;/span&gt;&lt;span&gt;questions&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; array of questions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; dns_resource_record_format &lt;/span&gt;&lt;span&gt;answers&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; array of answers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; dns_resource_record_format &lt;/span&gt;&lt;span&gt;name_servers&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; array of name servers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; dns_resource_record_format &lt;/span&gt;&lt;span&gt;additional_records&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; array of additional records&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;DNS header is of 12 bytes and the content inside it are ~500 bytes.&lt;/p&gt;
&lt;h2 id=&quot;5-structuring-the-project&quot;&gt;5. Structuring the project&lt;/h2&gt;
&lt;p&gt;I don’t only want to implement DNS but to write code in more maintainable, production level manner so that it is easier to read and modify later.&lt;/p&gt;
&lt;p&gt;For this project as the external dependency are very less so a simple MakeFile is enough&lt;/p&gt;
&lt;p&gt;I have made separate modules for each work in project i.e. All dns related stuff at one place, networking stuff etc.&lt;/p&gt;
&lt;p&gt;All the top level file imports are itself in the header file but will surely make a structure to include only 1 header file&lt;/p&gt;
</description>
      </item>
      <item>
          <title>Building a Custom HTTP Server from Scratch in C++</title>
          <pubDate>Thu, 17 Sep 2026 00:00:00 +0000</pubDate>
          <author>Garvit Singla</author>
          <link>/blog/http-server-in-cpp/</link>
          <guid>/blog/http-server-in-cpp/</guid>
          <description xml:base="/blog/http-server-in-cpp/">&lt;p&gt;Most web developers interact with web servers through high-level abstractions like Express, FastAPI, or Actix. We type &lt;code&gt;app.listen(8080)&lt;/code&gt; and take for granted the intricate operating system plumbing running beneath our feet.&lt;/p&gt;
&lt;p&gt;To truly understand how backend systems work, I decided to build a custom &lt;strong&gt;HTTP/1.1 server from scratch in C++&lt;/strong&gt; using raw Berkeley UNIX sockets.&lt;/p&gt;
&lt;p&gt;In this article, I’ll walk through the architectural design, object-oriented socket hierarchy, and how we listen for TCP handshakes and serve real HTTP responses.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Full source code is open source on GitHub: &lt;a rel=&quot;external&quot; href=&quot;https://github.com/garvittsingla/http-server-cpp&quot;&gt;garvittsingla/http-server-cpp&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-big-picture-how-web-traffic-actually-works&quot;&gt;The Big Picture: How Web Traffic Actually Works&lt;/h2&gt;
&lt;p&gt;At the lowest level, an HTTP server is just a user-space program asking the operating system kernel for a communication endpoint (a &lt;strong&gt;socket&lt;/strong&gt;), binding it to a local network interface and port, and waiting in an infinite loop for clients to send bytes.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ Client (Browser / cURL) ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             v  TCP 3-Way Handshake (SYN, SYN-ACK, ACK)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ Kernel TCP Stack ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             v  accept() extracts connection from backlog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ Server Process (C++) ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  - recv()   -&amp;gt; reads raw HTTP request string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  - parse()  -&amp;gt; extracts Method, Route, Headers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  - send()   -&amp;gt; writes raw HTTP/1.1 response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  - close()  -&amp;gt; terminates TCP socket&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of putting all raw C socket system calls into one massive, messy 400-line &lt;code&gt;main.cpp&lt;/code&gt;, I designed a modular, object-oriented hierarchy under the &lt;code&gt;HDE&lt;/code&gt; namespace.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;1-the-foundation-hde-simplesocket&quot;&gt;1. The Foundation: &lt;code&gt;HDE::SimpleSocket&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Every network socket in UNIX requires initializing a &lt;code&gt;struct sockaddr_in&lt;/code&gt; address structure, specifying the address family (&lt;code&gt;AF_INET&lt;/code&gt; for IPv4), byte-ordering conversion functions (&lt;code&gt;htons&lt;/code&gt; and &lt;code&gt;htonl&lt;/code&gt;), and instantiating the socket file descriptor via &lt;code&gt;socket()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here is the abstract base class:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Networking/SimpleSocket.hpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; HDE&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    class&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; SimpleSocket&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        private&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr_in&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; IP + port structure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; sock&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;                   //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; File descriptor handle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; connection&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;             //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Connection status code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        public&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            SimpleSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; u_long&lt;/span&gt;&lt;span&gt; interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Pure virtual method: forces derived classes to specify connection behavior&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            virtual&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; connect_to_network&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; sock&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr_in&lt;/span&gt;&lt;span&gt; address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            void&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; test_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; item_to_test&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr_in&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; get_address&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; get_sock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; get_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;            void&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; set_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; conn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;implementing-socket-creation&quot;&gt;Implementing Socket Creation&lt;/h3&gt;
&lt;p&gt;In &lt;code&gt;SimpleSocket.cpp&lt;/code&gt;, we construct the socket and convert the port and interface to network byte order (Big Endian):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;SimpleSocket.hpp&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;SimpleSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;SimpleSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; u_long&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; domain&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; htons&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;port&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;s_addr&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; htonl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Request a socket file descriptor from kernel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    sock&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; socket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    test_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sock&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;SimpleSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;test_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; item_to_test&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;item_to_test&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        perror&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Socket creation failed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        exit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;EXIT_FAILURE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the pure virtual &lt;code&gt;connect_to_network()&lt;/code&gt; function. A client socket uses &lt;code&gt;connect()&lt;/code&gt;, whereas a server socket uses &lt;code&gt;bind()&lt;/code&gt;. By declaring it pure virtual, &lt;code&gt;SimpleSocket&lt;/code&gt; cannot be accidentally instantiated on its own.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;2-binding-to-an-interface-hde-bindingsocket&quot;&gt;2. Binding to an Interface: &lt;code&gt;HDE::BindingSocket&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Once a socket descriptor is allocated, we must bind it to an IP address and port on the host machine. This tells the kernel: &lt;em&gt;“Any traffic arriving on port 8080 belongs to this file descriptor.”&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Networking/BindingSocket.cpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;BindingSocket.hpp&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;BindingSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;BindingSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; u_long&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    :&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; SimpleSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;    int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; conn&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; connect_to_network&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;get_sock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; get_address&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    set_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;conn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    test_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;get_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;BindingSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;connect_to_network&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span&gt; sock&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr_in&lt;/span&gt;&lt;span&gt; address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sock&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By overriding &lt;code&gt;connect_to_network&lt;/code&gt;, we invoke the POSIX &lt;code&gt;bind()&lt;/code&gt; system call seamlessly during initialization.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;3-entering-the-passive-state-hde-listeningsocket&quot;&gt;3. Entering the Passive State: &lt;code&gt;HDE::ListeningSocket&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;A bound socket cannot accept incoming connections until it is explicitly switched into &lt;strong&gt;passive listening mode&lt;/strong&gt; via &lt;code&gt;listen()&lt;/code&gt;. We also specify a &lt;code&gt;backlog&lt;/code&gt; parameter, which defines the maximum length of the queue of pending connections.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Networking/ListeningSocket.cpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;ListeningSocket.hpp&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;ListeningSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ListeningSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; u_long&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; interface&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; bklg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    :&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; BindingSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;domain&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; protocol&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; port&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; interface&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    backlog&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; bklg&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    start_listening&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    test_connection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;listening&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;ListeningSocket&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;start_listening&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    listening&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; listen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;get_sock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; backlog&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With just three focused classes, we have encapsulated socket creation, network binding, and listener state with robust error checking at each phase.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;4-the-server-event-loop-handling-incoming-requests&quot;&gt;4. The Server Event Loop: Handling Incoming Requests&lt;/h2&gt;
&lt;p&gt;Now comes the fun part: serving real HTTP requests in &lt;code&gt;berner.cpp&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Networking/ListeningSocket.hpp&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;iostream&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;unistd.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;sys/socket.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;netinet/in.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;arpa/inet.h&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;cstring&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Listen on IPv4 (AF_INET), TCP (SOCK_STREAM), Port 8080, Any Interface, Backlog 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    HDE&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;ListeningSocket&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; serverSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        AF_INET&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; SOCK_STREAM&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 8080&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; INADDR_ANY&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;    std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Server listening on port 8080...&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;endl&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr_in&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; client_address&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        socklen_t&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; address_len&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_address&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; 1. Accept incoming TCP connection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        int&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; client_fd&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; accept&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            serverSocket&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;get_sock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; sockaddr&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_address&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;            &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;address_len&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_fd&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            perror&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;accept failed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;            continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Print connecting client&amp;#39;s IP address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; client_ip&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;INET_ADDRSTRLEN&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        inet_ntop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;AF_INET&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_address&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;sin_addr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; client_ip&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; INET_ADDRSTRLEN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;        std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Connection received from &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; client_ip&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;endl&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; 2. Read the raw HTTP request bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        ssize_t&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; bytes_received&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; recv&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;bytes_received&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            perror&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;recv failed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_fd&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;            continue&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        buffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;bytes_received&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;        std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Request payload:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; buffer&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;endl&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; 3. Format valid HTTP/1.1 response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-b z-d-6 z-d-b&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Content-Length: 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;Hi&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; 4. Send response &amp;amp; terminate client socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_fd&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; response&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; strlen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;response&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;client_fd&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;testing-the-server&quot;&gt;Testing the Server&lt;/h2&gt;
&lt;p&gt;We compile using standard C++17 with &lt;code&gt;-Wall&lt;/code&gt; flags:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;g++&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;std=c++17&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Wall&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;I.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;  berner.cpp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;  Networking/SimpleSocket.cpp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;  Networking/BindingSocket.cpp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;  Networking/ListeningSocket.cpp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; server&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run the binary:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;./server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Output: Server listening on port 8080...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now fire up a terminal and test it with cURL:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;curl&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; http://localhost:8080&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Response:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;http&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;HTTP&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;1.1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 200&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; OK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; text/plain&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;Content-Length&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Hi&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works! The browser and cURL parse our headers and body seamlessly because our byte stream strictly respects the RFC 2616 HTTP specification.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;what-s-next-concurrency-epoll&quot;&gt;What’s Next: Concurrency &amp;amp; Epoll&lt;/h2&gt;
&lt;p&gt;This simple server uses a synchronous, blocking model—it handles one connection at a time before calling &lt;code&gt;accept()&lt;/code&gt; again.&lt;/p&gt;
&lt;p&gt;In production servers (like Nginx), scalability is achieved by using non-blocking I/O multiplexing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;select&lt;/code&gt; / &lt;code&gt;poll&lt;/code&gt;&lt;/strong&gt;: Cross-platform but $O(N)$ scanning overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;epoll&lt;/code&gt; on Linux&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;kqueue&lt;/code&gt; on macOS / BSD&lt;/strong&gt;: $O(1)$ event notifications triggered by ready file descriptors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thread Pools&lt;/strong&gt;: Worker threads consuming client sockets from a thread-safe queue.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Building this project demystified the magic of the web stack for me. Every framework you use is ultimately built on these exact primitives.&lt;/p&gt;
&lt;p&gt;Check out the full repository on GitHub: &lt;a rel=&quot;external&quot; href=&quot;https://github.com/garvittsingla/http-server-cpp&quot;&gt;garvittsingla/http-server-cpp&lt;/a&gt;.&lt;/p&gt;
</description>
      </item>
      <item>
          <title>You suck at Subnetting</title>
          <pubDate>Fri, 12 Jun 2026 00:00:00 +0000</pubDate>
          <author>Garvit Singla</author>
          <link>/blog/you-suck-at-subnetting/</link>
          <guid>/blog/you-suck-at-subnetting/</guid>
          <description xml:base="/blog/you-suck-at-subnetting/">&lt;p&gt;I was studying for my Computer Networks exam late at night, and I came across a topic that almost every computer science student dreads at first: &lt;strong&gt;Subnetting&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Most textbooks throw binary math at you right away: &lt;em&gt;255.255.255.192, AND operations, borrow 3 bits, calculate magic numbers&lt;/em&gt;. But when you strip away the convoluted formulas, subnetting is just a clean, logical puzzle of organizing computers into neighborhoods.&lt;/p&gt;
&lt;p&gt;Here is the intuition behind how a single IPv4 address block is partitioned across different departments, branches, and organizations without burning through scarce address space.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-core-problem-why-subnet-at-all&quot;&gt;The Core Problem: Why Subnet at All?&lt;/h2&gt;
&lt;p&gt;An IPv4 address consists of &lt;strong&gt;32 bits&lt;/strong&gt;, typically written in dotted-decimal format:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;192.168.1.1  =&amp;gt;  11000000 . 10101000 . 00000001 . 00000001&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the early days of the Internet, addresses were categorized into rigid classes (Class A, Class B, Class C). If a company needed 300 IP addresses, a Class C network (254 usable hosts) was too small, so they were granted a Class B network (65,534 usable hosts).&lt;/p&gt;
&lt;p&gt;The result? Over &lt;strong&gt;65,000 addresses were completely wasted&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Subnetting—and specifically &lt;strong&gt;Classless Inter-Domain Routing (CIDR)&lt;/strong&gt;—was invented to solve this exact problem by decoupling network boundaries from octet octets.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;anatomy-of-an-ip-address-network-vs-host&quot;&gt;Anatomy of an IP Address: Network vs Host&lt;/h2&gt;
&lt;p&gt;Every IP address has two responsibilities:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Network ID&lt;/strong&gt;: Which street or neighborhood the packet belongs to.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Host ID&lt;/strong&gt;: Which specific apartment unit on that street receives the packet.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;strong&gt;Subnet Mask&lt;/strong&gt; tells routers where the Network ID ends and where the Host ID begins:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;IP Address:   192.168.1.50   -&amp;gt;  11000000.10101000.00000001.00110010&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Subnet Mask:  255.255.255.0  -&amp;gt;  11111111.11111111.11111111.00000000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                 [------- Network ID -------] [ Host ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you see &lt;code&gt;/24&lt;/code&gt; (CIDR notation), it literally means: &lt;strong&gt;the first 24 bits are fixed as the network prefix&lt;/strong&gt;. The remaining 8 bits ($32 - 24 = 8$) are left for hosts:&lt;/p&gt;
&lt;p&gt;$$\text{Usable Hosts} = 2^{\text{host bits}} - 2 = 2^8 - 2 = 254$$&lt;/p&gt;
&lt;p&gt;Why subtract 2?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;All 0s in host portion&lt;/strong&gt;: Represents the Network Address itself.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;All 1s in host portion&lt;/strong&gt;: Represents the Broadcast Address for everyone in that subnet.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;borrowing-bits-the-subnetting-trick&quot;&gt;Borrowing Bits: The Subnetting Trick&lt;/h2&gt;
&lt;p&gt;Suppose your organization is assigned &lt;code&gt;192.168.1.0/24&lt;/code&gt;, but you have 4 separate teams:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Engineering (50 people)&lt;/li&gt;
&lt;li&gt;Design (25 people)&lt;/li&gt;
&lt;li&gt;Sales (20 people)&lt;/li&gt;
&lt;li&gt;Operations (15 people)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Instead of begging for 4 separate Class C ranges, we &lt;strong&gt;borrow bits from the host portion&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;To create at least 4 subnets, we need $2^n \ge 4 \implies n = 2$ bits.&lt;/p&gt;
&lt;p&gt;Now our subnet mask increases from &lt;code&gt;/24&lt;/code&gt; to &lt;code&gt;/26&lt;/code&gt; (&lt;code&gt;24 + 2 = 26&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;New Mask: 255.255.255.192 (/26)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Remaining Host bits: 32 - 26 = 6 bits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Hosts per subnet: 2^6 - 2 = 62 usable hosts&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each subnet has a step size (block size) of $256 - 192 = 64$:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: left&quot;&gt;Subnet&lt;/th&gt;&lt;th style=&quot;text-align: left&quot;&gt;Network Address&lt;/th&gt;&lt;th style=&quot;text-align: left&quot;&gt;Usable Host Range&lt;/th&gt;&lt;th style=&quot;text-align: left&quot;&gt;Broadcast Address&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Engineering&lt;/strong&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.0/26&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.1&lt;/code&gt; – &lt;code&gt;192.168.1.62&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.63&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Design&lt;/strong&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.64/26&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.65&lt;/code&gt; – &lt;code&gt;192.168.1.126&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.127&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Sales&lt;/strong&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.128/26&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.129&lt;/code&gt; – &lt;code&gt;192.168.1.190&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.191&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Operations&lt;/strong&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.192/26&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.193&lt;/code&gt; – &lt;code&gt;192.168.1.254&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align: left&quot;&gt;&lt;code&gt;192.168.1.255&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Zero overlap, complete broadcast isolation, and optimal resource utilization.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-rule-of-thumb-for-your-next-exam-or-interview&quot;&gt;The Rule of Thumb for Your Next Exam or Interview&lt;/h2&gt;
&lt;p&gt;Whenever you need to calculate subnets quickly in your head:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Identify the number of hosts required.&lt;/li&gt;
&lt;li&gt;Find the smallest power of 2 that satisfies: $2^h - 2 \ge \text{required hosts}$.&lt;/li&gt;
&lt;li&gt;Your CIDR prefix will be $32 - h$.&lt;/li&gt;
&lt;li&gt;Your block size is always $2^h$.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Next time someone tells you subnetting is hard, remember it’s just cutting a pie into powers of two.&lt;/p&gt;
</description>
      </item>
      <item>
          <title>How to actually win in life (Real world experiment)</title>
          <pubDate>Wed, 20 May 2026 00:00:00 +0000</pubDate>
          <author>Garvit Singla</author>
          <link>/blog/how-to-actually-win-in-life/</link>
          <guid>/blog/how-to-actually-win-in-life/</guid>
          <description xml:base="/blog/how-to-actually-win-in-life/">&lt;p&gt;In a world governed by cutthroat competition and pure self-interest, how does cooperation ever take root?&lt;/p&gt;
&lt;p&gt;Why would any rational individual choose to cooperate if a selfish betrayer can exploit them?&lt;/p&gt;
&lt;p&gt;The answer isn’t wishful thinking or moral preaching. It was proven by a real-world computer science experiment conducted by political scientist &lt;strong&gt;Robert Axelrod&lt;/strong&gt; in the 1980s.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;axelrod-s-tournament-the-setup&quot;&gt;Axelrod’s Tournament: The Setup&lt;/h2&gt;
&lt;p&gt;Axelrod invited the world’s leading game theorists, mathematicians, economists, and computer scientists to submit computer programs to play the &lt;strong&gt;Iterated Prisoner’s Dilemma&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In each round between two players:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If both &lt;strong&gt;Cooperate&lt;/strong&gt;, both receive &lt;strong&gt;3 points&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If one &lt;strong&gt;Defects&lt;/strong&gt; while the other Cooperates, the defector gets &lt;strong&gt;5 points&lt;/strong&gt; (temptation) while the sucker gets &lt;strong&gt;0 points&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If both &lt;strong&gt;Defect&lt;/strong&gt;, both receive &lt;strong&gt;1 point&lt;/strong&gt; (punishment).&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  Player B: Cooperate    Player B: Defect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Player A: Cooperate     (3, 3)                (0, 5)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Player A: Defect        (5, 0)                (1, 1)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In a single game, the strictly dominant mathematical strategy is always to &lt;strong&gt;Defect&lt;/strong&gt;. If you defect, you’re guaranteed to do better regardless of what the other person does.&lt;/p&gt;
&lt;p&gt;So what happens when the game is played hundreds of times with no known end date?&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-champion-tit-for-tat&quot;&gt;The Champion: Tit for Tat&lt;/h2&gt;
&lt;p&gt;Scientists submitted sophisticated programs with probabilistic algorithms, deceptive traps, and psychological heuristics.&lt;/p&gt;
&lt;p&gt;Yet the winning strategy was the simplest program in the entire competition: &lt;strong&gt;Tit for Tat&lt;/strong&gt;, written by Anatol Rapoport in just &lt;strong&gt;four lines of BASIC code&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;On the very first round, &lt;strong&gt;Cooperate&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;On every subsequent round, &lt;strong&gt;do whatever the other player did on the previous round&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That’s it.&lt;/p&gt;
&lt;p&gt;When pitted against aggressive defectors, complex algorithms, and sneaky cheats, Tit for Tat consistently outperformed every other strategy across hundreds of thousands of simulated interactions.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-four-principles-of-long-term-success&quot;&gt;The Four Principles of Long-Term Success&lt;/h2&gt;
&lt;p&gt;Axelrod analyzed what made Tit for Tat invincible. He discovered four specific characteristics that separated the winning algorithms from the losers:&lt;/p&gt;
&lt;h3 id=&quot;1-be-nice-never-be-the-first-to-defect&quot;&gt;1. Be Nice (Never Be the First to Defect)&lt;/h3&gt;
&lt;p&gt;Top-performing strategies were “nice”—they never initiated hostility or deception. In any repeated game, initiating distrust creates negative-sum spirals where both sides lose energy defending themselves.&lt;/p&gt;
&lt;h3 id=&quot;2-be-retaliatory-don-t-be-a-pushover&quot;&gt;2. Be Retaliatory (Don’t Be a Pushover)&lt;/h3&gt;
&lt;p&gt;When the other party defects, Tit for Tat punishes them immediately on the very next move. Being purely altruistic (turning the other cheek forever) invites exploitation by bad actors.&lt;/p&gt;
&lt;h3 id=&quot;3-be-forgiving-don-t-hold-grudges&quot;&gt;3. Be Forgiving (Don’t Hold Grudges)&lt;/h3&gt;
&lt;p&gt;As soon as the defector returns to cooperation, Tit for Tat immediately resumes cooperating. It doesn’t hold multi-turn grudges or demand excessive apologies. This prevents endless cycles of mutual retaliation.&lt;/p&gt;
&lt;h3 id=&quot;4-be-clear-and-predictable-zero-deception&quot;&gt;4. Be Clear and Predictable (Zero Deception)&lt;/h3&gt;
&lt;p&gt;Tit for Tat is completely transparent. The other party quickly figures out that cooperating brings steady rewards, while defecting causes immediate retaliation.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;translating-game-theory-to-everyday-life&quot;&gt;Translating Game Theory to Everyday Life&lt;/h2&gt;
&lt;p&gt;Life is an iterated game, not a one-shot transaction:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“You don’t win by being cutthroat. You win by being clear, trustworthy, principled when wronged, and quick to reconcile.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;In Engineering &amp;amp; Open Source&lt;/strong&gt;: Build genuine goodwill, ship clean contributions, and protect your boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In Friendships &amp;amp; Business&lt;/strong&gt;: Never screw people over for short-term gain. The compound interest of a spotless reputation dwarfs any quick payoff.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Start with trust. Protect your standards. Forgive quickly.&lt;/p&gt;
</description>
      </item>
      <item>
          <title>GitHub Actions for dummies</title>
          <pubDate>Mon, 18 May 2026 00:00:00 +0000</pubDate>
          <author>Garvit Singla</author>
          <link>/blog/github-actions-for-dummies/</link>
          <guid>/blog/github-actions-for-dummies/</guid>
          <description xml:base="/blog/github-actions-for-dummies/">&lt;p&gt;Have you ever wondered how tech teams manage to push code into production ten times a day without setting everything on fire?&lt;/p&gt;
&lt;p&gt;How do they ensure thousands of unit tests run, linters pass, Docker containers build, and staging deployments trigger automatically—the exact moment a pull request is merged?&lt;/p&gt;
&lt;p&gt;The magic behind this is &lt;strong&gt;Continuous Integration &amp;amp; Continuous Deployment (CI/CD)&lt;/strong&gt;, and &lt;strong&gt;GitHub Actions&lt;/strong&gt; is the cleanest tool to master it.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-mental-model-what-is-github-actions&quot;&gt;The Mental Model: What is GitHub Actions?&lt;/h2&gt;
&lt;p&gt;Think of GitHub Actions as a free, ephemeral cloud computer (a runner) that spins up in an isolated sandbox, executes whatever terminal commands you specify, and then disappears.&lt;/p&gt;
&lt;p&gt;There are only &lt;strong&gt;4 core concepts&lt;/strong&gt; you need to understand:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Event / Trigger]  -&amp;gt;  [Workflow file (.github/workflows/*.yml)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                              |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     +--------+--------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     |                 |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  [Job 1]           [Job 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     |                 |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  [Steps]           [Steps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  - Checkout        - Run Build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  - Run Tests       - Deploy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Workflow&lt;/strong&gt;: The YAML configuration file residing in &lt;code&gt;.github/workflows/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Event (Trigger)&lt;/strong&gt;: What kicks off the workflow (&lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pull_request&lt;/code&gt;, &lt;code&gt;schedule&lt;/code&gt;, or manual button &lt;code&gt;workflow_dispatch&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Job&lt;/strong&gt;: A set of steps executed on the same virtual machine (e.g. &lt;code&gt;ubuntu-latest&lt;/code&gt;, &lt;code&gt;macos-latest&lt;/code&gt;, &lt;code&gt;windows-latest&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step&lt;/strong&gt;: An individual task—either running a shell command (&lt;code&gt;run: npm test&lt;/code&gt;) or using a pre-packaged community action (&lt;code&gt;uses: actions/checkout@v4&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;writing-your-first-workflow-in-60-seconds&quot;&gt;Writing Your First Workflow in 60 Seconds&lt;/h2&gt;
&lt;p&gt;Create a file in your repository: &lt;code&gt;.github/workflows/test.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;est and Lint Suite&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; 1. Trigger when code is pushed to main or opened in a PR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;  p&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ush&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    b&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ranches&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;ain&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;  p&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ull_request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    b&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ranches&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;ain&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;obs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;  v&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;erify&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; L&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;int &amp;amp; Run Unit Tests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    r&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;uns-on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; u&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;buntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    s&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;teps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Step 1: Clone the repository onto the runner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;heckout Code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        u&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;ctions/checkout@v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Step 2: Install Node runtime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; S&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;etup Node.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        u&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;ctions/setup-node@v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        w&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ith&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;          n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ode-version&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;          c&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ache&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Step 3: Install dependencies&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; I&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;nstall dependencies&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        r&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;un&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;pm ci&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Step 4: Run linter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; R&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;un ESLint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        r&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;un&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;pm run lint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-3 z-d-i&quot;&gt; Step 5: Execute test suite&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; R&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;un Test Suite&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        r&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;un&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;pm test&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s all it takes! Every time anyone submits a pull request, GitHub spins up an Ubuntu VM, pulls down your repository, installs packages, runs your checks, and reports a green checkmark or red cross directly next to their commit.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;3-pro-tips-for-production-pipelines&quot;&gt;3 Pro-Tips for Production Pipelines&lt;/h2&gt;
&lt;h3 id=&quot;1-matrix-builds&quot;&gt;1. Matrix Builds&lt;/h3&gt;
&lt;p&gt;Test across multiple platforms or language versions simultaneously:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;trategy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;  m&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;atrix&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    o&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;buntu-latest&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;acos-latest&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    n&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ode-version&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;18&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 20&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; 22&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;2-protect-secrets&quot;&gt;2. Protect Secrets&lt;/h3&gt;
&lt;p&gt;Never commit &lt;code&gt;.env&lt;/code&gt; files or API tokens! Store them under &lt;strong&gt;Repository Settings &amp;gt; Secrets and variables &amp;gt; Actions&lt;/strong&gt;, and inject them cleanly:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;  D&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;ATABASE_URL&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt; $&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-2&quot;&gt;{{ secrets.PROD_DATABASE_URL }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;3-fail-fast-cache&quot;&gt;3. Fail Fast &amp;amp; Cache&lt;/h3&gt;
&lt;p&gt;Always enable dependency caching (&lt;code&gt;cache: &#39;npm&#39;&lt;/code&gt; or &lt;code&gt;cache: &#39;cargo&#39;&lt;/code&gt;). It can turn a 5-minute build into a 25-second breeze.&lt;/p&gt;
&lt;p&gt;Automate the boring stuff so you can focus on building what matters.&lt;/p&gt;
</description>
      </item>
    </channel>
</rss>
