TCP server is slow

koryckid
Posts: 2
Joined: Wed Oct 23, 2019 8:23 pm

TCP server is slow

Postby koryckid » Thu Oct 24, 2019 5:55 am

Hello, i am new in freertos and esp32.
I am working on the fastest speed with transmission packet over tcp socket,
I have TCP server on esp32, and from laptop ( both device connected to router) i send with netcat a bytes.
Program is one's from example esp-idf esp32 received fata and send back.

I am using wireshark to measure the time sending two bytes from laptop to esp and from esp to laptop.
Time is very big and because it takes ~150 ms and it is not constant for every time.

Image

I set RTOS CLOCK to 1000HZ.

Do you have any idea how to speed up this transmission
  1. /* BSD Socket API Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include <sys/param.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/event_groups.h"
  14. #include "esp_system.h"
  15. #include "esp_wifi.h"
  16. #include "esp_event_loop.h"
  17. #include "esp_log.h"
  18. #include "nvs_flash.h"
  19.  
  20. #include "lwip/err.h"
  21. #include "lwip/sockets.h"
  22. #include "lwip/sys.h"
  23. #include <lwip/netdb.h>
  24.  
  25.  
  26. /* The examples use simple WiFi configuration that you can set via
  27.    'make menuconfig'.
  28.    If you'd rather not, just change the below entries to strings with
  29.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  30. */
  31. #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  32. #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  33.  
  34. #define PORT CONFIG_EXAMPLE_PORT
  35.  
  36. /* FreeRTOS event group to signal when we are connected & ready to make a request */
  37. static EventGroupHandle_t wifi_event_group;
  38.  
  39. const int IPV4_GOTIP_BIT = BIT0;
  40.  
  41. static const char *TAG = "example";
  42.  
  43. static esp_err_t event_handler(void *ctx, system_event_t *event)
  44. {
  45.     switch (event->event_id) {
  46.     case SYSTEM_EVENT_STA_START:
  47.         esp_wifi_connect();
  48.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
  49.         break;
  50.     case SYSTEM_EVENT_STA_CONNECTED:
  51.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_CONNECTED");
  52.         break;
  53.     case SYSTEM_EVENT_STA_GOT_IP:
  54.         xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
  55.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
  56.         break;
  57.     case SYSTEM_EVENT_STA_DISCONNECTED:
  58.         /* This is a workaround as ESP32 WiFi libs don't currently auto-reassociate. */
  59.         esp_wifi_connect();
  60.         xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
  61.         break;
  62.     default:
  63.         break;
  64.     }
  65.     return ESP_OK;
  66. }
  67.  
  68. static void initialise_wifi(void)
  69. {
  70.     tcpip_adapter_init();
  71.     wifi_event_group = xEventGroupCreate();
  72.     ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  73.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  74.     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  75.     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  76.     wifi_config_t wifi_config = {
  77.         .sta = {
  78.             .ssid = EXAMPLE_WIFI_SSID,
  79.             .password = EXAMPLE_WIFI_PASS,
  80.         },
  81.     };
  82.     ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  83.     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  84.     ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  85.     ESP_ERROR_CHECK( esp_wifi_start() );
  86. }
  87.  
  88. static void wait_for_ip()
  89. {
  90.     uint32_t bits = IPV4_GOTIP_BIT;
  91.  
  92.     ESP_LOGI(TAG, "Waiting for AP connection...");
  93.     xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
  94.     ESP_LOGI(TAG, "Connected to AP");
  95. }
  96.  
  97. static void tcp_server_task(void *pvParameters)
  98. {
  99.     char rx_buffer[128];
  100.     char addr_str[128];
  101.     int addr_family;
  102.     int ip_protocol;
  103.  
  104.     while (1) {
  105.  
  106.         struct sockaddr_in destAddr;
  107.         destAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  108.         destAddr.sin_family = AF_INET;
  109.         destAddr.sin_port = htons(PORT);
  110.         addr_family = AF_INET;
  111.         ip_protocol = IPPROTO_IP;
  112.         inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
  113.  
  114.         int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  115.         if (listen_sock < 0) {
  116.             ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  117.             break;
  118.         }
  119.         ESP_LOGI(TAG, "Socket created");
  120.  
  121.         int err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
  122.         if (err != 0) {
  123.             ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  124.             break;
  125.         }
  126.         ESP_LOGI(TAG, "Socket binded");
  127.  
  128.         err = listen(listen_sock, 1);
  129.         if (err != 0) {
  130.             ESP_LOGE(TAG, "Error occured during listen: errno %d", errno);
  131.             break;
  132.         }
  133.         ESP_LOGI(TAG, "Socket listening");
  134.  
  135.         struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
  136.         uint addrLen = sizeof(sourceAddr);
  137.         int sock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
  138.         if (sock < 0) {
  139.             ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
  140.             break;
  141.         }
  142.         ESP_LOGI(TAG, "Socket accepted");
  143.  
  144.         while (1) {
  145.             int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  146.             // Error occured during receiving
  147.             if (len < 0) {
  148.                 ESP_LOGE(TAG, "recv failed: errno %d", errno);
  149.                 break;
  150.             }
  151.             // Connection closed
  152.             else if (len == 0) {
  153.                 ESP_LOGI(TAG, "Connection closed");
  154.                 break;
  155.             }
  156.             // Data received
  157.             else {
  158.                 // Get the sender's ip address as string
  159.                 if (sourceAddr.sin6_family == PF_INET) {
  160.                     inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  161.                 } else if (sourceAddr.sin6_family == PF_INET6) {
  162.                     inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  163.                 }
  164.  
  165.                 rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  166.                 //ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
  167.                 //ESP_LOGI(TAG, "%s", rx_buffer);
  168.  
  169.                 int err = send(sock, rx_buffer, len, 0);
  170.                 if (err < 0) {
  171.                     ESP_LOGE(TAG, "Error occured during sending: errno %d", errno);
  172.                     break;
  173.                 }
  174.             }
  175.         }
  176.  
  177.         if (sock != -1) {
  178.             ESP_LOGE(TAG, "Shutting down socket and restarting...");
  179.             shutdown(sock, 0);
  180.             close(sock);
  181.         }
  182.     }
  183.     vTaskDelete(NULL);
  184. }
  185.  
  186. void app_main()
  187. {
  188.     ESP_ERROR_CHECK( nvs_flash_init() );
  189.     initialise_wifi();
  190.     wait_for_ip();
  191.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  192.  
  193.     xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 24, NULL);
  194. }

Who is online

Users browsing this forum: No registered users and 110 guests