SO_REUSEADDR and SO_REUSEPORT socket options for TCP/IP programming

Keywords: socket Nginx

Basic concepts:

The SO_REUSEADDR socket option has four different functions:

(1) SO_REUSEADDR allows you to start a listening server and bundle well-known ports, even though previously established ports still exist as their local ports.

This condition is usually met as follows:

A) Start a listening server;

b) Connect to the request and derive a subprocess to process the customer;

c) The listening server terminates, but the subprocess continues to serve customers on existing connections;

d) Restart the listening server.

By default, when the listening server calls socket, bind, and listen to restart in step d, the bind call fails because it attempts to bundle ports on an existing connection. But if the server sets the SO_REUSEADDR socket option between socket and bind calls, then bind will succeed. All TCP servers should specify this socket option to allow the server to be restarted in this case.

(2) SO_REUSEADDR allows multiple instances of the same server to be started on the same port as long as each instance is bundled with a different local IP address.

(3) SO_REUSEADDR allows a single process to bundle the same port to multiple sockets, as long as each bundle specifies a different local IP address.

(4) SO_REUSEADDR allows full duplicate binding: when an IP address and port are bound to a socket. If the transport protocol supports, the same IP address and port can also be bundled to another socket. Generally speaking, this feature only supports UDP sockets.


The SO_REUSEPORT socket option performs the following two different functions:

(1) This option allows full duplicate bundling, but only if each socket that wants to bundle the same IP address and port specifies this socket option.

(2) If the bound IP address is a multicast address, SO_REUSEADDR and SO_REUSEPORT are considered equivalent.


Here we focus on:

SO_REUSEADDR's first (1) function (blue font)

SO_REUSEPORT's first (1) function (blue font)


Application scenario: The nginx smoothing upgrade is applied to relevant knowledge.


Example 1:

We need to know,

(a) If there is no restriction on TCP socket options, if two processes are started, the second process will make an error when calling the bind function (Address already in use).

b) If we set SO_REUSEADDR before calling bind, but do not close the socket before the second process starts, then the second process will still make an error when calling the bind function (Address already in use).

c) If we set SO_REUSEADDR before calling bind and receive a client connection, and close the bind socket before the second process starts, then the first process has only one socket (the connection with the client), then the second process can bind successfully and meet expectations.

Code:

  1. #include <sys/types.h>  
  2. #include <sys/socket.h>  
  3. #include <unistd.h>  
  4. #include <stdio.h>  
  5. #include <errno.h>  
  6. #include <arpa/inet.h>  
  7. #include <stdlib.h>  
  8.   
  9. void Perror(const char *s)  
  10. {  
  11.     perror(s);  
  12.     exit(EXIT_FAILURE);  
  13. }  
  14.   
  15. int main()  
  16. {  
  17.     int sockfd = socket(AF_INET, SOCK_STREAM, 0); //TCP  
  18.     int backlog = 100;  
  19.     short port = 9527; //port  
  20.     struct sockaddr_in servaddr;  
  21.     servaddr.sin_family = AF_INET; //IPv4  
  22.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Represents that the IP address is selected by the kernel  
  23.     servaddr.sin_port = htons(port);  
  24.   
  25.     int flag = 1;  
  26.     if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) {  
  27.         Perror("setsockopt fail");  
  28.     }  
  29.   
  30.     int res = bind(sockfd, (sockaddr *)&servaddr, sizeof(servaddr));  
  31.     if (0 == res)  
  32.         printf("server bind success, 0.0.0.0:%d\n", port);  
  33.     else {  
  34.         Perror("bind fail");  
  35.     }  
  36.   
  37.     if (-1 == listen(sockfd, backlog)) {  
  38.         Perror("listen fail");  
  39.     }  
  40.   
  41.     //Waiting for connection  
  42.     struct sockaddr_in cliaddr;  
  43.     socklen_t len = sizeof(cliaddr);  
  44.     int connfd = accept(sockfd, (sockaddr *)&cliaddr, &len);  
  45.     if (-1 == connfd) {  
  46.         Perror("accept fail");  
  47.     }  
  48.   
  49.     //Resolving Client Address  
  50.     char buff[INET_ADDRSTRLEN + 1] = {0};  
  51.     inet_ntop(AF_INET, &cliaddr.sin_addr, buff, INET_ADDRSTRLEN);  
  52.     uint16_t cli_port = ntohs(cliaddr.sin_port);  
  53.     printf("connection from %s, port %d\n", buff, cli_port);  
  54.   
  55.     //Close sockfd of bind  
  56.     close(sockfd);  
  57.   
  58.     //  
  59.     sleep(1200);  
  60.   
  61.     return 0;  
  62. }  

Compile:

g++ server.cpp -o s1

g++ server.cpp -o s2

Operation results:



Example 2,

SO_REUSEPORT may not be supported in older kernel versions. The kernel version of my test environment is 3.10. Compared with SO_REUSEADDR, SO_REUSEPORT does not have so many restrictions. It allows two unrelated processes to use the same IP address to listen on the same port at the same time and does not appear. thundering herd.

Code:

  1. #include <sys/types.h>  
  2. #include <sys/socket.h>  
  3. #include <unistd.h>  
  4. #include <stdio.h>  
  5. #include <errno.h>  
  6. #include <arpa/inet.h>  
  7. #include <stdlib.h>  
  8.   
  9. void Perror(const char *s)  
  10. {  
  11.     perror(s);  
  12.     exit(EXIT_FAILURE);  
  13. }  
  14.   
  15. int main()  
  16. {  
  17.     int sockfd = socket(AF_INET, SOCK_STREAM, 0); //TCP  
  18.     int backlog = 100;  
  19.     short port = 9527; //port  
  20.     struct sockaddr_in servaddr;  
  21.     servaddr.sin_family = AF_INET; //IPv4  
  22.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Represents that the IP address is selected by the kernel  
  23.     servaddr.sin_port = htons(port);  
  24.   
  25.     int flag = 1;  
  26.     if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag))) {  
  27.         Perror("setsockopt fail");  
  28.     }  
  29.   
  30.     int res = bind(sockfd, (sockaddr *)&servaddr, sizeof(servaddr));  
  31.     if (0 == res)  
  32.         printf("server bind success, 0.0.0.0:%d\n", port);  
  33.     else {  
  34.         Perror("bind fail");  
  35.     }  
  36.   
  37.     if (-1 == listen(sockfd, backlog)) {  
  38.         Perror("listen fail");  
  39.     }  
  40.   
  41.     //Waiting for connection  
  42.     while (1) {  
  43.         struct sockaddr_in cliaddr;  
  44.         socklen_t len = sizeof(cliaddr);  
  45.         int connfd = accept(sockfd, (sockaddr *)&cliaddr, &len);  
  46.         if (-1 == connfd) {  
  47.             Perror("accept fail");  
  48.         }  
  49.   
  50.         //Resolving Client Address  
  51.         char buff[INET_ADDRSTRLEN + 1] = {0};  
  52.         inet_ntop(AF_INET, &cliaddr.sin_addr, buff, INET_ADDRSTRLEN);  
  53.         uint16_t cli_port = ntohs(cliaddr.sin_port);  
  54.         printf("connection from %s, port %d\n", buff, cli_port);  
  55.     }  
  56.   
  57.     return 0;  
  58. }  

Compile:

g++ server.cpp -o s1

g++ server.cpp -o s2

Operation results:

Posted by YappyDog on Wed, 02 Jan 2019 09:51:09 -0800