ESP32-C3-MINI socket连接

wwwwwwww
Posts: 17
Joined: Fri Sep 17, 2021 6:08 am

ESP32-C3-MINI socket连接

Postby wwwwwwww » Sun Sep 26, 2021 3:58 am

C3为服务端,一个设备不停的断开连接再重新连接C3,站点数也会增加,在断开连接时并不会减少站点数.导致多次以后,超过了最大站点连接数.请问有什么解决办法吗

wwwwwwww
Posts: 17
Joined: Fri Sep 17, 2021 6:08 am

Re: ESP32-C3-MINI socket连接

Postby wwwwwwww » Sun Sep 26, 2021 4:04 am

  1.  for (;;)
  2.         {
  3.  
  4.             // initialize file descriptor set
  5.             FD_ZERO(&fdsr);
  6.             FD_SET(listen_sock, &fdsr);
  7.  
  8.             // timeout setting
  9.             tv.tv_sec = 60;
  10.             tv.tv_usec = 0;
  11.  
  12.             // add active connection to fd set
  13.             for (i = 0; i < BACKLOG; i++) {
  14.                 if (fd_A[i] != 0) {
  15.                     FD_SET(fd_A[i], &fdsr);
  16.                 }
  17.             }
  18.             //block here until listen_sock can read
  19.              int ret = select(maxsockfd+1, &fdsr, NULL, NULL, &tv);
  20.             if (ret < 0) {
  21.                 perror("select");
  22.                 break;
  23.             } else if (ret == 0) {
  24.                 printf("timeout\n");
  25.                 continue;
  26.             }
  27.              // check every fd in the set and handle the recv()
  28.             for (i = 0; i < conn_amount; i++)
  29.             {
  30.  
  31.                 if (FD_ISSET(fd_A[i], &fdsr))
  32.                 {
  33.  
  34.                     ret = recv(fd_A[i], rx_buffer, sizeof(rx_buffer) - 1, 0);
  35.                     ret1= ret;
  36.                     station = i;
  37.                     if (ret <= 0)
  38.                     {        // client close
  39.                          ESP_LOGE(TAG, "recv failed or connection closed: errno %d", errno);
  40.                          printf("client[%d] close\n", i);
  41.                         close(fd_A[i]);
  42.                         FD_CLR(fd_A[i], &fdsr);
  43.                         fd_A[i] = 0;
  44.                        // conn_amount=0;
  45.                     }
  46.                     else
  47.                     {        // receive data
  48.                         //printf("star\n");
  49.                         if (ret < BUF_SIZE)
  50.                             memset(&rx_buffer[ret], '\0', 1);
  51.                         printf("client[%d] send:%s\n", i, rx_buffer);
  52.  
  53.  
  54.                         ctl_mode = rx_buffer[0];
  55.                         //Uart0Recv(i,rx_buffer,ret);
  56.                         /*int err = send(fd_A[i], rx_buffer, ret, 0);
  57.  
  58.                         if (err < 0) {
  59.                             ESP_LOGE(TAG, "Error occured during sending: errno %d", errno);
  60.                         continue;
  61.                         }*/
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             // check whether a new connection comes and handler the new connection
  67.             if (FD_ISSET(listen_sock, &fdsr)) {
  68.                 int connect_sock = accept(listen_sock, (struct sockaddr *)&client_addr, &addrLen);
  69.                 if (connect_sock <= 0) {
  70.                     perror("accept");
  71.                     continue;
  72.                 }
  73.                 ESP_LOGI(TAG, "Socket accepted");
  74.  
  75.                 // add to fd queue
  76.                 if (conn_amount < BACKLOG)
  77.                 {
  78.                     fd_A[conn_amount++] = connect_sock;
  79.                     printf("new connection client[%d] %s:%d\n", conn_amount,
  80.                     inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
  81.                     if (connect_sock > maxsockfd)
  82.                         maxsockfd = connect_sock;
  83.  
  84.                 }
  85.                 else {
  86.                     printf("max connections arrive, exit\n");
  87.                     send(connect_sock, "bye", 4, 0);
  88.                     close(connect_sock);
  89.                     continue;
  90.                 }
  91.             }
  92.             //showclient();
  93.         }
  94.          //  when select() return error, close other connections
  95.         for (i = 0; i < BACKLOG; i++) {
  96.            ESP_LOGE(TAG, "Shutting down socket and restarting...");
  97.             if (fd_A[i] != 0) {
  98.                 shutdown(fd_A[i], 0);
  99.                 close(fd_A[i]);
  100.             }
  101.         }

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32-C3-MINI socket连接

Postby ESP_YJM » Sun Sep 26, 2021 9:01 am

你关闭 socket 的时候可以选择强制关闭 socket。 使用 SO_LINGER 选项。具体可以参考 viewtopic.php?f=13&t=20705

Who is online

Users browsing this forum: Google [Bot] and 54 guests