Linux Commands

Unix Domain Socket Usage

“When data needs to be exchanged between processes running on the same host operating system, a Unix Domain Socket (UDS) is introduced as the data communications endpoint. Inter-Process Communication socket, often known as UDS, is a type of IPC Socket. Processes running on the same CPU can communicate effectively owing to UNIX domain sockets. Use the socket function and AF_UNIX as the socket’s domain to establish a UNIX domain socket. A UNIX domain socket must be bound to a specific file path using the bind function after it has been created. To effectively communicate between processes on the same computer, the AF_UNIX (commonly referred to as AF_LOCAL) socket family is implemented.”

In the past, UNIX domain sockets were either unidentified or linked to a file system pathname. Within this article, we will discuss the use of the Unix Domain socket.

Let’s see an example where we used the AF_UNIX socket domain family to perform communication between client and server. We can run multiple clients again on a single server, but for demo purposes, we just use the single client connected to a server. In this case, two different processes, one running for the server and one running for a client, are communicating on the same computer for which the UNIX domain socket is used. Create a file using VIM Editor and name it server1.c but you can use NANO or any other editor.

Type the subsequent lines of code into the file when it is open in insert mode (Escape + I). First, define the SOCKET NAME variable, i.e., the name of the communication socket. In the temporary directory, we have added the socket file. The subsequent lines of code come before the main function, including the necessary header files. The socket name structural variable of type sockaddr_un is declared. Create four variables of the integer type to be used later. The creation of the server socket and channel communication has been divided into the following steps:

1. Using the socket() system call and the AF UNIX flag, the server creates a UNIX domain socket. Future system calls can be made using the file descriptor that this method returns. The connection socket variable, which is a server file descriptor, is tested in the conditional statement to see if it contains -1, which denotes that the socket construction process failed.

2. Next, we must use the portable memset function to completely wipe the memory. Set the socket’s family name to AF UNIX after that.

3. For the client to connect, the server binds the socket to a well-known address using the bind() system call, but before that, copy the SOCKET_NAME to the socket_name.sun_path variable using string copy method (strcopy). Using the return result in a conditional expression, we determine whether the bind system call was successful or not.

4. The listen() system call is used by the server to designate a socket as passive or as one that will accept incoming connection requests from clients.

5. The client sends individual messages for each of its command-line inputs. The server calculates the sums of incoming messages. The command string “END/ENTER” is sent by the client. The server replies with a message that contains the client’s integers added together. After printing the sum of input values in response by the server, the client exits. As quickly as a new client associates, the server waits by using the loop. The parameter “DOWN” can be used to terminate the server when the client is invoked.

6. Connection listening is done in the first for loop, while read and write operations are invoked in the second loop. When sending messages to clients, the server employs the writing system call.

7. After that, the peer socket can be reached via the read() and write() system functions (i.e., to communicate between the server and the client).

8. Finally, the server should call the close() method to close the connection after it is through accessing the socket.

Use up the command indicated in the screenshot to compile the code on Linux using the GCC compiler. This command creates a server-named output file.

Here is the client-side file’s code in the C Programming language. The SOCKET NAME used in the server file is also needed for communication. Create a UNIX Domain Socket after importing the necessary header files, using the same approach as in the server file. The rest code is similar to the client using the write() system call to send input to the server. The parameters in the main function header are used to read the command line inputs, and then we write them using a for loop to transmit to the server. Wait for the server response using the read method after a successful write operation. The read method keeps the server’s answer in a buffer and then displays it on the screen. Close the socket connection after this communication.

Let’s examine how the server and client interact. To do this, we’ll need two terminals, where we must first run the server output file before launching the client and sending inputs to the server. The client exits after reading and displaying the server’s response.

In case the bind-address is already in use, in this case, use the SO_REUSEADDR as the socket option.

If the server is offline and a client wants to connect, the output would be like the below.

If a client does not provide any input number:

If the client enters a number while communicating with the server, the server will add the numbers and respond to the client by displaying the result.

To shut down the server on client request

Conclusion

In this article, we have demonstrated the use of both client-side and server-side to utilize the UNIX domain socket. For this, we have tried the simple C code for both sides in the Kali Linux operating system. We hope you will get good help from this article.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.