Sockets are endpoints for communication. Importantly, sockets enable communication between programs running on separate machines or programs running on the same machine.
2. TCP/IP Sockets
TCP/IP stands for Transmission Control Protocol/Internet Protocol. These sockets facilitate communication between two computers. As a result, the socket consists of the IP address and the port number that the machines use to transmit the data. In fact, all applications transmitting data use the socket to send and receive data.
In particular, TCP/IP supports three types of sockets: stream sockets, datagram sockets, and raw sockets.
2.1. Stream Sockets
Stream sockets are the most commonly used type of communication transport protocol over TCP/IP. Also, these sockets provide a reliable and nearly error-free data pipe between two endpoints. In addition, the data pipe can be bidirectional. Both endpoints transmit and receive through the socket.
Moreover, stream sockets are also relied upon to deliver sequenced and unduplicated data. This means packets are delivered in the order they are sent and that computers receive a particular packet only once.
However, if packets arrive on the physical network out of order, the network adapter and the host operating system ensure they are assembled in the correct sequence for processing by an application.
For example, a use case for stream sockets is a web server like Apache handling HTTP requests on port 80. For HTTP, a socket address would be similar to 18.104.22.168:80.
2.2. Datagram Sockets
Datagram sockets define a connection-less service. In fact, these sockets use the User Datagram Protocol (UDP) to encapsulate and transmit data. The packets are sent independently, and there are no guarantees. This means the packets can be lost, duplicated, and arrive out of order.
In addition, the size of the packets is limited to the size that can be sent in one transaction. There is no disassembly or assembly of packets in datagram sockets. To transmit data successfully, applications that use datagram sockets must have their own error handling and data ordering logic.
For example, Domain Name System (DNS) servers use the User Datagram Protocol to send and receive requests for domain names. Since the DNS server’s default port is 53, a socket address would be similar to 22.214.171.124:53.
2.3. Raw Sockets
A raw socket is a type of network socket that enables a software application on the computer to send and receive packets from the network without using the computer’s primary operating system. Note that raw sockets bypass the normal TCP/IP processing system and send packets to the specific user application.
Furthermore, raw sockets allow applications to directly access lower-level protocols such as the Internet Protocol (IP). Along with direct access to lower-level protocols, raw sockets also receive un-extracted packets. Therefore, raw sockets are mainly used to test new protocol implementations.
3. Unix Sockets
Unix sockets, also known as Inter-process Communication (IPC) sockets, are data communication endpoints that allow bidirectional data exchange between processes running on the same computer.
These sockets can be stream-based or datagram-based. In addition, these sockets exchange data between processes directly in the operating system’s kernel through files on the filesystem. Processes read and write to their shared socket files to send and receive data.
Lastly, Unix sockets are widely used by database systems that don’t need to be connected to a network interface. For instance, on Ubuntu, MySQL defaults to using /var/run/mysqld/mysql.sock for communication with local clients.
4. Unix Sockets vs. TCP/IP Sockets
Let’s look at the differences between Unix sockets and TCP/IP sockets:
|Comparison Factor||Unix Sockets||TCP/IP Sockets|
|Full Name||Unix sockets are also known as Inter-process Communication (IPC) sockets||TCP/IP stands for Transmission Control Protocol/Internet Protocol|
|Functionality||Unix sockets are used for communication between processes running on the same computer||TCP/IP sockets are used for programs running on different computers|
|Requirements||There is no need for a hostname and port number for processes to communicate||There is a need for a hostname and port number for programs to communicate using TCP/IP sockets|
|Speed||Unix sockets connections are fast because the processes are running on the same system, and they can avoid some checks and operations||TCP/IP connections are slow relative to Unix sockets|
In this article, we learned about TCP/IP sockets and Unix sockets. We also looked at the differences between the two sockets.