tcpclient.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include <string.h>
  11. #if !defined(SAL_USING_POSIX)
  12. #error "Please enable SAL_USING_POSIX!"
  13. #else
  14. #include <sys/time.h>
  15. #include <sys/select.h>
  16. #endif
  17. #include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
  18. #include "netdb.h"
  19. #define DEBUG_TCP_CLIENT
  20. #define DBG_TAG "TCP"
  21. #ifdef DEBUG_TCP_CLIENT
  22. #define DBG_LVL DBG_LOG
  23. #else
  24. #define DBG_LVL DBG_INFO /* DBG_ERROR */
  25. #endif
  26. #include <rtdbg.h>
  27. #define BUFSZ 1024
  28. static int started = 0;
  29. static int is_running = 0;
  30. static char url[256];
  31. static int port = 8080;
  32. static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
  33. static void tcpclient(void *arg)
  34. {
  35. int ret;
  36. char *recv_data;
  37. int bytes_received;
  38. int sock = -1;
  39. struct hostent *host = RT_NULL;
  40. struct sockaddr_in server_addr;
  41. struct timeval timeout;
  42. fd_set readset;
  43. /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
  44. host = gethostbyname(url);
  45. if (host == RT_NULL)
  46. {
  47. LOG_E("Get host by name failed!");
  48. return;
  49. }
  50. /* 分配用于存放接收数据的缓冲 */
  51. recv_data = rt_malloc(BUFSZ);
  52. if (recv_data == RT_NULL)
  53. {
  54. LOG_E("No memory");
  55. return;
  56. }
  57. /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
  58. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  59. {
  60. /* 创建socket失败 */
  61. LOG_E("Create socket error");
  62. goto __exit;
  63. }
  64. /* 初始化预连接的服务端地址 */
  65. server_addr.sin_family = AF_INET;
  66. server_addr.sin_port = htons(port);
  67. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  68. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  69. /* 连接到服务端 */
  70. if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  71. {
  72. /* 连接失败 */
  73. LOG_E("Connect fail!");
  74. goto __exit;
  75. }
  76. started = 1;
  77. is_running = 1;
  78. timeout.tv_sec = 3;
  79. timeout.tv_usec = 0;
  80. while (is_running)
  81. {
  82. FD_ZERO(&readset);
  83. FD_SET(sock, &readset);
  84. /* Wait for read */
  85. if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
  86. continue;
  87. /* 从sock连接中接收最大BUFSZ - 1字节数据 */
  88. bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
  89. if (bytes_received < 0)
  90. {
  91. /* 接收失败,关闭这个连接 */
  92. LOG_E("Received error, close the socket.");
  93. goto __exit;
  94. }
  95. else if (bytes_received == 0)
  96. {
  97. /* 打印recv函数返回值为0的警告信息 */
  98. LOG_W("Received warning, recv function return 0.");
  99. continue;
  100. }
  101. else
  102. {
  103. /* 有接收到数据,把末端清零 */
  104. recv_data[bytes_received] = '\0';
  105. if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
  106. {
  107. /* 如果是首字母是q或Q,关闭这个连接 */
  108. LOG_I("Got a 'q' or 'Q', close the socket.");
  109. goto __exit;
  110. }
  111. else
  112. {
  113. /* 在控制终端显示收到的数据 */
  114. LOG_D("Received data = %s", recv_data);
  115. }
  116. }
  117. /* 发送数据到sock连接 */
  118. ret = send(sock, send_data, rt_strlen(send_data), 0);
  119. if (ret < 0)
  120. {
  121. /* 发送失败,关闭这个连接 */
  122. LOG_I("send error, close the socket.");
  123. goto __exit;
  124. }
  125. else if (ret == 0)
  126. {
  127. /* 打印send函数返回值为0的警告信息 */
  128. LOG_W("Send warning, send function return 0.");
  129. }
  130. }
  131. __exit:
  132. if (recv_data)
  133. {
  134. rt_free(recv_data);
  135. recv_data = RT_NULL;
  136. }
  137. if (sock >= 0)
  138. {
  139. closesocket(sock);
  140. sock = -1;
  141. }
  142. started = 0;
  143. is_running = 0;
  144. return;
  145. }
  146. static void usage(void)
  147. {
  148. rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
  149. rt_kprintf(" tcpclient --stop\n");
  150. rt_kprintf(" tcpclient --help\n");
  151. rt_kprintf("\n");
  152. rt_kprintf("Miscellaneous:\n");
  153. rt_kprintf(" -h Specify host address\n");
  154. rt_kprintf(" -p Specify the host port number\n");
  155. rt_kprintf(" --stop Stop tcpclient program\n");
  156. rt_kprintf(" --help Print help information\n");
  157. }
  158. static void tcpclient_test(int argc, char** argv)
  159. {
  160. rt_thread_t tid;
  161. if (argc == 1 || argc > 5)
  162. {
  163. LOG_I("Please check the command you entered!\n");
  164. goto __usage;
  165. }
  166. else
  167. {
  168. if (rt_strcmp(argv[1], "--help") == 0)
  169. {
  170. goto __usage;
  171. }
  172. else if (rt_strcmp(argv[1], "--stop") == 0)
  173. {
  174. is_running = 0;
  175. return;
  176. }
  177. else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
  178. {
  179. if (started)
  180. {
  181. LOG_I("The tcpclient has started!");
  182. LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
  183. return;
  184. }
  185. if (rt_strlen(argv[2]) > sizeof(url))
  186. {
  187. LOG_E("The input url is too long, max %d bytes!", sizeof(url));
  188. return;
  189. }
  190. rt_memset(url, 0x0, sizeof(url));
  191. rt_strncpy(url, argv[2], rt_strlen(argv[2]));
  192. port = atoi(argv[4]);
  193. }
  194. else
  195. {
  196. goto __usage;
  197. }
  198. }
  199. tid = rt_thread_create("tcp_client",
  200. tcpclient, RT_NULL,
  201. 2048, RT_THREAD_PRIORITY_MAX/3, 20);
  202. if (tid != RT_NULL)
  203. {
  204. rt_thread_startup(tid);
  205. }
  206. return;
  207. __usage:
  208. usage();
  209. }
  210. #ifdef RT_USING_FINSH
  211. MSH_CMD_EXPORT_ALIAS(tcpclient_test, tcpclient,
  212. Start a tcp client. Help: tcpclient --help);
  213. #endif