Interprocess Communication with Sockets

Computer EngineeringMCAOperating System

Interprocess communication 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 of data from one process to another.

One of the ways to manage interprocess communication is by using sockets. They provide point-to-point, two-way communication between two processes. Sockets are an endpoint of communication and a name can be bound to them. A socket can be associated with one or more processes.

Types of Sockets

The different types of sockets are given as follows −

  • Sequential Packet Socket: This type of socket provides a reliable connection for datagrams whose maximum length is fixed This connection is two-way as well as sequenced.

  • Datagram Socket: A two-way flow of messages is supported by the datagram socket. The receiver in a datagram socket may receive messages in a different order than that in which they were sent. The operation of datagram sockets is similar to that of passing letters from the source to the destination through a mail.

  • Stream Socket: Stream sockets operate like a telephone conversation and provide a two-way and reliable flow of data with no record boundaries. This data flow is also sequenced and unduplicated.

  • Raw Socket: The underlying communication protocols can be accessed using the raw sockets.

Socket Creation

Sockets can be created in a specific domain and the specific type using the following declaration −

int socket(int domain, int type, int protocol)

If the protocol is not specified in the above system call, the system uses a default protocol that supports the socket type. The socket handle is returned. It is a descriptor.

The bind function call is used to bind an internet address or path to a socket. This is shown as follows −

int bind(int s, const struct sockaddr *name, int namelen)

Connecting Stream Sockets

Connecting the stream sockets is not a symmetric process. One of the processes acts as a server and the other acts as a client. The server specifies the number of connection requests that can be queued using the following declaration −

int listen(int s, int backlog)

The client initiates a connection to the server’s socket by using the following declaration −

int connect(int s, struct sockaddr *name, int namelen)

A new socket descriptor which is valid for that particular connection is returned by the following declaration −

int accept(int s, struct sockaddr *addr, int *addrlen)

Stream Data Transfer

The send() and recv() functions are used to send and receive data using sockets. These are similar to the read() and write() functions but contain some extra flags. The declaration for send() and recv() are as follows −

int send(int s, const char *msg, int len, int flags)
int recv(int s, char *buf, int len, int flags)

Stream Closing

The socket is discarded or closed by calling close().

Published on 28-Sep-2018 15:58:51