The bind() system call binds an address or name with any socket in the C language. This system call has mostly been used in socket programming. This address will be bonded to the socket descriptor. The bind() system call contains three parameters in actual. The first one is the descriptor of a socket. The second parameter is the pointer used for the address of a socket. The address must be some local path. The third argument can be the size of a socket address. Today’s guide will see how the “bind()” system call works in Ubuntu 20.04 using the C language. So, log in from the system first.

Example:

After the successful and proper login from the user account of the Linux system, open up the shell console application. You may either use the “Ctrl Alt T” shortcut key or search it from the desktop Activity area to launch it. After the launch of the Terminal console, let’s first create a C language file with the widely known query “touch” as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-1.png" data-lazy- height="20" src="data:image/svg xml,” width=”307″>

This file has been created in the Linux home folder. To open and edit this file, use any simple editor. The most suggested ones are Vim and GNU. So, we have utilized the “GNU” nano editor to open the newly made “new.c” file.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-2.png" height="23" src="data:image/svg xml,” width=”300″>

After the file has been opened in the editor, we have added the below-displayed code in it. The code contains the necessary libraries included in it for the socked programming and input-output stream. At the very start of a code, we have defined the path of a socket as the “home” directory of Ubuntu 20.04. The main method has been initialized with an integer variable and character type array pointer as an argument. An integer variable “s” has been declared. A structure type variable for the socket address has been initialized. The function “socket” has been used to initialize a socket in the Linux system and a socket stream with 0 sizes. All the information regarding the socket has been saved in its descriptor “s”. If the size of a socket equals “-1”, It must throw an error message while quitting the compilation of a code.

The function “memset” has been used here to bind the local socket address with the size of a structure defined already. The family of a system socket address has been defined. After that, the system call strncpy has been used here to copy the socket address to a structured path and find the size of a structured path. Now the main part comes. We have been using the “if” statement here to use the bind() system call in it. The bind system call has been fetching the socket descriptor, getting the structural address and bind it with the local socket address, and getting the size of a socket address path. If the size of a structural address equals “-1”, it will print an error “bind” along with some built-in error message from the system using the “EXIT_FAILURE” function. The code has been saved after that.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-3.png" data-lazy- height="402" src="data:image/svg xml,” width=”699″>

Firstly, compile your code with a “GCC” compiler used for C language in the Linux system. After that, execute the file with the “a.out” query in the console. The output throws an exception that the Address is already in use. It indicated the address “home” directory of the Linux system, which is always in users’ use.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-4.png" data-lazy- height="56" src="data:image/svg xml,” width=”302″>

Let’s open the same “new.c” file to make a little change in it. We have to use the same “nano” query in the console application to open it within an editor for edit. After the file has been properly opened, we have changed the Socket path defined at the top of the code. We have changed the path to “Pictures” from “home” to get a different result.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-5.png" data-lazy- height="402" src="data:image/svg xml,” width=”700″>

After the compilation, the file was executed, and got another message. This message also indicates the error “Permission denied” to access the path “Pictures”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bind-System-Call-in-C-6.png" data-lazy- height="54" src="data:image/svg xml,” width=”316″>

Conclusion:

So, this was all about the Bind() system call in C within Linux operating system. We have given a brief explanation of socket programming. Make sure to go through the Linux manual for a clear understanding.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content