tcp_server.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "tcpip_adapter.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. static const char *TAG = "example";
  24. static void tcp_server_task(void *pvParameters)
  25. {
  26. char rx_buffer[128];
  27. char addr_str[128];
  28. int addr_family;
  29. int ip_protocol;
  30. while (1) {
  31. #ifdef CONFIG_EXAMPLE_IPV4
  32. struct sockaddr_in dest_addr;
  33. dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  34. dest_addr.sin_family = AF_INET;
  35. dest_addr.sin_port = htons(PORT);
  36. addr_family = AF_INET;
  37. ip_protocol = IPPROTO_IP;
  38. inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
  39. #else // IPV6
  40. struct sockaddr_in6 dest_addr;
  41. bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
  42. dest_addr.sin6_family = AF_INET6;
  43. dest_addr.sin6_port = htons(PORT);
  44. addr_family = AF_INET6;
  45. ip_protocol = IPPROTO_IPV6;
  46. inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  47. #endif
  48. int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  49. if (listen_sock < 0) {
  50. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  51. break;
  52. }
  53. ESP_LOGI(TAG, "Socket created");
  54. int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  55. if (err != 0) {
  56. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  57. break;
  58. }
  59. ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  60. err = listen(listen_sock, 1);
  61. if (err != 0) {
  62. ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
  63. break;
  64. }
  65. ESP_LOGI(TAG, "Socket listening");
  66. struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
  67. uint addr_len = sizeof(source_addr);
  68. int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
  69. if (sock < 0) {
  70. ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
  71. break;
  72. }
  73. ESP_LOGI(TAG, "Socket accepted");
  74. while (1) {
  75. int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  76. // Error occurred during receiving
  77. if (len < 0) {
  78. ESP_LOGE(TAG, "recv failed: errno %d", errno);
  79. break;
  80. }
  81. // Connection closed
  82. else if (len == 0) {
  83. ESP_LOGI(TAG, "Connection closed");
  84. break;
  85. }
  86. // Data received
  87. else {
  88. // Get the sender's ip address as string
  89. if (source_addr.sin6_family == PF_INET) {
  90. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  91. } else if (source_addr.sin6_family == PF_INET6) {
  92. inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  93. }
  94. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  95. ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
  96. ESP_LOGI(TAG, "%s", rx_buffer);
  97. int err = send(sock, rx_buffer, len, 0);
  98. if (err < 0) {
  99. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  100. break;
  101. }
  102. }
  103. }
  104. if (sock != -1) {
  105. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  106. shutdown(sock, 0);
  107. close(sock);
  108. }
  109. }
  110. vTaskDelete(NULL);
  111. }
  112. void app_main()
  113. {
  114. ESP_ERROR_CHECK(nvs_flash_init());
  115. tcpip_adapter_init();
  116. ESP_ERROR_CHECK(esp_event_loop_create_default());
  117. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  118. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  119. * examples/protocols/README.md for more information about this function.
  120. */
  121. ESP_ERROR_CHECK(example_connect());
  122. xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
  123. }