Socket Programming
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
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.
TCP Connection Lifecycle
In TCP:
Server (Passive Participant)
- Creates a
ServerSocket - Listens on a port
- Accepts connection requests
- Creates a new socket per client
Client (Active Participant)
- Creates a
Socket - Connects to server
- 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.netCore classes:
For TCP:
Socket(client-side)ServerSocket(server-side)
For UDP:
DatagramSocketDatagramPacket
For Address Resolution:
InetAddress
Socket I/O in Java
TCP sockets use:
InputStreamOutputStream
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.
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.





