udp_server.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 udp_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 sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  49. if (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(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  55. if (err < 0) {
  56. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  57. }
  58. ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  59. while (1) {
  60. ESP_LOGI(TAG, "Waiting for data");
  61. struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
  62. socklen_t socklen = sizeof(source_addr);
  63. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  64. // Error occurred during receiving
  65. if (len < 0) {
  66. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  67. break;
  68. }
  69. // Data received
  70. else {
  71. // Get the sender's ip address as string
  72. if (source_addr.sin6_family == PF_INET) {
  73. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  74. } else if (source_addr.sin6_family == PF_INET6) {
  75. inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  76. }
  77. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
  78. ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
  79. ESP_LOGI(TAG, "%s", rx_buffer);
  80. int err = sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
  81. if (err < 0) {
  82. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  83. break;
  84. }
  85. }
  86. }
  87. if (sock != -1) {
  88. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  89. shutdown(sock, 0);
  90. close(sock);
  91. }
  92. }
  93. vTaskDelete(NULL);
  94. }
  95. void app_main()
  96. {
  97. ESP_ERROR_CHECK(nvs_flash_init());
  98. tcpip_adapter_init();
  99. ESP_ERROR_CHECK(esp_event_loop_create_default());
  100. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  101. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  102. * examples/protocols/README.md for more information about this function.
  103. */
  104. ESP_ERROR_CHECK(example_connect());
  105. xTaskCreate(udp_server_task, "udp_server", 4096, NULL, 5, NULL);
  106. }