C Programming

So_Reuseaddr Soket Parameter

You may have tried many C functions, many POSIX C library functions, and Unix-based C functions in your programs. Most of the C functions related to POSIX or networks use some flags in their parenthesis as an argument or used for specific purposes. Have you ever tried to learn some built-in flags while programming? If not, it’s totally fine.

We will discuss the use of the SO_REUSEADDR flag of C in this article. It is an integer Boolean type flag that specifies whether or not the local address reuse should be permitted by the rules used to validate the addresses provided in the bind system calls. This means that a socket may bind for the AF_INET sockets, barring the presence of an active listening socket connected to the address. It is not feasible to bind to a listening socket’s specified port when it is configured to INADDR_ANY for any local address.

The bind method can reuse the local address after using the SO_REUSEADDR flag. Note that the local address, which is composed of IP address and port number, signifies that two local addresses can coexist in themselves if any IP or port number part is different and therefore does not necessitate the application of this flag. The local address cannot be utilized with this parameter if it has already been accessed by a socket that is in a listen-to state. When the local address of the socket is in the listening status and the IP component of the address is INADDR ANY, all the local IPS are monitored. The bind can no longer sustain any local addresses for this port, even with this argument.


Here is the client code that is used for the clients to connect to the server using the two separate IP addresses. The client in the following example wishes to connect to the server that is using a different port number but the same IP address. The local host IP address and port value were passed to the connect_tcp function in the first call where they were set to 8888.

However, after binding to the local address, the system used the UNIX Socket Domain to perform a second socket connection where the IP address remained the same but the port number was changed to 7777. It means that a client is connected to two different socket addresses on a single TCP request, which is conceivable because the addresses are different. This code links to the targeted machine after binding the local address first. The two bind attempts should be successful because, as the aforementioned shows, the IP portion of the local addresses of the two bind connections varies.

Utilizing a Linux command line terminal, compile the code using the GCC compiler for the C language code:

Using the ncat command, we imitate the server:

To view each socket status for port number 7777, use the ss command:

The following output displays the socket status for port number 7777:

As seen from the pevious screenshot, there is now no other connection and just the ncat server is listening on port 7777. After executing the output file created in the compilation step, execute it.

Now, execute the same command after executing the output file of the previous code in the Linux terminal. Notice that there are two calls to connect_tcp connection, then four connections are established during these calls.

Now that two connections are successfully made, one using and port 7777 and the other using port 8888, we received the following output. There are four connections overall in the following screen because we invoked the connect tcp method twice:

There are four ESTAB (Establish) state connections visible, which is normal because the output is from the server side and the client side, respectively. The very first three lines are written from the perspective of the server while the subsequent two are presented from that of the client. The process name in the background also shows this. Before joining, the client can connect many local IP addresses, and the server can do the same thing by using a target. It can bind to a variety of local addresses before monitoring without the SO_REUSEADDR argument. We won’t illustrate it now because the software code is similar. The code first binds the local address to the target address, which is either or, and then perform a connection to those IP addresses.

When we run this client, it displays on the screen the “Address already in use” indicating that the first client has this address. The previous code is now modified by just adding a few more lines of the code to use the SO REUSEADDR flag. We wish to connect our client to a similar socket address that is already used or held by another client. Here is the code snippet of the modified code:

We used the VIM editor to open the file and added the code in the insert mode.

Recompile the code. But before you do, take note of the following: either create a new file or utilize an existing one, and modify the output file’s name during compiling. After compilation, execute the output file using the command line terminal. After execution, we can see that a client is connected to the same IP address and port number where our first client was connected. This is because we used the SO_REUSEADDR flag.


This is about the use of the so_reuseaddr socket parameter which is a Boolean type flag in the C language. We have seen an example using the so_reuseaddr socket parameter to specify whether or not the local address reused in further programs or threads should be allowed by the directions used to authenticate the addresses only if in the bind system calls.

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.