What is a socket in networking?

Know all about a network socket.

Mesh Network Topology Type
How does a socket enable communication?

An operating system is a piece of software that runs on the hardware of a computer. It oversees the management of computer resources (such as printers, hard discs, RAM, and so on) along with user processes for user-specific tasks. Network sockets enable user processes to communicate with each other.

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

Why a user process may need a socket?

A process may need to communicate with other processes in order to achieve the desired functionalities. Communicating processes could be on the same or on computers separated by a distance.

Two processes on the same machine have multiple options for sending and receiving data, such as piped, FIFO, messages queues, network sockets, and so on.

However, if they are running on different machines, the network socket is the only way for them to communicate.

In this tutorial, we will go over what a socket is in a computer network and all types of it.

What is a network socket in Linux?

A network socket is a type of endpoint that is associated with a process and allows two-way communication with a remote application. It is a file descriptor on the Linux level kernel level.

When a process wants to send or receive data over the network, it creates a file descriptor and assigns an IP address and a port number to it. If the process does not provide a port number, the operating system will use an ephemeral port range, which is an integer between 1024 and 65535.

Process and socket
Process and socket

A user program in Linux calls the socket() system call to create a new file descriptor. After the socket has been opened, the bind() system call is used to assign an IP address and a port number to it.

Other functions, known as send and receive using the opened socket, are used to send and receive user data. A send transmits a message over the network using TCP/IP or other underlying protocol layers.

A user message over the network has a source address. The sending socket’s IP address is the source address, while the destination socket’s IP address is the destination address.

Client-Server model and network sockets:

The majority of network applications use client-server architecture. A server is a program that creates a socket and waits for clients to connect. A network client, on the other hand, is a process that, when using a connection-oriented protocol like TCP, opens the socket and attempts to connect to the server.

The data transfer begins after the connection is established. When you visit a website, for example, your web browser acts as an HTTP client, and the domain you want to visit acts as the server IP.

A TCP connection is established at the network level. The browser then sends an HTTP page request to the server, and the server responds with another page.

A sender does not establish a connection for connectionless communication (e.g., UDP), and can send a packet immediately after opening a socket.

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 –

Connection-oriented bidirectional communication uses a specific type of socket. Before sending or receiving any data, the client established a connection with the server. Data flows in streams of bytes in both directions. This adds to the user application’s responsibilities of identifying message boundaries within a stream. A stream socket connection ensures that data is transferred in a timely and error-free manner.

After the data transfer is complete, either party can initiate a connection close procedure to gracefully terminate the communication channel.

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

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 –

Before beginning any data transfer, this type of socket does not require the establishment of a connection. The sender simply sends the packet, which is received by the receiver. The packet will be dropped if the receiver is not ready. Socket communication using datagrams does not guarantee delivery in order or without errors. However, these are faster than stream sockets and are well-suited 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.