tcp_client.c 4.0 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 "freertos/event_groups.h"
  12. #include "esp_system.h"
  13. #include "esp_wifi.h"
  14. #include "esp_event.h"
  15. #include "esp_log.h"
  16. #include "nvs_flash.h"
  17. #include "esp_netif.h"
  18. #include "protocol_examples_common.h"
  19. #include "addr_from_stdin.h"
  20. #include "lwip/err.h"
  21. #include "lwip/sockets.h"
  22. #if defined(CONFIG_EXAMPLE_IPV4)
  23. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
  24. #elif defined(CONFIG_EXAMPLE_IPV6)
  25. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
  26. #else
  27. #define HOST_IP_ADDR ""
  28. #endif
  29. #define PORT CONFIG_EXAMPLE_PORT
  30. static const char *TAG = "example";
  31. static const char *payload = "Message from ESP32 ";
  32. static void tcp_client_task(void *pvParameters)
  33. {
  34. char rx_buffer[128];
  35. char host_ip[] = HOST_IP_ADDR;
  36. int addr_family = 0;
  37. int ip_protocol = 0;
  38. while (1) {
  39. #if defined(CONFIG_EXAMPLE_IPV4)
  40. struct sockaddr_in dest_addr;
  41. dest_addr.sin_addr.s_addr = inet_addr(host_ip);
  42. dest_addr.sin_family = AF_INET;
  43. dest_addr.sin_port = htons(PORT);
  44. addr_family = AF_INET;
  45. ip_protocol = IPPROTO_IP;
  46. #elif defined(CONFIG_EXAMPLE_IPV6)
  47. struct sockaddr_in6 dest_addr = { 0 };
  48. inet6_aton(host_ip, &dest_addr.sin6_addr);
  49. dest_addr.sin6_family = AF_INET6;
  50. dest_addr.sin6_port = htons(PORT);
  51. dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
  52. addr_family = AF_INET6;
  53. ip_protocol = IPPROTO_IPV6;
  54. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  55. struct sockaddr_in6 dest_addr = { 0 };
  56. ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr));
  57. #endif
  58. int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  59. if (sock < 0) {
  60. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  61. break;
  62. }
  63. ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);
  64. int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
  65. if (err != 0) {
  66. ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
  67. break;
  68. }
  69. ESP_LOGI(TAG, "Successfully connected");
  70. while (1) {
  71. int err = send(sock, payload, strlen(payload), 0);
  72. if (err < 0) {
  73. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  74. break;
  75. }
  76. int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  77. // Error occurred during receiving
  78. if (len < 0) {
  79. ESP_LOGE(TAG, "recv failed: errno %d", errno);
  80. break;
  81. }
  82. // Data received
  83. else {
  84. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  85. ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
  86. ESP_LOGI(TAG, "%s", rx_buffer);
  87. }
  88. vTaskDelay(2000 / portTICK_PERIOD_MS);
  89. }
  90. if (sock != -1) {
  91. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  92. shutdown(sock, 0);
  93. close(sock);
  94. }
  95. }
  96. vTaskDelete(NULL);
  97. }
  98. void app_main(void)
  99. {
  100. ESP_ERROR_CHECK(nvs_flash_init());
  101. ESP_ERROR_CHECK(esp_netif_init());
  102. ESP_ERROR_CHECK(esp_event_loop_create_default());
  103. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  104. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  105. * examples/protocols/README.md for more information about this function.
  106. */
  107. ESP_ERROR_CHECK(example_connect());
  108. xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL);
  109. }