Sockets in Operating Systems

2
0

Introduction

Sockets are general communication endpoints used when processes need to exchange data in a flexible way. They can support communication on the same machine or across different machines on a network.

Earlier IPC mechanisms such as pipes, message queues, and shared memory mainly focus on local communication. Sockets extend the idea into client-server communication, which is why they are common in backend systems and network services.

What Is a Socket?

A socket is an endpoint for communication. One process uses a socket to send or receive data, and another process communicates through its own endpoint.

A socket can be local to one machine or network-based across machines. This makes sockets useful for both IPC and network programming.

Why Sockets Are Needed

Many real systems are built around client-server communication. A browser connects to a web server, a backend service connects to a database, and a local tool may communicate with a daemon running on the same machine.

Sockets are useful because they support:

  • Local communication: Processes on the same machine can talk through Unix domain sockets.

  • Network communication: Processes on different machines can communicate through TCP or UDP sockets.

  • Client-server design: One side can wait for requests while another side initiates communication.

  • Bidirectional data exchange: Once connected, both sides can usually send and receive data.

This makes sockets broader than simple one-way stream IPC.

Sockets and its Need

Sockets and its Need

Main Socket Types

The important socket types are Unix domain sockets, TCP sockets, and UDP sockets. They all use the socket idea, but their behavior is different.

Socket Type

Main Use

Key Behavior

Unix domain socket

Local IPC on the same machine

Uses socket API without network routing

TCP socket

Reliable network communication

Connection-oriented, ordered byte stream

UDP socket

Datagram-based network communication

Connectionless, no TCP-style reliability guarantee

A common mistake is thinking every socket means internet communication. Unix domain sockets are local IPC mechanisms, even though they still use socket-style operations.

TCP Server Flow

A TCP server prepares an endpoint and waits for clients to connect. It does not begin by talking to a specific client; it first sets up a listening socket.

The basic flow is:

  • socket(): Create a communication endpoint.

  • bind(): Attach the socket to an address and port.

  • listen(): Mark the socket as ready to accept incoming connections.

  • accept(): Accept a client connection and get a connected socket.

  • recv() or read(): Receive data from the client.

  • send() or write(): Send data back to the client.

  • close(): Release the socket when finished.

The listening socket waits for new connections. The connected socket returned by accept() is used for communication with a specific client.

TCP Server and Client Flow

TCP Server and Client Flow

TCP Client Flow

A TCP client creates its own socket and initiates a connection to the server. After the connection is established, both sides can exchange data.

The basic client flow is:

  • socket(): Create the client endpoint.

  • connect(): Initiate connection to the server.

  • send() or write(): Send data to the server.

  • recv() or read(): Receive data from the server.

  • close(): Close the endpoint when done.

This is the usual model behind many request-response services.

Unix Domain Sockets

A Unix domain socket is a socket for local communication on the same machine. It uses the socket API, but communication stays local instead of being routed over a network.

Unix domain sockets are useful for service-to-service communication on one host. Examples include local database connections, system services, Docker-style local daemon communication, proxies, and local agents.

Important points include:

  • Local IPC: Both endpoints are on the same machine.

  • No network routing: Communication does not need external network paths.

  • Socket-style interface: Programs can still use socket operations.

  • Local features: Some systems support credential passing or other local-only behavior.

Unix Domain Sockets

Unix Domain Sockets

When Sockets Fit Well

Sockets fit well when communication has a client-server shape or may cross a machine boundary. They are also useful when local services want a standard endpoint model.

Good fits include:

  • Web servers and clients

  • Backend services

  • Database clients and servers

  • Local daemons and tools

  • Service-to-service communication

  • Networked applications

For simple one-way local byte streams, a pipe may be easier. For very large local shared data, shared memory may be faster. Sockets are strongest when endpoint-based communication is the natural model.

Summary

A socket is a communication endpoint used to send and receive data. Sockets support local IPC through Unix domain sockets and network communication through TCP or UDP sockets.

TCP sockets provide reliable connection-oriented byte streams, while UDP sockets provide connectionless datagrams. Unix domain sockets are local IPC endpoints on the same machine, useful for daemons, databases, agents, and system services.

CS Core

Read Similar Blogs

Comments0