Sitemap
Miyuru Tharuka

Miyuru Tharuka

Socket Programming

3 min readFeb 18, 2026

--

When computers talk to each other over a network, they use sockets.

What Is a Socket?

A socket is the interface between an application and the network.

It allows an application to:

  • Send data to another host
  • Receive data from another host
  • Choose the communication style (reliable vs unreliable, connection-oriented vs connectionless)

Each machine has:

  • An IP address → identifies the host
  • Ports (0–65535) → identify applications/processes
  • A socket → binds IP + Port + protocol

Think of:

  • IP address → Apartment building
  • Port → Mailbox
  • Application → Person receiving mail
  • Network → Postal service
Press enter or click to view image in full size
How Network Socket Work

TCP vs UDP — The Two Core Socket Types

TCP (Transmission Control Protocol)

  • Reliable delivery
  • In-order transmission
  • Connection-oriented
  • Bidirectional communication

Used in:

  • Chat applications
  • File transfer (FTP)
  • Email (SMTP)
  • Web communication (HTTP runs on TCP)

Best when reliability matters.

UDP (User Datagram Protocol)

  • No guaranteed delivery
  • No order guarantee
  • No connection setup
  • Faster, lightweight

Used in:

  • Streaming
  • Broadcasting
  • Real-time systems

Best when speed matters more than reliability.

Press enter or click to view image in full size
TCP vs UDP

TCP Connection Lifecycle

In TCP:

Server (Passive Participant)

  1. Creates a ServerSocket
  2. Listens on a port
  3. Accepts connection requests
  4. Creates a new socket per client

Client (Active Participant)

  1. Creates a Socket
  2. Connects to server
  3. Exchanges data

Important concept:

  • The server keeps listening while handling multiple clients.
  • Multi-threading is required to prevent blocking.

Blocking and Concurrency

Socket operations are blocking by default.

Get Miyuru Tharuka’s stories in your inbox

Join Medium for free to get updates from this writer.

If a server waits on one client, others cannot connect.

Solution:

  • Multi-threading
  • One thread per connection

This concept is foundational in scalable backend systems.

Java Socket Programming

Java provides networking support via:

java.net

Core classes:

For TCP:

  • Socket (client-side)
  • ServerSocket (server-side)

For UDP:

  • DatagramSocket
  • DatagramPacket

For Address Resolution:

  • InetAddress

Socket I/O in Java

TCP sockets use:

  • InputStream
  • OutputStream

Data is sent as a stream of bytes.

Common operations:

  • read()
  • write()
  • close()

UDP Programming Model

Instead:

  • Sender creates a DatagramPacket
  • Attaches destination IP + port
  • Sends via DatagramSocket
  • Receiver extracts data from received packet

No handshake.
No connection.
No guarantees.

TCP vs HTTP

Important clarification:

  • TCP = Transport layer protocol
  • HTTP = Application layer protocol (runs on TCP)

All web communication ultimately relies on socket communication underneath.

Press enter or click to view image in full size
TCP vs HTTP

WebSockets

WebSockets provide:

  • Persistent, bidirectional communication
  • Built on top of TCP
  • Used in real-time applications

Again — underneath, it’s still socket communication.

Why This Matters

Socket programming is:

  • The most fundamental form of client-server communication
  • The backbone of distributed systems
  • The underlying mechanism behind:
  • REST APIs
  • Microservices
  • Web services
  • Cloud communication

Even high-level frameworks ultimately depend on socket-level communication.

--

--