How Does Linux Use Sockets?

Learn via video courses
Topics Covered

Linux actively utilizes sockets to establish connections, send and receive data, and execute various network operations. They act as the essential link between processes, allowing effective communication and enabling the foundation of networking capabilities in Linux.

What are Linux Sockets?

Linux sockets are software endpoints that facilitate communication between running programs, known as processes, over a network. A socket acts as a interface to enable data exchange. You can think of a socket as a door allowing information to flow in and out between processes.

Examples of processes include web servers, database servers, email clients, chat applications, file transfer programs, and many more.

What are Linux Sockets

What are Linux Sockets Used for?

Linux sockets fulfills various purposes:

  1. Client-Server Communication: Sockets enable data exchange between clients and servers. Servers wait for incoming client connections while clients initiate connections. It allows clients to send requests and servers to respond efficiently.
  2. Interprocess Communication: Sockets facilitate communication between processes on the same or different machines. Processes can share data by sending messages through sockets, making them essential for applications like chat programs.
  3. Network Services: Sockets are vital for network services such as web servers, email servers, and FTP servers. They handle client connections, process requests, and efficiently manage multiple connections for data transfer.
  4. Data Transfer: Sockets are essential for reliable and efficient data transfer between systems, ensuring file integrity during file downloads from remote servers.
  5. Network Programming: Linux sockets are the foundation for network programming, enabling diverse applications like instant messaging, video conferencing, and online gaming through protocol implementation and data exchange.

Types of Linux Sockets

Linux provides different sockets, each designed to fulfill specific communication requirements. Let's explore the different types of Linux sockets:

Stream-Oriented Sockets

Stream-oriented sockets, also known as TCP sockets, are a crucial type of socket in Linux. These sockets facilitate reliable, ordered, and error-checked data transmission. While using TCP sockets, a connection-oriented communication channel is established between two endpoints.

In Linux the ss -tln command shows active TCP socket connections. It displays only TCP sockets (-t), and listening sockets (-l), and displays IP addresses and port numbers numerically (-n).

SS_COMMAND

TCP sockets ensure data integrity, guaranteeing that packets arrive in the same order they were sent. Applications that require the delivery of all data without loss or duplication, such as file transfers or email, benefit from the reliable nature of stream-oriented sockets.

Datagram-Oriented Sockets

Datagram-oriented sockets, also known as UDP sockets, offer a different approach to communication in Linux. Unlike TCP sockets, UDP sockets provide unreliable and unordered communication. They operate on a best-effort basis, meaning some packets may be lost, duplicated, or arrive out of order.

The ss -uln command shows active UDP listening sockets. It filters the output to display only UDP sockets (-u).

SS_COMMAND_DO_SOCKET

Datagram sockets prioritize low overhead and real-time communication scenarios. Applications such as audio/video streaming or online gaming, which require quick data transmission and can tolerate occasional packet loss, often rely on datagram-oriented sockets.

Raw Sockets

Raw sockets in Linux provide direct access to network protocols, bypassing the standard transport layer protocols like TCP or UDP. These sockets offer developers the flexibility to create custom protocols or perform low-level network analysis.

To view raw sockets, use the ss -wln command. It displays raw listening sockets (-w).

RAW_SOCKET

Network monitoring tools, firewalls, and packet sniffers use raw sockets. They enable in-depth inspection of network packets and facilitate the implementation of specialized networking functionality.

Sequenced Sockets

Sequenced sockets, known as SCTP sockets, introduce the Stream Control Transmission Protocol (SCTP) in Linux. SCTP sockets provide reliable, ordered, and multi-stream communication. This socket type is useful for applications requiring reliable and ordered data delivery.

SCTP offers features such as message fragmentation, bundling, and multi-homing. Telephony systems, real-time streaming applications, and other scenarios where data delivery integrity is paramount can benefit from the capabilities of sequenced sockets.

How Sockets Work in Linux

Understanding how sockets work in Linux is essential for effective network programming. Each Linux socket uses a specific domain and type. The domain determines the protocol family, such as IPv4 or IPv6, while the type specifies the nature of communication, such as reliable two-way communication (e.g., TCP) or one-way communication with best-effort delivery (e.g., UDP). WORKING_SOCKET

For instance, when using a stream-oriented TCP socket in Linux, the process starts by specifying the type with the socket() system call. The subsequent API calls differ depending on the system's role in the network.

On the server side:

  • bind() binds a socket to a network address and port.
  • listen() instructs the server to wait for incoming connections to the specified network location.
  • accept() receives the client connections.
  • read() and write() enable communication with the remote endpoint once the server establishes the communication and creates a new socket for the client.

On the client side:

  • connect() establishes a connection with the server process, taking the address of the remote server socket as an argument.
  • send() and recv() are used to send and receive data.
  • close() end the connection between the client and the server. SOCKET_WORKING

On the other hand, datagram communication does not require establishing a connection. The server and the clients use the same system calls to exchange information between sockets.

The socket() call initializes the socket. However, since there is no need to establish a permanent connection, the datagram socket does not utilize the bind(), listen(), and accept() calls like the stream-oriented type. Instead, the send() and recv() calls conduct packet exchange directly. SOCKET_DATAGRAM_WORKING

Conclusion

In conclusion, Linux sockets are essential for network programming, enabling communication between processes over a network. Here are the key takeaways from the article:

  • Linux sockets act as endpoints for process communication.
  • They are used for client-server communication, interprocess communication, network services, data transfer, and network programming.
  • Types include stream-oriented, datagram-oriented, raw, and sequenced sockets.
  • Stream-oriented sockets ensure reliable, ordered data transmission.
  • Datagram-oriented sockets offer low-overhead, unreliable communication.
  • Raw sockets allow direct access to network protocols.
  • Sequenced sockets provide reliable, ordered, multi-stream communication.
  • Socket workflow includes creation, binding, listening, accepting connections, data exchange, and closure.