<?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 Miyuru Tharuka on Medium]]></title>
        <description><![CDATA[Stories by Miyuru Tharuka on Medium]]></description>
        <link>https://medium.com/@miyurut20?source=rss-20f5cc3224f2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dLu3HjPbVTy0uFLlmgqKRA.jpeg</url>
            <title>Stories by Miyuru Tharuka on Medium</title>
            <link>https://medium.com/@miyurut20?source=rss-20f5cc3224f2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 19:35:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@miyurut20/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[Socket Programming]]></title>
            <link>https://medium.com/@miyurut20/socket-programming-f6b9cd0d6cb5?source=rss-20f5cc3224f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6b9cd0d6cb5</guid>
            <category><![CDATA[tcp-vs-udp]]></category>
            <category><![CDATA[sockets]]></category>
            <dc:creator><![CDATA[Miyuru Tharuka]]></dc:creator>
            <pubDate>Wed, 18 Feb 2026 03:20:41 GMT</pubDate>
            <atom:updated>2026-02-18T03:20:41.057Z</atom:updated>
            <content:encoded><![CDATA[<p>When computers talk to each other over a network, they use <strong>sockets</strong>.</p><h3><strong>What Is a Socket?</strong></h3><p>A <strong>socket</strong> is the interface between an application and the network.</p><p>It allows an application to:</p><ul><li>Send data to another host</li><li>Receive data from another host</li><li>Choose the communication style (reliable vs unreliable, connection-oriented vs connectionless)</li></ul><p>Each machine has:</p><ul><li>An <strong>IP address</strong> → identifies the host</li><li><strong>Ports (0–65535)</strong> → identify applications/processes</li><li>A <strong>socket</strong> → binds IP + Port + protocol</li></ul><p>Think of:</p><ul><li>IP address → Apartment building</li><li>Port → Mailbox</li><li>Application → Person receiving mail</li><li>Network → Postal service</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C8LgzFby3JPVOSbaSZeZbQ.png" /><figcaption>How Network Socket Work</figcaption></figure><h3><strong>TCP vs UDP — The Two Core Socket Types</strong></h3><p>TCP (Transmission Control Protocol)</p><ul><li>Reliable delivery</li><li>In-order transmission</li><li>Connection-oriented</li><li>Bidirectional communication</li></ul><p>Used in:</p><ul><li>Chat applications</li><li>File transfer (FTP)</li><li>Email (SMTP)</li><li>Web communication (HTTP runs on TCP)</li></ul><p><strong><em>Best when reliability matters.</em></strong></p><p>UDP (User Datagram Protocol)</p><ul><li>No guaranteed delivery</li><li>No order guarantee</li><li>No connection setup</li><li>Faster, lightweight</li></ul><p>Used in:</p><ul><li>Streaming</li><li>Broadcasting</li><li>Real-time systems</li></ul><p><em>Best when speed matters more than reliability.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CBYLd55J0E5uHiq5f_kwwQ.png" /><figcaption>TCP vs UDP</figcaption></figure><h3><strong>TCP Connection Lifecycle</strong></h3><p>In TCP:</p><p>Server (Passive Participant)</p><ol><li>Creates a ServerSocket</li><li>Listens on a port</li><li>Accepts connection requests</li><li>Creates a new socket per client</li></ol><p>Client (Active Participant)</p><ol><li>Creates a Socket</li><li>Connects to server</li><li>Exchanges data</li></ol><p>Important concept:</p><ul><li>The server keeps listening while handling multiple clients.</li><li>Multi-threading is required to prevent blocking.</li></ul><h3><strong>Blocking and Concurrency</strong></h3><p>Socket operations are <strong>blocking by default</strong>.</p><p>If a server waits on one client, others cannot connect.</p><p>Solution:</p><ul><li>Multi-threading</li><li>One thread per connection</li></ul><p>This concept is foundational in scalable backend systems.</p><h3><strong>Java Socket Programming</strong></h3><p>Java provides networking support via:</p><pre>java.net</pre><p><strong>Core classes:</strong></p><p>For TCP:</p><ul><li>Socket (client-side)</li><li>ServerSocket (server-side)</li></ul><p>For UDP:</p><ul><li>DatagramSocket</li><li>DatagramPacket</li></ul><p>For Address Resolution:</p><ul><li>InetAddress</li></ul><h3><strong>Socket I/O in Java</strong></h3><p>TCP sockets use:</p><ul><li>InputStream</li><li>OutputStream</li></ul><p>Data is sent as a <strong>stream of bytes</strong>.</p><p>Common operations:</p><ul><li>read()</li><li>write()</li><li>close()</li></ul><h3><strong>UDP Programming Model</strong></h3><p>Instead:</p><ul><li>Sender creates a DatagramPacket</li><li>Attaches destination IP + port</li><li>Sends via DatagramSocket</li><li>Receiver extracts data from received packet</li></ul><p>No handshake.<br>No connection.<br>No guarantees.</p><h3>TCP vs HTTP</h3><p>Important clarification:</p><ul><li><strong>TCP</strong> = Transport layer protocol</li><li><strong>HTTP</strong> = Application layer protocol (runs on TCP)</li></ul><p>All web communication ultimately relies on socket communication underneath.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0rSQ7rMV8o5qdKARBDdg0g.png" /><figcaption>TCP vs HTTP</figcaption></figure><h3>WebSockets</h3><p>WebSockets provide:</p><ul><li>Persistent, bidirectional communication</li><li>Built on top of TCP</li><li>Used in real-time applications</li></ul><p>Again — underneath, it’s still socket communication.</p><h3>Why This Matters</h3><p>Socket programming is:</p><ul><li>The most fundamental form of client-server communication</li><li>The backbone of distributed systems</li><li>The underlying mechanism behind:</li><li>REST APIs</li><li>Microservices</li><li>Web services</li><li>Cloud communication</li></ul><p>Even high-level frameworks ultimately depend on <strong>socket-level communication</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6b9cd0d6cb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>