Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Interprocess Communication with Sockets
Interprocess Communication (IPC) is the mechanism provided by the operating system that allows processes to communicate with each other. This communication could involve a process letting another process know that some event has occurred or transferring data from one process to another.
Sockets are one of the most powerful IPC mechanisms, providing point-to-point, two-way communication between processes. They serve as endpoints of communication and can be associated with one or more processes. Sockets can facilitate communication between processes on the same machine or across different machines over a network.
Types of Sockets
The different types of sockets are given as follows −
Sequential Packet Socket: This type provides a reliable connection for datagrams with fixed maximum length. The connection is two-way, sequenced, and maintains record boundaries.
Datagram Socket: Supports a two-way flow of messages without guaranteeing order. Messages may arrive in a different sequence than sent. Similar to sending letters through mail − delivery is not guaranteed to be in order.
Stream Socket: Operates like a telephone conversation, providing reliable, two-way data flow with no record boundaries. Data is sequenced, unduplicated, and connection-oriented.
Raw Socket: Allows direct access to underlying communication protocols, bypassing transport layer processing. Typically used for network programming and protocol implementation.
Socket Architecture
Socket System Calls
Socket Creation
A socket is created using the socket() system call −
int socket(int domain, int type, int protocol)
Parameters:
domain − Communication domain (AF_UNIX for local, AF_INET for Internet)
type − Socket type (SOCK_STREAM, SOCK_DGRAM, etc.)
protocol − Protocol to use (0 for default)
Binding Address to Socket
The bind() function associates an address with a socket −
int bind(int s, const struct sockaddr *name, int namelen)
Stream Socket Communication
Stream socket communication follows a client-server model with asymmetric connection setup.
Server-Side Operations
The server listens for incoming connections −
int listen(int s, int backlog)
The server accepts client connections −
int accept(int s, struct sockaddr *addr, int *addrlen)
Client-Side Operations
The client initiates connection to the server −
int connect(int s, struct sockaddr *name, int namelen)
Data Transfer Operations
Once connection is established, data can be exchanged using specialized socket functions −
int send(int s, const char *msg, int len, int flags) int recv(int s, char *buf, int len, int flags)
These functions are similar to write() and read() but provide additional control through flags parameter for socket-specific behavior.
Socket Termination
Sockets are closed using the standard close() system call, which releases the socket descriptor and terminates the connection.
Conclusion
Sockets provide a flexible and powerful IPC mechanism supporting both local and network communication. They offer various types (stream, datagram, raw) to suit different communication requirements. The client-server model with system calls like socket(), bind(), listen(), and accept() enables robust interprocess communication.
