What is a socket in networking?


Know all about network sockets.

Mesh Network Topology Type
How does a socket enable communication?

In computing, an operating system is a software that runs on top of the underlying hardware. The software manages computer resources (such as printers, drives, disks, RAM, etc.) and user processes.

An application that has been developed to perform a specific task, such as banking, network communication, etc., is referred to as a user process.

Using network sockets, user processes can communicate with each other on the same or different hosts.

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

Why might a user process require a socket?

The communication between two processes may be necessary to achieve the desired functionalities. The communicating processes may be located on the same or on different computers that are distant from one another.

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

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

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?

In the Linux kernel, a network socket is a file descriptor that enables two-way communication with a remote application.

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 assigns an ephemeral port range, an integer between 1024 and 65535.

Process and socket
Process and socket

Sockets and Linux system calls.

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 program creates a socket and waits for clients to connect. On the other hand, a network client is a process that opens the socket and attempts to connect to the server when using a connection-oriented protocol like TCP.

The data transfer begins after the connection is established. For example, when you visit a website, 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 establishes 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 sequence and error-free.

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

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 socket type does not require establishing a connection. The sender 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.