<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shruti Maharjan on Medium]]></title>
        <description><![CDATA[Stories by Shruti Maharjan on Medium]]></description>
        <link>https://medium.com/@shrutimaharjan2005?source=rss-35d766d69946------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*J08xk-yz6XTgHqXe1ASBzg.jpeg</url>
            <title>Stories by Shruti Maharjan on Medium</title>
            <link>https://medium.com/@shrutimaharjan2005?source=rss-35d766d69946------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 16:55:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shrutimaharjan2005/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setting Up a DHCP Server with Ubuntu and VMware Workstation]]></title>
            <link>https://medium.com/@shrutimaharjan2005/setting-up-a-dhcp-server-with-ubuntu-and-vmware-workstation-e6533b2f5453?source=rss-35d766d69946------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6533b2f5453</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[vmware-workstation]]></category>
            <category><![CDATA[dhcp-server]]></category>
            <category><![CDATA[vm]]></category>
            <category><![CDATA[vmware]]></category>
            <dc:creator><![CDATA[Shruti Maharjan]]></dc:creator>
            <pubDate>Sun, 08 Nov 2026 05:52:13 GMT</pubDate>
            <atom:updated>2026-11-08T05:52:13.548Z</atom:updated>
            <content:encoded><![CDATA[<p>Whenever a device connects to a network, it needs an IP address to communicate with other devices. But imagine having to manually configure an IP address on every laptop, phone, and computer connected to a network. That would quickly become inconvenient!</p><p>This is where DHCP comes in.</p><p><strong>DHCP (Dynamic Host Configuration Protocol)</strong> is a network protocol that automatically assigns IP addresses and other network configuration information to devices when they join a network.</p><p>Most home routers already have a built-in DHCP server. But instead of relying on one, let’s set up our own!</p><p>In this lab, we’ll configure an Ubuntu virtual machine to act as a DHCP server using ISC DHCP Server. Then, we’ll connect another Ubuntu virtual machine to the same network and verify that it receives an IP address automatically.</p><p>We’ll use VMware Workstation to create an isolated virtual network so that our experiment doesn’t interfere with the existing network.</p><p>Let’s get started!</p><h3>1. Understanding the setup</h3><p>Before configuring anything, let’s understand what we’re trying to build.</p><p>We’ll need two virtual machines:</p><ol><li><strong>DHCP Server:</strong> This machine will run ISC DHCP Server and assign IP addresses.</li><li><strong>DHCP Client:</strong> This machine will request an IP address from our DHCP server.</li></ol><p>Both machines will be connected to the same isolated virtual network.</p><p>Our network will use the following configuration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NZ1O-9lBxLxUGEtgLDm3eA.png" /></figure><p>We’ll also give our DHCP server a second network adapter connected to NAT. This will allow it to access the internet when installing the required packages.</p><h3>2. Creating an Ubuntu virtual machine</h3><p>First, download an Ubuntu ISO image from the official Ubuntu website:</p><p><a href="https://ubuntu.com/download/desktop">https://ubuntu.com/download/desktop</a></p><p>Open VMware Workstation and create a new virtual machine.</p><p>Follow the installation steps:</p><ol><li>Select the downloaded Ubuntu ISO image.</li><li>Allocate memory and processor resources.</li><li>Create a virtual disk.</li><li>Complete the Ubuntu installation.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EeF-iTuV8hLygm-SfIg1fA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b3HqqQJ7CqAIyblYdqWNYQ.png" /></figure><p>For this lab, I’m using Ubuntu Desktop. Ubuntu Server works too, and the steps are nearly identical. One difference: Desktop manages networking through NetworkManager, so when we edit the Netplan file later, we’ll also add renderer: networkd under network: so our static IP is applied.</p><p>Once Ubuntu is installed, start the virtual machine.</p><h3>3. Creating an isolated virtual network in VMware</h3><p>Before configuring the DHCP server, we need a separate network for our lab.</p><p>By default, VMware commonly connects virtual machines to a NAT network. NAT allows a virtual machine to access the internet through the host computer’s network connection.</p><p>However, VMware’s NAT network usually has its own DHCP service.</p><p>If we leave that DHCP service enabled while testing our own DHCP server on the same network, both servers could respond to client requests. This would make our experiment unpredictable.</p><p>To avoid that, we’ll create a dedicated virtual network.</p><h3>Step 1: Open Virtual Network Editor</h3><p>In VMware Workstation:</p><ol><li>Open <strong>Edit</strong>.</li><li>Select <strong>Virtual Network Editor</strong>.</li><li>Click <strong>Change Settings</strong> if administrator permissions are required.</li><li>Add a new virtual network.</li></ol><p>Select an unused network, such as VMnet2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hCAdpF-3vnRpge2YcArFgg.png" /></figure><h3>Step 2: Configure VMnet2</h3><p>Set the following options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2z3XXiV82jqoIlkIvs9l4g.png" /></figure><p><strong>What does Host-only mean?</strong></p><p>A Host-only network creates a private virtual network between the host computer and the virtual machines connected to it.</p><p>Virtual machines connected to this network can communicate with each other, but the network does not automatically provide internet access.</p><p>This makes it useful for isolated experiments.</p><p>We also need to uncheck <strong>Use local DHCP service to distribute IP addresses to VMs</strong>.</p><p>This disables VMware’s DHCP service on VMnet2. Our Ubuntu machine will handle DHCP requests instead.</p><p>Click Apply and OK to save the configuration.</p><p>VMware’s host adapter for VMnet2 usually takes 192.168.10.1. That’s why we’ll give the DHCP server 192.168.10.2.</p><h3>Step 3: Connect the Ubuntu VM to VMnet2</h3><p>Now open the settings of our Ubuntu virtual machine.</p><p>Select its Network Adapter and change the network connection from NAT to:</p><p><strong>Custom: Specific virtual network → VMnet2</strong></p><p>Make sure the network adapter is connected.</p><p>Start the VM if it isn’t already running.</p><p>Our Ubuntu machine is now connected to the isolated lab network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H50uChnRr1vm42CJmOP-RA.png" /></figure><h3>4. Assigning a static IP address to the DHCP server</h3><p>Now let’s configure the network interface inside Ubuntu.</p><p>Open the terminal and run:</p><pre>ip -br addr</pre><p>This command displays the network interfaces and their IP addresses in a compact format.</p><p>We should see something similar to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*j9cxyf3CopqXISE1fAbFtQ.png" /></figure><p>Let’s understand this output:</p><ul><li>lo is the loopback interface. It allows a computer to communicate with itself.</li><li>ens33 is the network interface connected to our VMware virtual network.</li><li>UP means the interface is enabled and has a link.</li></ul><p>Notice that ens33 doesn&#39;t have an IPv4 address yet.</p><p>We need to assign a static IP address to our DHCP server.</p><h3>Why does the DHCP server need a static IP?</h3><p>Other devices need to communicate with the DHCP server. Its address should therefore remain predictable.</p><p>If the server itself received a different IP address every time it connected, clients and other network services could have difficulty locating it.</p><p>For this lab, we’ll use:</p><pre>192.168.10.2/24</pre><p>Here:</p><ul><li>192.168.10.2 is the server&#39;s IP address.</li><li>/24 represents the subnet mask 255.255.255.0.</li></ul><h3>Step 1: Find the Netplan configuration file</h3><p>Ubuntu uses Netplan to manage network configuration.</p><p>First, list the available configuration files:</p><pre>ls /etc/netplan/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*msuKjfWOkRns7QVI6tuI4Q.png" /></figure><p>Open the relevant configuration file. For example:</p><pre>sudo nano /etc/netplan/00-installer-config.yaml</pre><p>The filename may differ on your system. Use the actual filename shown by the previous command.</p><h3>Step 2: Configure the static address</h3><p>Under the ens33 interface, configure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WJQmULp6E4s6v-7QuUY0yw.png" /></figure><p>Make sure the indentation matches the rest of your YAML file.</p><p>Let’s understand the configuration:</p><ul><li>dhcp4: false disables automatic IPv4 configuration.</li><li>dhcp6: false disables automatic IPv6 configuration.</li><li>addresses specifies the static IP address.</li></ul><p>Save the file and exit Nano.</p><h3>Step 3: Apply the configuration</h3><p>Run:</p><pre>sudo netplan try</pre><p>Netplan will test the configuration and ask you to confirm it.</p><p>If everything looks correct, confirm the changes.</p><p>You can also apply the configuration using:</p><pre>sudo netplan apply</pre><p>Now verify the result:</p><pre>ip -br addr</pre><p>We should see something similar to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EF6CC48DRGMIok1q2BeCTw.png" /></figure><p>Great! Our DHCP server now has a static IP address on the lab network.</p><h3>5. Installing ISC DHCP Server</h3><p>Now that our network interface is configured, let’s install the DHCP server software.</p><p>We’ll use ISC DHCP Server, a software implementation of the DHCP protocol. ISC ended maintenance of this project in 2022 in favor of Kea, but it’s still widely used and works well for learning.</p><p>Normally, we would install it using:</p><pre>sudo apt update<br>sudo apt install isc-dhcp-server -y</pre><p>But there’s a small problem.</p><p>Remember when we changed the network adapter from NAT to Host-only?</p><p>We disconnected the virtual machine from the NAT network. Since our Host-only network doesn’t provide internet access, Ubuntu can no longer download packages.</p><p>So, how do we solve this?</p><h3>Adding a second network adapter</h3><p>Instead of replacing our isolated network, we’ll add another network adapter.</p><p>Shut down the Ubuntu VM and open its VMware settings.</p><p>Add a second Network Adapter and set its connection type to <strong>NAT</strong>.</p><p>Our DHCP server will now have two network interfaces:</p><ul><li>ens33 for VMnet2 (Host-only) to communicate with DHCP clients</li><li>ens37 for NAT to access the internet</li></ul><p>This is a useful setup because it allows our server to remain connected to the isolated lab network while also accessing the internet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IVNEkFBPlnVNQOPwfSQFOw.png" /></figure><p>Start Ubuntu again and inspect the interfaces:</p><pre>ip -br addr</pre><p>We should now see both ens33 and ens37.</p><p>The new interface may initially be down or may not have an IP address.</p><h3>Configuring the second interface</h3><p>Ubuntu also needs to obtain an IP address on the NAT interface.</p><p>Open the relevant Netplan configuration file:</p><pre>sudo nano /etc/netplan/00-installer-config.yaml</pre><p>Keep the existing static configuration for ens33 and add the following configuration for ens37:</p><pre>ens37:<br>  dhcp4: true<br>  dhcp6: false</pre><p>The relevant portion of the complete configuration should now look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VOlxSEHIZC844k6ChP3v-A.png" /></figure><p>Save the file and apply the configuration:</p><pre>sudo netplan try</pre><p>Confirm the changes if everything looks correct.</p><p>Now run:</p><pre>ip -br addr</pre><p>Our NAT interface should now have an IP address assigned automatically.</p><p>Let’s test internet connectivity:</p><pre>ping -c 4 8.8.8.8</pre><p>If we receive replies, our virtual machine has internet connectivity. Also test DNS, since apt depends on it:<br> ping -c 4 ubuntu.com</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_-da6oxdnJiQCdoD081gVg.png" /></figure><p>We can now install ISC DHCP Server:</p><pre>sudo apt update<br>sudo apt install isc-dhcp-server -y</pre><p>Wait for the installation to finish.</p><h3>6. Configuring the DHCP server</h3><p>Installing the package is only the first step. We still need to configure which network interface the DHCP server should listen on and which IP addresses it can distribute.</p><h3>Step 1: Select the network interface</h3><p>Open the ISC DHCP Server default configuration file:</p><pre>sudo nano /etc/default/isc-dhcp-server</pre><p>Find the following setting:</p><pre>INTERFACESv4=&quot;&quot;</pre><p>Change it to:</p><pre>INTERFACESv4=&quot;ens33&quot;</pre><p>Why ens33?</p><p>Our server has two network interfaces:</p><ul><li>ens33 is connected to the isolated DHCP lab network.</li><li>ens37 is connected to the NAT network.</li></ul><p>We want our DHCP server to listen only on the isolated network, not the NAT network.</p><p>This is important because we don’t want our lab DHCP server distributing addresses on the wrong network.</p><p>Save the file and exit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PvK86Dme7eMgE_2QBUF6bw.png" /></figure><h3>Step 2: Configure the DHCP address pool</h3><p>Now let’s configure the IP address range that our server will distribute.</p><p>Open the main DHCP configuration file:</p><pre>sudo nano /etc/dhcp/dhcpd.conf</pre><p>Add the following configuration:</p><pre>default-lease-time 86400;<br>max-lease-time 86400;</pre><pre>subnet 192.168.10.0 netmask 255.255.255.0 {<br>    range 192.168.10.100 192.168.10.200;<br>}</pre><p>Let’s understand each part.</p><p><strong>Default lease time</strong></p><pre>default-lease-time 86400;</pre><p>A lease is the period for which a client is allowed to use an assigned IP address.</p><p>Here, 86400 seconds equals 24 hours.</p><p><strong>Maximum lease time</strong></p><pre>max-lease-time 86400;</pre><p>This specifies the maximum lease duration that the server will allow.</p><p><strong>Subnet declaration</strong></p><pre>subnet 192.168.10.0 netmask 255.255.255.0 {</pre><p>This tells the DHCP server that the following configuration applies to the 192.168.10.0/24 network.</p><p><strong>Address range</strong></p><pre>range 192.168.10.100 192.168.10.200;</pre><p>This defines the range of IP addresses available for DHCP clients.</p><p>For example:</p><ul><li>First client: 192.168.10.100</li><li>Second client: 192.168.10.101</li><li>Third client: 192.168.10.102</li></ul><p>The server will select an available address from this pool when a client requests one.</p><p>Notice that our DHCP server uses 192.168.10.2, while the address pool starts at .100. This prevents the server&#39;s own static IP address from being assigned to another client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mukazqk9P5dviGxTI48phg.png" /></figure><h3>Step 3: Validate the configuration</h3><p>Before starting the DHCP service, it’s a good idea to check whether our configuration contains any syntax errors.</p><p>Run:</p><pre>sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf</pre><p>Here:</p><ul><li>dhcpd runs the DHCP server program.</li><li>-t checks the configuration without starting the server.</li><li>-cf specifies the configuration file to check.</li></ul><p>If the configuration is valid, we can proceed.</p><h3>Step 4: Start the DHCP service</h3><p>Restart the DHCP server:</p><pre>sudo systemctl restart isc-dhcp-server</pre><p>Now check its status:</p><pre>sudo systemctl status isc-dhcp-server</pre><p>We want to see:</p><pre>Active: active (running)</pre><p>This means our DHCP service is running.</p><p>If the service fails, check the error messages in the status output and verify the interface name and configuration file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bx8TJnmY2RdLr4z0HBvynQ.png" /></figure><p>Our DHCP server is now ready to receive requests!</p><h3>7. Connecting the DHCP client</h3><p>Now comes the interesting part: testing whether our server actually works.</p><p>We’ll create another Ubuntu virtual machine to act as our DHCP client.</p><p>Install Ubuntu on the second VM using the same process as before.</p><p>Once installed, open its VMware settings.</p><p>Change its Network Adapter to:</p><p><strong>Custom: Specific virtual network → VMnet2</strong></p><p>Make sure the adapter is connected.</p><p>The client and server must be connected to the same Host-only network.</p><h3>Step 1: Inspect the client network interface</h3><p>Start the second Ubuntu VM and open its terminal.</p><p>Run:</p><pre>ip -br addr</pre><p>We should see something similar to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*QNTHJxDmrbhoXBI1LN42sQ.png" /></figure><p>The ens33 interface should be configured to obtain an IPv4 address automatically.</p><p>Ubuntu Desktop normally manages this through NetworkManager.</p><h3>Step 2: Check the assigned IP address</h3><p>Run:</p><pre>ip -4 addr show ens33</pre><p>In my case, the output included:</p><pre>inet 192.168.10.100/24</pre><p>There it is!</p><p>Our DHCP client has received 192.168.10.100, which is the first address in the DHCP pool we configured earlier.</p><p>We can also check the routing table:</p><pre>ip route</pre><p>Since our isolated lab network doesn’t have a router or default gateway configured, we should see the directly connected subnet route.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*AHrQFVZ80FaFWbmbcjokaQ.png" /></figure><p>Our DHCP server has successfully assigned an IP address to another virtual machine!</p><h3>8. Inspecting the DHCP lease database</h3><p>The DHCP server also maintains a record of the addresses it has assigned.</p><p>On the server VM, run:</p><pre>sudo cat /var/lib/dhcp/dhcpd.leases</pre><p>This file stores information about DHCP leases, including:</p><ul><li>The leased IP address.</li><li>The client’s hardware address or identifier.</li><li>Lease start and end times.</li><li>Lease state.</li></ul><p>Since we’ve connected only one client, we should find an entry for:</p><pre>192.168.10.100</pre><p>This confirms that our DHCP server has recorded the client’s lease.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nXgvIFgVrCKlw3CvGTZ9Tw.png" /></figure><h3>9. Observing DHCP packets using tcpdump</h3><p>So far, we’ve verified that the client received an IP address and that the server recorded its lease.</p><p>But what actually happens behind the scenes when a device requests an IP address?</p><p>We can observe the network traffic using tcpdump.</p><p>tcpdump is a command-line packet analyzer that allows us to capture and inspect packets travelling through a network interface.</p><h3>Step 1: Install tcpdump</h3><p>On the DHCP server, run:</p><pre>sudo apt install tcpdump -y</pre><h3>Step 2: Start capturing DHCP traffic</h3><p>Run:</p><pre>sudo tcpdump -i ens33 -n -vvv &#39;udp port 67 or udp port 68&#39;</pre><p>Let’s break this command down:</p><ul><li>tcpdump: Starts packet capture.</li><li>-i ens33: Captures traffic on our DHCP lab interface.</li><li>-n: Prevents IP addresses and ports from being converted into hostnames and service names.</li><li>-vvv: Displays more detailed packet information.</li><li>udp port 67 or udp port 68: Filters traffic to DHCP-related UDP ports.</li></ul><p>DHCP uses UDP port 67 on the server side and UDP port 68 on the client side.</p><p>Leave this terminal running.</p><h3>Step 3: Understand the DHCP process</h3><p>When a new client joins a network and needs an IP address, DHCP normally follows four steps known as <strong>DORA</strong>.</p><p><strong>1. Discover</strong></p><p>The client broadcasts a DHCP Discover message to find available DHCP servers.</p><p>Essentially, it is asking:</p><p>“Is there a DHCP server on this network?”</p><p><strong>2. Offer</strong></p><p>The DHCP server responds with a DHCP Offer message containing an available IP address.</p><p>For example:</p><p>“Here is an available address: 192.168.10.100.”</p><p><strong>3. Request</strong></p><p>The client sends a DHCP Request message indicating that it wants to use the offered address.</p><p><strong>4. Acknowledgment (ACK)</strong></p><p>The server responds with a DHCP ACK message, confirming the lease.</p><p>The client can now configure its network interface using the assigned IP address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/0*LzuPEy9GNpThRkj3" /></figure><h3>Step 4: Inspect the captured packets</h3><p>During my first capture, I only saw DHCP Request and DHCP ACK packets. The client already had a valid lease, so it renewed its address instead of running the full DORA process.</p><p>To see all four steps, keep tcpdump running on the server, then on the client run:<br> sudo nmcli device disconnect ens33 &amp;&amp; sudo nmcli device connect ens33</p><p>The client has to request a fresh lease, so you should typically see Discover, Offer, Request, and ACK.</p><p>The client already had a valid lease, so when it reconnected, it reused or renewed its existing address rather than necessarily performing the complete four-step DORA process again.</p><p>The capture showed information such as:</p><ul><li>Client requesting 192.168.10.100.</li><li>DHCP server responding from 192.168.10.2.</li><li>Server confirming the lease through DHCP ACK.</li><li>Subnet mask 255.255.255.0.</li></ul><p>This is useful because we aren’t just seeing the final assigned IP address. We’re observing the actual communication between the DHCP client and server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUcp-lB3F2nTVzSDJOa0AQ.png" /></figure><p>And that’s another confirmation that our DHCP server is working!</p><h3>10. <strong>Troubleshooting</strong></h3><ul><li>Service won’t start: run sudo journalctl -u isc-dhcp-server -e and check for typos in dhcpd.conf.</li><li>“No subnet declaration for ens33”: the server’s static IP isn’t in the 192.168.10.0/24 subnet, so recheck Netplan.</li><li>Client gets a 169.254.x.x address: the client isn’t reaching the server, so confirm both VMs are on VMnet2 and the service is running.</li><li>If ufw is enabled on the server, allow DHCP: sudo ufw allow 67/udp.</li></ul><p>To recap, we built an isolated VMware network, gave our server a static IP, installed and configured ISC DHCP Server, and verified the whole process with a client, the lease file, and a packet capture. From here, you could add a gateway and DNS options, set up static reservations for specific devices, or try Kea, the modern replacement for ISC DHCP.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6533b2f5453" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Terminal Chat Application Using TCP Sockets in Node.js]]></title>
            <link>https://medium.com/@shrutimaharjan2005/building-a-terminal-chat-application-using-tcp-sockets-in-node-js-736b82586294?source=rss-35d766d69946------2</link>
            <guid isPermaLink="false">https://medium.com/p/736b82586294</guid>
            <category><![CDATA[clients]]></category>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[chat-apps]]></category>
            <dc:creator><![CDATA[Shruti Maharjan]]></dc:creator>
            <pubDate>Tue, 14 Jul 2026 17:35:05 GMT</pubDate>
            <atom:updated>2026-07-14T17:35:05.669Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we’ll build a simple terminal-based chat application using <strong>TCP sockets</strong> and Node.js. Instead of using libraries like Socket.IO, we’ll use Node’s built-in <strong>net</strong> module to understand how client-server communication works under the hood.</p><h3>What is TCP?</h3><p><strong>TCP (Transmission Control Protocol)</strong> is a connection-oriented protocol that allows two devices to exchange data reliably over a network. Before data is sent, a connection is established between the client and the server.</p><p>In this project, we’ll build a chat application where multiple terminal clients can communicate with each other through a TCP server.</p><h3>Project Setup</h3><p>Create a new folder and add two files:</p><pre>client.js<br>server.js</pre><p>Initialize a Node.js project:</p><pre>npm init -y</pre><p>No external packages are required for this project since we’ll only use Node’s built-in modules.</p><h3>Creating the Server</h3><p>Import the built-in net module.</p><pre>const net = require(&quot;net&quot;);</pre><p>Create a TCP server.</p><pre>const server = net.createServer();</pre><p>At this point, the server exists but isn’t listening for any connections yet.</p><p>Start the server on port <strong>3000</strong>.</p><pre>server.listen(3000, () =&gt; {<br>    console.log(&quot;Server is listening on port 3000&quot;);<br>});</pre><p>A <strong>port</strong> is used to identify different applications running on the same machine. Here, our chat server listens on port 3000.</p><h3>Handling Client Connections</h3><p>Whenever a client connects, Node creates a new <strong>socket</strong> for that client.</p><p>A socket represents the connection between the client and the server. It keeps track of information such as the connection status and is used to send and receive data.</p><pre>const net = require(&quot;net&quot;);</pre><pre>const server = net.createServer((socket) =&gt; {<br>    console.log(&quot;Client connected&quot;);<br>});</pre><pre>server.listen(3000, () =&gt; {<br>    console.log(&quot;Server is listening on port 3000&quot;);<br>});</pre><h3>Creating the Client</h3><p>Now let’s connect to our server.</p><pre>const net = require(&quot;net&quot;);</pre><pre>const client = net.createConnection({<br>    host: &quot;localhost&quot;,<br>    port: 3000,<br>});</pre><p>localhost refers to your own computer, and 3000 is the port where our server is listening.</p><p>Run the server first:</p><pre>node server.js</pre><p>Then open another terminal and run:</p><pre>node client.js</pre><p>You won’t see anything in the client terminal yet, but the server should print:</p><pre>Client connected</pre><h3>Listening for Events</h3><p>Let’s know when the client successfully connects.</p><pre>client.on(&quot;connect&quot;, () =&gt; {<br>    console.log(&quot;Connected to the server!&quot;);<br>});</pre><p>on() registers an <strong>event listener</strong>. Whenever the &quot;connect&quot; event occurs, the callback function is executed.</p><p>Similarly, we can listen for incoming data.</p><pre>client.on(&quot;data&quot;, (data) =&gt; {<br>    console.log(data.toString());<br>});</pre><p>Notice the use of toString(). Data received over TCP arrives as a <strong>Buffer</strong>, so we convert it into a readable string.</p><h3>Sending Data from the Server</h3><p>Let’s send a welcome message whenever a client connects.</p><pre>const net = require(&quot;net&quot;);</pre><pre>const server = net.createServer((socket) =&gt; {<br>    console.log(&quot;Client connected&quot;);</pre><pre>    socket.write(&quot;Hello from server!\n&quot;);<br>});</pre><pre>server.listen(3000, () =&gt; {<br>    console.log(&quot;Server is listening on port 3000&quot;);<br>});</pre><p>Since the client is listening for the &quot;data&quot; event, this message will automatically appear in the client terminal.</p><h3>Making the Chat Interactive</h3><p>We now need a way for users to type messages in the terminal.</p><p>Node provides another built-in module called <strong>readline</strong>.</p><pre>const readline = require(&quot;readline&quot;);</pre><p>Create a readline interface.</p><pre>const rl = readline.createInterface({<br>    input: process.stdin,<br>    output: process.stdout,<br>});</pre><p>process.stdin represents the keyboard input, while process.stdout represents the terminal output.</p><p>Read user input.</p><pre>rl.on(&quot;line&quot;, (input) =&gt; {<br>    console.log(input);<br>});</pre><p>Every time the user presses <strong>Enter</strong>, the &quot;line&quot; event is triggered.</p><p>Instead of printing the input, send it to the server.</p><pre>rl.on(&quot;line&quot;, (input) =&gt; {<br>    client.write(input);<br>});</pre><h3>Receiving Messages on the Server</h3><p>The server also needs to listen for incoming messages.</p><pre>socket.on(&quot;data&quot;, (data) =&gt; {<br>    console.log(`Received: ${data.toString()}`);<br>});</pre><p>Now anything typed in the client terminal appears in the server terminal.</p><h3>Supporting Multiple Clients</h3><p>To broadcast messages, the server needs to remember every connected client.</p><p>Create an array.</p><pre>const clients = [];</pre><p>Whenever a client connects:</p><pre>clients.push(socket);</pre><p>Now the server knows every connected client.</p><p>Broadcast messages to all clients.</p><pre>socket.on(&quot;data&quot;, (data) =&gt; {<br>    const message = data.toString();</pre><pre>    clients.forEach((client) =&gt; {<br>        client.write(message);<br>    });<br>});</pre><p>Now if one client sends a message, every connected client receives it.</p><h3>Prevent Sending Messages Back to the Sender</h3><p>Currently, the sender also receives their own message.</p><p>We can avoid this with a simple condition.</p><pre>clients.forEach((client) =&gt; {<br>    if (client !== socket) {<br>        client.write(message);<br>    }<br>});</pre><h3>Handling Client Disconnections</h3><p>When a client disconnects, we should remove it from our array.</p><pre>socket.on(&quot;close&quot;, () =&gt; {<br>    const index = clients.indexOf(socket);</pre><pre>    if (index !== -1) {<br>        clients.splice(index, 1);<br>    }<br>});</pre><p>It’s also good practice to handle socket errors.</p><pre>socket.on(&quot;error&quot;, (err) =&gt; {<br>    console.log(&quot;Socket error:&quot;, err.code);<br>});</pre><h3>Adding Usernames</h3><p>Instead of anonymous messages, let’s allow each client to choose a username.</p><p>When the client connects:</p><pre>rl.question(&quot;Enter your username: &quot;, (username) =&gt; {<br>    client.write(`USERNAME:${username}`);<br>});</pre><p>On the server:</p><pre>if (message.startsWith(&quot;USERNAME:&quot;)) {<br>    socket.username = message.substring(9);</pre><pre>    console.log(`${socket.username} joined the chat`);</pre><pre>    return;<br>}</pre><p>The username is stored as a property on the client’s socket.</p><p>Now when broadcasting messages:</p><pre>client.write(`${socket.username}: ${message}`);</pre><p>Messages become much more readable.</p><pre>Shruti: Hello!<br>Bob: Hi!</pre><h3>Join and Leave Notifications</h3><p>When someone disconnects:</p><pre>socket.on(&quot;close&quot;, () =&gt; {<br>    console.log(`${socket.username} left the chat`);</pre><pre>    clients.forEach((client) =&gt; {<br>        if (client !== socket) {<br>            client.write(`${socket.username} left the chat\n`);<br>        }<br>    });</pre><pre>    const index = clients.indexOf(socket);</pre><pre>    if (index !== -1) {<br>        clients.splice(index, 1);<br>    }<br>});</pre><p>On the client:</p><pre>client.on(&quot;close&quot;, () =&gt; {<br>    console.log(&quot;Disconnected from server&quot;);<br>    process.exit(0);<br>});</pre><h3>Final Thoughts</h3><p>This project demonstrates the basics of TCP socket programming in Node.js without relying on higher-level libraries.</p><p>While libraries like Socket.IO make real-time communication much easier, building a chat application using the net module helped me better understand:</p><ul><li>TCP connections</li><li>Client-server architecture</li><li>Event-driven programming</li><li>Socket communication</li><li>Managing multiple clients</li><li>Broadcasting messages</li><li>Connection lifecycle</li></ul><p>You can find the complete source code here:</p><p><a href="https://github.com/shruti-1007/tcp_terminal_chat"><strong>https://github.com/shruti-1007/tcp_terminal_chat</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=736b82586294" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Mini Message Board with Express and EJS | The Odin Project]]></title>
            <link>https://medium.com/@shrutimaharjan2005/building-a-mini-message-board-with-express-and-ejs-the-odin-project-e86727048d4e?source=rss-35d766d69946------2</link>
            <guid isPermaLink="false">https://medium.com/p/e86727048d4e</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[node-js-tutorial]]></category>
            <dc:creator><![CDATA[Shruti Maharjan]]></dc:creator>
            <pubDate>Tue, 30 Jun 2026 17:53:23 GMT</pubDate>
            <atom:updated>2026-06-30T18:07:01.206Z</atom:updated>
            <content:encoded><![CDATA[<p>As part of the Mini Message Board project from The Odin Project, I built a simple web application to become familiar with Node.js, Express, routing, EJS templates, forms, and middleware.</p><h3>Initializing the Project</h3><p>First, initialize a Node.js project:</p><pre>npm init -y</pre><p>This creates a package.json file, which stores information about the project such as dependencies, scripts, project name, and version.</p><h3>Installing Express</h3><p>Next, install Express:</p><pre>npm install express</pre><p>Express is a framework for building web servers and APIs in Node.js. It simplifies many common backend tasks such as:</p><ul><li>Routing</li><li>Middleware</li><li>Request handling</li><li>Response handling</li></ul><p>Without Express, all of this functionality would need to be implemented manually using Node’s built-in HTTP module.</p><p>After running the command, two new things appear in the project:</p><ul><li>node_modules</li><li>package-lock.json</li></ul><p>The node_modules folder contains all installed packages. Although we only installed Express, many additional packages are also installed because Express depends on them internally.</p><p>The package-lock.json file stores the exact versions of all installed packages to ensure that the project behaves consistently across different machines.</p><h3>Installing EJS</h3><p>Next, install EJS:</p><pre>npm install ejs</pre><p>EJS (Embedded JavaScript) is a template engine for Express that allows JavaScript code to be embedded inside HTML files, making it possible to create dynamic webpages.</p><h3>Creating the Server</h3><p>Create an app.js file and add the following code:</p><pre>const express = require(&quot;express&quot;);<br>const app = express();<br>app.listen(3000, () =&gt; {<br>    console.log(&quot;Server running on port 3000&quot;);<br>});</pre><p>Here:</p><ul><li>express() creates the server instance.</li><li>app.listen() starts the server on port 3000.</li></ul><p>Run the server using:</p><pre>node app.js</pre><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*V1Ttl94K9UfIecm5iHNuxg.png" /></figure><h3>Adding Middleware</h3><p>To process form submissions, we need middleware that can parse incoming form data.</p><pre>app.use(express.urlencoded({ extended: true }));</pre><p>This middleware converts form data into a JavaScript object that can be accessed using req.body.</p><h3>Configuring EJS</h3><p>Create a views folder and configure Express to use it:</p><pre>app.set(&quot;views&quot;, path.join(__dirname, &quot;views&quot;));<br>app.set(&quot;view engine&quot;, &quot;ejs&quot;);</pre><p>These lines tell Express:</p><ul><li>where to find view templates</li><li>which template engine to use</li></ul><h3>Using Routers</h3><p>To keep the application organized, routes are moved into a separate router file.</p><pre>const indexRouter = require(&quot;./routes/indexRouter&quot;);<br>app.use(&quot;/&quot;, indexRouter);</pre><p>This tells Express:</p><blockquote><em>Whenever a request starts with </em><em>/, pass control to </em><em>indexRouter.</em></blockquote><h3>Final app.js</h3><pre>const express = require(&quot;express&quot;);<br>const path = require(&quot;path&quot;);<br>const app = express();<br>app.set(&quot;views&quot;, path.join(__dirname, &quot;views&quot;));<br>app.set(&quot;view engine&quot;, &quot;ejs&quot;);<br>app.use(express.urlencoded({ extended: true }));<br>const indexRouter = require(&quot;./routes/indexRouter&quot;);<br>app.use(&quot;/&quot;, indexRouter);<br>app.listen(3000, () =&gt; {<br>    console.log(&quot;Server running on port 3000&quot;);<br>});</pre><p>The project structure now looks like:</p><pre>mini-message-board/<br>│<br>├── app.js<br>├── package.json<br>├── package-lock.json<br>├── node_modules/<br>│<br>├── routes/<br>│   └── indexRouter.js<br>│<br>└── views/<br>    ├── index.ejs<br>    ├── form.ejs<br>    └── message.ejs</pre><h3>Creating the Router</h3><p>Inside routes/indexRouter.js:</p><pre>const { Router } = require(&quot;express&quot;);<br>const indexRouter = Router();<br>module.exports = indexRouter;</pre><p>Using routers keeps app.js small and organized as the application grows.</p><h3>Creating a Temporary Database</h3><p>For this project, messages are stored in an array:</p><pre>const messages = [<br>  {<br>    text: &quot;Hi there!&quot;,<br>    user: &quot;Amando&quot;,<br>    added: new Date()<br>  },<br>  {<br>    text: &quot;Hello World!&quot;,<br>    user: &quot;Charles&quot;,<br>    added: new Date()<br>  }<br>];</pre><p>This acts as a temporary in-memory database.</p><h3>Creating the Homepage Route</h3><pre>indexRouter.get(&quot;/&quot;, (req, res) =&gt; {<br>    res.render(&quot;index&quot;, {<br>        title: &quot;Mini Message Board&quot;,<br>        messages: messages<br>    });<br>});</pre><p>Whenever a GET request is made to /, Express renders the index.ejs template and passes the title and messages to it.</p><h3>Creating index.ejs</h3><pre>&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;<br>&lt;a href=&quot;/new&quot;&gt;Create Message&lt;/a&gt;<br>&lt;% messages.forEach((message, index) =&gt; { %&gt;<br>    &lt;h2&gt;&lt;%= message.user %&gt;&lt;/h2&gt;<br>    &lt;p&gt;&lt;%= message.text %&gt;&lt;/p&gt;<br>    &lt;small&gt;&lt;%= message.added %&gt;&lt;/small&gt;<br>    &lt;br&gt;<br>    &lt;a href=&quot;/message/&lt;%= index %&gt;&quot;&gt;Open&lt;/a&gt;<br>    &lt;hr&gt;<br>&lt;% }) %&gt;</pre><p>The EJS template loops through every message and displays its contents.</p><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*cmj76MMZgVv2HeuSdP8VFg.png" /><figcaption>Output</figcaption></figure><h3>Creating the Form Page</h3><p>Add a route for the form page:</p><pre>indexRouter.get(&quot;/new&quot;, (req, res) =&gt; {<br>    res.render(&quot;form&quot;);<br>});</pre><p>Create form.ejs:</p><pre>&lt;h1&gt;New Message&lt;/h1&gt;<br>&lt;form method=&quot;POST&quot; action=&quot;/new&quot;&gt;<br>    &lt;input<br>        type=&quot;text&quot;<br>        name=&quot;messageUser&quot;<br>        placeholder=&quot;Your Name&quot;<br>    &gt;<br>    &lt;br&gt;&lt;br&gt;<br>    &lt;input<br>        type=&quot;text&quot;<br>        name=&quot;messageText&quot;<br>        placeholder=&quot;Message&quot;<br>    &gt;<br>    &lt;br&gt;&lt;br&gt;<br>    &lt;button type=&quot;submit&quot;&gt;<br>        Submit<br>    &lt;/button&gt;<br>&lt;/form&gt;</pre><p>When the user submits the form, a POST request is sent to /new.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*I71y0b71Hh80ymNts48kJQ.png" /></figure><h3>Handling Form Submission</h3><pre>indexRouter.post(&quot;/new&quot;, (req, res) =&gt; {<br>    const messageUser = req.body.messageUser;<br>    const messageText = req.body.messageText;<br><br><br>messages.push({<br>        text: messageText,<br>        user: messageUser,<br>        added: new Date()<br>    });<br>    res.redirect(&quot;/&quot;);<br>});</pre><p>This route:</p><ol><li>Reads the submitted form data.</li><li>Adds it to the messages array.</li><li>Redirects the user back to the homepage.</li></ol><h3>Creating a Message Details Page</h3><pre>indexRouter.get(&quot;/message/:id&quot;, (req, res) =&gt; {<br>    const id = req.params.id;<br><br><br>const message = messages[id];<br>    res.render(&quot;message&quot;, {<br>        message<br>    });<br>});</pre><p>The :id part is a route parameter that allows us to access individual messages.</p><h3>Creating message.ejs</h3><pre>&lt;h1&gt;Message Details&lt;/h1&gt;<br>&lt;h2&gt;&lt;%= message.user %&gt;&lt;/h2&gt;<br>&lt;p&gt;&lt;%= message.text %&gt;&lt;/p&gt;<br>&lt;p&gt;&lt;%= message.added %&gt;&lt;/p&gt;<br>&lt;a href=&quot;/&quot;&gt;Back&lt;/a&gt;</pre><p>Now every message has its own details page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*aCJ-UfUQicnZlDatlCDzIg.png" /><figcaption>Message Details Page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*gbFldbJ17KfqiHVugR0BjQ.png" /><figcaption>Main Page</figcaption></figure><h3>Conclusion</h3><p>This small project introduced several important backend concepts:</p><ul><li>Express servers</li><li>Routing</li><li>Middleware</li><li>EJS templates</li><li>Forms</li><li>POST requests</li><li>Route parameters</li><li>Redirects</li></ul><p>Although the project stores data in an array, the same concepts will later be used with real databases such as PostgreSQL or MongoDB.</p><p>For a first Express application, this project provides an excellent introduction to server-side web development with Node.js.</p><p>Github Link: <a href="https://github.com/shruti-1-0-1/backend/tree/main/mini_msg_board">https://github.com/shruti-1-0-1/backend/tree/main/mini_msg_board</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e86727048d4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>