Unix - UNIX Network Socket Programming
Computer Networking
UNIX network socket programming is the method used to create communication between computers over a network using software interfaces called sockets. A socket acts as an endpoint for sending and receiving data between systems. In UNIX systems, sockets are widely used to build network applications such as web servers, email servers, chat systems, and file transfer programs.
Socket programming provides a way for processes on different machines to exchange information. It enables one computer to request services from another and receive responses. This communication can happen over local networks, the internet, or even within the same machine.
What is a Socket
A socket is a software object that represents one end of a communication link between two processes. It can be compared to a communication channel through which data flows from one program to another.
In UNIX, sockets are treated similarly to files. This means they use file descriptors and can be read from or written to using standard system calls.
A socket consists of:
-
IP address of the host
-
Port number
-
Communication protocol
-
Transport type
Together, these identify a communication session.
Types of Sockets in UNIX
UNIX supports different types of sockets depending on the communication requirements.
Stream Socket
Stream sockets use the Transmission Control Protocol (TCP). They provide reliable communication.
Characteristics:
-
Connection-oriented
-
Data arrives in sequence
-
Error checking included
-
Suitable for long sessions
Used for:
-
Web browsing
-
Remote login
-
Email communication
-
Database connections
Datagram Socket
Datagram sockets use the User Datagram Protocol (UDP).
Characteristics:
-
Connectionless
-
Faster communication
-
No guarantee of delivery
-
Lightweight
Used for:
-
Live streaming
-
Online gaming
-
DNS requests
-
Voice calls
Raw Socket
Raw sockets allow direct access to lower-level protocols and packet headers. These are mainly used for network diagnostics and protocol development.
Socket Programming Architecture
Network socket programming follows the client-server model.
Server
The server waits for requests from clients and responds accordingly. It listens on a specific port and handles incoming connections.
Examples:
-
Web server
-
Mail server
-
FTP server
Client
The client initiates communication by connecting to the server.
Examples:
-
Browser
-
Email client
-
Terminal application
Working of Socket Communication
The socket communication process follows several stages.
Step 1: Socket Creation
The program creates a socket using the socket() system call.
This allocates communication resources in the kernel.
General syntax:
socket(domain, type, protocol)
Parameters:
-
Domain specifies communication family
-
Type specifies socket type
-
Protocol specifies transport protocol
Step 2: Binding
The server binds the socket to an address and port.
This tells the operating system which port the server will use.
System call:
bind()
The bind function associates the socket with a unique address.
Step 3: Listening
The server waits for incoming connection requests.
System call:
listen()
The system creates a queue of pending client requests.
Step 4: Accepting Connections
The server accepts incoming requests.
System call:
accept()
This creates a new socket dedicated to that client.
Step 5: Data Transfer
Both client and server exchange data.
Functions:
-
read()
-
write()
-
send()
-
recv()
These functions transfer messages between the endpoints.
Step 6: Closing Connection
After communication ends, the socket is closed.
Function:
close()
Resources are released back to the operating system.
Important Socket Domains
Socket domains define communication families.
AF_INET
Used for IPv4 internet communication.
AF_INET6
Used for IPv6 communication.
AF_UNIX
Used for local interprocess communication on the same machine.
AF_UNIX sockets do not use network hardware. They allow processes on the same host to communicate.
Important Socket Functions
UNIX provides many system calls for socket programming.
socket()
Creates a socket.
bind()
Assigns address to socket.
listen()
Marks socket as passive.
accept()
Accepts client requests.
connect()
Used by client to connect to server.
send()
Sends data.
recv()
Receives data.
shutdown()
Partially closes communication.
close()
Fully closes socket.
TCP Socket Communication
TCP provides reliable communication.
How it works:
-
Client requests connection
-
Server accepts
-
Data exchanged
-
Connection closed
TCP ensures:
-
Ordered delivery
-
No packet loss
-
Error recovery
-
Flow control
This makes it suitable for applications requiring accuracy.
UDP Socket Communication
UDP is simpler.
How it works:
-
Client sends data
-
Server receives directly
-
No formal connection
UDP is useful where speed is more important than reliability.
File Descriptor Role in Socket Programming
In UNIX, each socket is represented by a file descriptor.
This is an integer returned by the kernel when a socket is created.
Example:
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
The returned value identifies the socket.
This allows programs to manage sockets like files.
Blocking and Non-Blocking Sockets
Blocking Socket
The program waits until an operation completes.
Example:
-
read waits until data arrives
Non-Blocking Socket
The program continues even if data is unavailable.
Used in:
-
High-performance servers
-
Real-time systems
-
Event-driven applications
Multiplexing in UNIX Sockets
Multiplexing allows one program to monitor many sockets.
Important functions:
-
select()
-
poll()
-
epoll()
These help servers handle thousands of clients efficiently.
Examples:
-
Web servers
-
Chat applications
-
Proxy servers
Socket Address Structure
Socket addresses are stored in structures.
For IPv4:
struct sockaddr_in
Contains:
-
Address family
-
Port number
-
IP address
This structure helps the kernel identify communication endpoints.
Security in Socket Programming
Network communication requires security.
UNIX systems use:
-
Access control
-
Firewall integration
-
Encryption
-
Authentication
Secure socket methods include:
-
SSL
-
TLS
-
SSH
These protect transmitted data.
Applications of UNIX Socket Programming
Socket programming is used in many real-world systems.
Web Services
Web servers communicate using sockets.
Examples:
-
HTTP server
-
API server
Remote Administration
Tools like SSH rely on sockets.
File Sharing
Network file transfer uses sockets.
Database Systems
Client applications connect to database servers using sockets.
Messaging Systems
Chat applications use socket communication.
Benefits of UNIX Socket Programming
Advantages include:
-
Standardized communication
-
Fast data transfer
-
Scalable architecture
-
Supports local and remote communication
-
Flexible protocol handling
-
Reliable service creation
Challenges
Socket programming also introduces complexity.
Issues include:
-
Synchronization
-
Packet handling
-
Network errors
-
Security concerns
-
Resource management
-
Connection timeout handling
Real Example
When a browser opens a website:
-
Browser creates client socket
-
Connects to server IP
-
Sends request
-
Server receives request
-
Server sends webpage data
-
Browser displays page
-
Connection closes
This entire process depends on socket programming.
Importance in UNIX
Socket programming is a core part of UNIX because UNIX was designed as a multiuser and network-capable operating system. It supports powerful networking APIs that developers use to create distributed systems.
Modern technologies such as:
-
Cloud platforms
-
Web servers
-
Microservices
-
Internet applications
-
Network tools
all rely heavily on socket communication.
Conclusion
UNIX network socket programming provides the foundation for communication between processes across networks. It uses sockets as endpoints for data exchange and supports both reliable and fast communication through TCP and UDP.
It is essential for building client-server applications, network services, and distributed systems. Understanding sockets helps in learning how real-world internet services operate, since nearly every online application depends on socket communication at its core.