setsockopt无法与SO_RCVBUF减少和TCP_QUICKACK一起使用

时间:2019-09-23 10:38:07

标签: c linux sockets tcp setsockopt

我有一个简单的TCP客户端-服务器示例。服务器仅用于接收数据。服务器端逻辑是:

#define LISTEN_PORT 31337
#define BUFFER_SIZE 4096
//#define QUICKACK

void main () {
     int buflen = 6000, opt_val = 1;
     int server_fd, client_fd, err;
     struct sockaddr_in server, client;
     char buf[BUFFER_SIZE] = {0};

     server_fd = socket(AF_INET, SOCK_STREAM, 0);
     if (server_fd < 0) {
          perror("SOCKET");
          return;
     }

     server.sin_family = AF_INET;
     server.sin_port = htons(LISTEN_PORT);
     server.sin_addr.s_addr = htonl(INADDR_ANY);

     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val) < 0) {
          perror ("SETSOCKOPT");
          return;
     }

     if (bind(server_fd, (struct sockaddr *) &server, sizeof(server)) < 0) {
          perror("BIND");
          return;
     }

     if (listen(server_fd, 128)) {
          perror("LISTEN");
          return;
     }

     printf("Server is listening on %d\n", LISTEN_PORT);

     for(;;) {
          socklen_t client_len = sizeof(client);
          client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
          if (client_fd < 0) {
               perror("ACCEPT");
               return;
          }

          if (setsockopt(client_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof(int)) < 0) {
               perror ("SETSOCKOPT");
               return;
          }

#ifdef QUICKACK
          if (setsockopt(client_fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int)) < 0) {
               perror ("SETSOCKOPT");
               return;
          }
#endif

          for (;;) {
               int read = recv(client_fd, buf, BUFFER_SIZE, 0);
#ifdef QUICKACK
               if (setsockopt(client_fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int)) < 0) {
                    perror ("SETSOCKOPT");
                    return;
               }
#endif

               if (!read) break;
               if (read < 0)
                    perror("READ");
               printf("%s\n", buf);
          }
     }
     return;
}

然后我与客户端或 nc 连接,并在 tcpdump 中看到一些奇怪的行为,例如:

<-[SYN]赢得43690

-> [SYN,ACK]赢得43690

<-[ACK]赢得43690

<-[PSH,ACK]赢得43690,长度为79

<-[PSH,ACK]赢得43690,长度为79

-> [ACK] ack 159,获胜43532

所以我可以得出结论,setsockopt()都没有为我工作。我试图禁用窗口缩放(sysctl -w net.ipv4.tcp_window_scaling=0),但这没有帮助。

相关:12

0 个答案:

没有答案