tcp_server.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* BSD Socket API Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include <sys/param.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "esp_netif.h"
  17. #include "protocol_examples_common.h"
  18. #include "lwip/err.h"
  19. #include "lwip/sockets.h"
  20. #include "lwip/sys.h"
  21. #include <lwip/netdb.h>
  22. #define PORT CONFIG_EXAMPLE_PORT
  23. #define KEEPALIVE_IDLE CONFIG_EXAMPLE_KEEPALIVE_IDLE
  24. #define KEEPALIVE_INTERVAL CONFIG_EXAMPLE_KEEPALIVE_INTERVAL
  25. #define KEEPALIVE_COUNT CONFIG_EXAMPLE_KEEPALIVE_COUNT
  26. static const char *TAG = "example";
  27. static void do_retransmit(const int sock)
  28. {
  29. int len;
  30. char rx_buffer[128];
  31. do {
  32. len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  33. if (len < 0) {
  34. ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno);
  35. } else if (len == 0) {
  36. ESP_LOGW(TAG, "Connection closed");
  37. } else {
  38. rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string
  39. ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer);
  40. // send() can return less bytes than supplied length.
  41. // Walk-around for robust implementation.
  42. int to_write = len;
  43. while (to_write > 0) {
  44. int written = send(sock, rx_buffer + (len - to_write), to_write, 0);
  45. if (written < 0) {
  46. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  47. }
  48. to_write -= written;
  49. }
  50. }
  51. } while (len > 0);
  52. }
  53. static void tcp_server_task(void *pvParameters)
  54. {
  55. char addr_str[128];
  56. int addr_family = (int)pvParameters;
  57. int ip_protocol = 0;
  58. int keepAlive = 1;
  59. int keepIdle = KEEPALIVE_IDLE;
  60. int keepInterval = KEEPALIVE_INTERVAL;
  61. int keepCount = KEEPALIVE_COUNT;
  62. struct sockaddr_storage dest_addr;
  63. if (addr_family == AF_INET) {
  64. struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
  65. dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
  66. dest_addr_ip4->sin_family = AF_INET;
  67. dest_addr_ip4->sin_port = htons(PORT);
  68. ip_protocol = IPPROTO_IP;
  69. }
  70. #ifdef CONFIG_EXAMPLE_IPV6
  71. else if (addr_family == AF_INET6) {
  72. struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&dest_addr;
  73. bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un));
  74. dest_addr_ip6->sin6_family = AF_INET6;
  75. dest_addr_ip6->sin6_port = htons(PORT);
  76. ip_protocol = IPPROTO_IPV6;
  77. }
  78. #endif
  79. int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  80. if (listen_sock < 0) {
  81. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  82. vTaskDelete(NULL);
  83. return;
  84. }
  85. int opt = 1;
  86. setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  87. #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
  88. // Note that by default IPV6 binds to both protocols, it is must be disabled
  89. // if both protocols used at the same time (used in CI)
  90. setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
  91. #endif
  92. ESP_LOGI(TAG, "Socket created");
  93. int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  94. if (err != 0) {
  95. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  96. ESP_LOGE(TAG, "IPPROTO: %d", addr_family);
  97. goto CLEAN_UP;
  98. }
  99. ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  100. err = listen(listen_sock, 1);
  101. if (err != 0) {
  102. ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
  103. goto CLEAN_UP;
  104. }
  105. while (1) {
  106. ESP_LOGI(TAG, "Socket listening");
  107. struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
  108. socklen_t addr_len = sizeof(source_addr);
  109. int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
  110. if (sock < 0) {
  111. ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
  112. break;
  113. }
  114. // Set tcp keepalive option
  115. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
  116. setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int));
  117. setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
  118. setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
  119. // Convert ip address to string
  120. if (source_addr.ss_family == PF_INET) {
  121. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
  122. }
  123. #ifdef CONFIG_EXAMPLE_IPV6
  124. else if (source_addr.ss_family == PF_INET6) {
  125. inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
  126. }
  127. #endif
  128. ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
  129. do_retransmit(sock);
  130. shutdown(sock, 0);
  131. close(sock);
  132. }
  133. CLEAN_UP:
  134. close(listen_sock);
  135. vTaskDelete(NULL);
  136. }
  137. void app_main(void)
  138. {
  139. ESP_ERROR_CHECK(nvs_flash_init());
  140. ESP_ERROR_CHECK(esp_netif_init());
  141. ESP_ERROR_CHECK(esp_event_loop_create_default());
  142. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  143. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  144. * examples/protocols/README.md for more information about this function.
  145. */
  146. ESP_ERROR_CHECK(example_connect());
  147. #ifdef CONFIG_EXAMPLE_IPV4
  148. xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL);
  149. #endif
  150. #ifdef CONFIG_EXAMPLE_IPV6
  151. xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET6, 5, NULL);
  152. #endif
  153. }