what is socket in networking

what is socket in networking?


What is socket in Computer Networking
What is a socket in Computer Networking

An operating system is software that manages the computer resources and the user processes. It provides all the necessary software packages to create and execute a process. There could be more than one process running at the same time. Each having its own memory space, process id, etc.

Sometimes to achieve the desired functionalities a process requires communication with other processes. The mechanism is known as Inter-Process Communication (IPC). Communicating processes could be located on the same computer or on far machines.

On the same machine, two processes can choose multiple IPC mechanisms e.g Piped, FIFO, messages queues, network sockets, etc. But if they are running on two different machines, the network socket is the only option to communicate.

In this tutorial, we will explain what is a socket in a computer network and what are different types of sockets.

What is a network socket?

A network socket is an endpoint associated with a process to provide a means for two-way communication with a remote application. At the Linux level, it is a file descriptor.

A process that wants to send or receive data over the network, opens a file descriptor and assigns an IP address and a port number to it.

Process and socket
Process and socket

In Linux, socket() is the system call to open a file descriptor and bind() for assigning an IP address and a port number. To send and receive data, a process uses the socket. A message over the network, in the source address, has the IP address that is assigned to the sending socket and the destination address having the IP address of the destination socket.

Client server model and network sockets:

Mostly the network applications work in a client-server model. A server is a process that opens the socket and waits for the connection from the client. A network client is a process that opens the socket and tries to connect to the server.

After a connection establishment, the data transfer begins. For example, if you browse a website, the web browser works as an HTTP client, and the domain you want to access works as a server.

At the network level, there is a TCP connection establishment. Once it’s done, the browser sends the webpage request and the server responds with a page.

What are the types of network sockets?

As per the OSI model, sockets are part of the transport layer protocols. There are two types of transport layer protocols, one is connection-oriented such as TCP and another is connectionless e.g UDP. This results in two kinds of sockets, stream, and datagram.

Stream socket –

The kind of sockets used by connection-oriented bidirectional communication. The client set up a connection with the server before sending/receiving any data. The data in each direction flows in the streams of bytes. This put additional responsibilities on the user applications to identify message boundaries within a stream. A stream socket connection ensures, sequenced and error-free data transfer.

Once the data transfer is done, either side can start a connection close procedure for a graceful closure of the communication channel.

Following is a list of system calls on the Linux operating system for stream-oriented network sockets.

System CallDescription
socket()Creates a socket by opening a new file descriptor.
bind()Assigns the IP address and port to an already created socket.
listen()Waits for the new connection from a client process.
accept() Accepts a new connection from the client and returns a file descriptor for the connection.
connect() Starts a connection establishment from the client.
read() Reads the data from a socket.
write()Write to the socket to send the data
close()Closes an already open communication.

Datagram socket –

This type of socket requires no setup of a connection before starting any data transfer. A sender just sends the packet and the receiver receives it. If receive is not ready packet will be dropped. Datagram type of socket communication does not guarantee sequence and error-free delivery. But these are faster as compared to the stream socket, so suit well for real-time delivery requirements.

Following is the list of system calls for datagram sockets.

System CallDescription
socket()Creates a socket by opening a new file descriptor.
bind()Assigns the IP address and port to an already created socket.
read() Reads the data from a socket.
write()Write to the socket to send the data.