lwip_demo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-24 ChungHsuan improve code comments
  9. */
  10. #include <rtthread.h>
  11. #include <string.h>
  12. #if !defined(SAL_USING_POSIX)
  13. #error "Please enable SAL_USING_POSIX!"
  14. #else
  15. #include <sys/time.h>
  16. #include <sys/select.h>
  17. #endif
  18. #include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
  19. #include "netdb.h"
  20. #define DEBUG_TCP_CLIENT
  21. #define DBG_TAG "TCP"
  22. #ifdef DEBUG_TCP_CLIENT
  23. #define DBG_LVL DBG_LOG
  24. #else
  25. #define DBG_LVL DBG_INFO /* DBG_ERROR */
  26. #endif
  27. #include <rtdbg.h>
  28. #include "lwip_demo.h"
  29. #ifdef SAM_LWIP_EXAMPLE
  30. #define BUFSZ 1024
  31. static int started = 0;
  32. static int is_running = 0;
  33. static char url[256] = "www.baidu.com";
  34. static int port = 8080;
  35. static const char send_data[] = "This is TCP Client from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
  36. /**
  37. * @brief This function is for creating a tcp client on RT-Thread
  38. */
  39. static void tcpclient(void *arg)
  40. {
  41. int ret;
  42. char *recv_data;
  43. int bytes_received;
  44. int sock = -1;
  45. struct hostent *host = RT_NULL;
  46. struct sockaddr_in server_addr;
  47. struct timeval timeout;
  48. fd_set readset;
  49. /* Get host address by parameter url(Domain name resolution if input domain) */
  50. /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
  51. host = gethostbyname(url);
  52. if (host == RT_NULL)
  53. {
  54. LOG_E("Get host by name failed!");
  55. return;
  56. }
  57. /* Allocate space for recv_data */
  58. /* 分配用于存放接收数据的缓冲 */
  59. recv_data = rt_malloc(BUFSZ);
  60. if (recv_data == RT_NULL)
  61. {
  62. LOG_E("No memory");
  63. return;
  64. }
  65. /* Create a socket and set it to SOCK_STREAM(TCP) */
  66. /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
  67. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  68. {
  69. /* Failed on creatinf socket */
  70. /* 创建socket失败 */
  71. LOG_E("Create socket error");
  72. goto __exit;
  73. }
  74. /* Initialize server side address */
  75. /* 初始化预连接的服务端地址 */
  76. server_addr.sin_family = AF_INET;
  77. server_addr.sin_port = htons(port);
  78. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  79. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  80. /* Connect to server */
  81. /* 连接到服务端 */
  82. if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  83. {
  84. /*Failed on connecting to server*/
  85. /* 连接失败 */
  86. LOG_E("Connect fail!");
  87. goto __exit;
  88. }
  89. started = 1;
  90. is_running = 1;
  91. timeout.tv_sec = 3;
  92. timeout.tv_usec = 0;
  93. while (is_running)
  94. {
  95. FD_ZERO(&readset);
  96. FD_SET(sock, &readset);
  97. /* Wait for read */
  98. if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
  99. continue;
  100. /* Receive the maximum size 1024 bytes from socket */
  101. /* 从sock连接中接收最大BUFSZ - 1字节数据 */
  102. bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
  103. if (bytes_received < 0)
  104. {
  105. /* Receive failed and close the connection */
  106. /* 接收失败,关闭这个连接 */
  107. LOG_E("Received error, close the socket.");
  108. goto __exit;
  109. }
  110. else if (bytes_received == 0)
  111. {
  112. /* Print warning message when recv function return 0 */
  113. /* 打印recv函数返回值为0的警告信息 */
  114. LOG_W("Received warning, recv function return 0.");
  115. continue;
  116. }
  117. else
  118. {
  119. /* Receive data sucessfully and append '\0' at the end of message */
  120. /* 有接收到数据,把末端清零 */
  121. recv_data[bytes_received] = '\0';
  122. if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
  123. {
  124. /* If the first letter is 'q' or 'Q', close the connection */
  125. /* 如果是首字母是q或Q,关闭这个连接 */
  126. LOG_I("Got a 'q' or 'Q', close the socket.");
  127. goto __exit;
  128. }
  129. else
  130. {
  131. /* Show the message in terminal */
  132. /* 在控制终端显示收到的数据 */
  133. LOG_D("Received data = %s", recv_data);
  134. }
  135. }
  136. /* Send message to connected socket */
  137. /* 发送数据到sock连接 */
  138. ret = send(sock, send_data, rt_strlen(send_data), 0);
  139. if (ret < 0)
  140. {
  141. /* Send failed, close the connection */
  142. /* 发送失败,关闭这个连接 */
  143. LOG_I("send error, close the socket.");
  144. goto __exit;
  145. }
  146. else if (ret == 0)
  147. {
  148. /* Print warning message when send function return 0 */
  149. /* 打印send函数返回值为0的警告信息 */
  150. LOG_W("Send warning, send function return 0.");
  151. }
  152. }
  153. __exit:
  154. if (recv_data)
  155. {
  156. rt_free(recv_data);
  157. recv_data = RT_NULL;
  158. }
  159. if (sock >= 0)
  160. {
  161. closesocket(sock);
  162. sock = -1;
  163. }
  164. started = 0;
  165. is_running = 0;
  166. return;
  167. }
  168. /**
  169. * @brief Call this function will run LWIP example code.
  170. *
  171. * @note .
  172. *
  173. * @param None.
  174. *
  175. * @return RT_OK or -RT_ERROR.
  176. */
  177. rt_err_t lwip_demo_run(void)
  178. {
  179. rt_thread_t tid;
  180. tid = rt_thread_create("tcp_client",
  181. tcpclient, RT_NULL,
  182. 2048, RT_THREAD_PRIORITY_MAX/3, 20);
  183. if (tid != RT_NULL)
  184. {
  185. rt_thread_startup(tid);
  186. }
  187. return RT_EOK;
  188. }
  189. #endif
  190. /*@}*/