C Programming

Bind System Call in C

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.


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:

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.

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.

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.

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.

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


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

Kalsoom Bibi

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