tcp_client.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*!
  2. \file tcp_client.c
  3. \brief TCP client demo program
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "lwip/opt.h"
  33. #include "lwip/sys.h"
  34. #include "tcp_client.h"
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include "gd32f30x.h"
  38. #include "main.h"
  39. #include "lwip/tcp.h"
  40. #include "lwip/memp.h"
  41. #include "lwip/api.h"
  42. #define TCP_CLIENT_TASK_PRIO ( tskIDLE_PRIORITY + 5)
  43. #define MAX_BUF_SIZE 50
  44. #define TIME_WAITING_FOR_CONNECT ( ( portTickType ) 500 )
  45. uint8_t load_tcp_config = 0;
  46. #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
  47. struct recev_packet
  48. {
  49. int length;
  50. char bytes[MAX_BUF_SIZE];
  51. };
  52. static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf);
  53. /*!
  54. \brief called when a data is received
  55. \param[in] conn: the TCP netconn over which to send data
  56. \param[in] buf: the received data
  57. \param[out] none
  58. \retval err_t: error value
  59. */
  60. static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf)
  61. {
  62. struct pbuf *q;
  63. struct recev_packet recev_packet;
  64. uint32_t data_len = 0;
  65. /* we perform here any necessary processing on the buf */
  66. if(NULL != buf){
  67. for(q = buf->p; q != NULL; q = q->next){
  68. /* if the received data size is larger than the size we want to get */
  69. if(q->len > (MAX_BUF_SIZE - data_len)){
  70. /* only copy MAX_BUF_SIZE bytes data */
  71. memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len));
  72. data_len = MAX_BUF_SIZE;
  73. }else{
  74. /* copy q->len bytes data */
  75. memcpy((recev_packet.bytes + data_len), q->payload, q->len);
  76. data_len += q->len;
  77. }
  78. if(data_len >= MAX_BUF_SIZE){
  79. break;
  80. }
  81. }
  82. recev_packet.length = data_len;
  83. netconn_write(conn, (void *)&(recev_packet.bytes), recev_packet.length, NETCONN_COPY);
  84. }
  85. return ERR_OK;
  86. }
  87. /*!
  88. \brief tcp_client task
  89. \param[in] arg: user supplied argument
  90. \param[out] none
  91. \retval none
  92. */
  93. static void tcp_client_task(void *arg)
  94. {
  95. struct netconn *conn;
  96. struct ip_addr ipaddr;
  97. struct netbuf *buf;
  98. err_t ret, recv_err;
  99. IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
  100. while(1){
  101. /* creat TCP connection */
  102. conn = netconn_new(NETCONN_TCP);
  103. /* bind the new netconn to any IP address and port 1026 */
  104. recv_err = netconn_bind(conn, IP_ADDR_ANY, 1026);
  105. if((ERR_USE != recv_err)&&(ERR_ISCONN != recv_err)){
  106. /* connect the new netconn to remote server and port 1026 */
  107. ret = netconn_connect(conn, &ipaddr, 1026);
  108. if(ERR_OK == ret) {
  109. recv_err = netconn_recv(conn, &buf);
  110. while(ERR_OK == recv_err){
  111. /* handle the received data */
  112. tcp_client_recv(conn, buf);
  113. netbuf_delete(buf);
  114. recv_err = netconn_recv(conn, &buf);
  115. }
  116. }
  117. }
  118. /* close connection and discard connection identifier */
  119. netconn_close(conn);
  120. netconn_delete(conn);
  121. }
  122. }
  123. #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */
  124. int lenth1,lenth2;
  125. #if LWIP_SOCKET
  126. #include "lwip/sockets.h"
  127. #include "netconf.h"
  128. #include "gd32_flash.h"
  129. #define SERVER_IP_ADDRESS "192.168.0.101"
  130. #define SERVER_PORT 8080
  131. #define MESSAGE "hello, world"
  132. static void tcp_client_task(void *arg)
  133. {
  134. int ret, sockfd = -1;
  135. struct sockaddr_in svr_addr;
  136. /* Create the socket outside the loop */
  137. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  138. if (sockfd < 0)
  139. {
  140. perror("Socket creation error");
  141. return;
  142. }
  143. struct sockaddr_in local_addr;
  144. local_addr.sin_family = AF_INET;
  145. local_addr.sin_port = htons(12345); // Set your desired local port
  146. local_addr.sin_addr.s_addr = INADDR_ANY;
  147. /* Bind the socket to a local address and port */
  148. if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
  149. {
  150. perror("Bind error");
  151. close(sockfd);
  152. return;
  153. }
  154. /* Set up the server address to connect to */
  155. svr_addr.sin_family = AF_INET;
  156. svr_addr.sin_port = htons(SERVER_PORT);
  157. inet_aton(SERVER_IP_ADDRESS, &(svr_addr.sin_addr));
  158. while(dhcp_done!=1)
  159. {
  160. vTaskDelay(100);
  161. }
  162. /* Connect to the server */
  163. ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  164. if (ret < 0)
  165. {
  166. perror("Connect error");
  167. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  168. }
  169. while (1)
  170. {
  171. /* Send "hello, world" to the server */
  172. ret = send(sockfd, MESSAGE, sizeof(MESSAGE) - 1, 0);
  173. if (ret <= 0)
  174. {
  175. perror("Send error");
  176. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  177. ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  178. {
  179. perror("Connect error");
  180. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  181. }
  182. continue;
  183. }
  184. //Receive response from the server (if needed)
  185. char buf[10];
  186. ret = recv(sockfd, buf, sizeof(buf), 0);
  187. /*
  188. char buf[1460];// 接收一包TCP数据
  189. char* dataBuf = pvPortMalloc(3 * 1024);// 最大20K,暂定3K
  190. memset(dataBuf,0,strlen(dataBuf));
  191. sprintf(dataBuf,"tcp_config");
  192. do{
  193. memset(buf, 0, sizeof(buf) + 4);
  194. ret = recv(sockfd, buf, sizeof(buf), 0);
  195. if (ret <= 0)
  196. {
  197. perror("Receive error");
  198. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  199. continue;
  200. }
  201. lenth1 = strlen(dataBuf);
  202. lenth2 = strlen(buf);
  203. memcpy(dataBuf + lenth1,buf,strlen(buf));
  204. lenth1 = strlen(dataBuf);
  205. }while(buf[1459] != NULL );
  206. save_config_params(dataBuf);
  207. vPortFree(dataBuf);
  208. */
  209. /* Process the received data if needed */
  210. vTaskDelay(pdMS_TO_TICKS(5000)); // Delay for 5 seconds before the next iteration
  211. }
  212. /* Close the socket after the loop */
  213. close(sockfd);
  214. }
  215. #endif /* LWIP_SOCKET */
  216. /*!
  217. \brief initialize the tcp_client application
  218. \param[in] none
  219. \param[out] none
  220. \retval none
  221. */
  222. void tcp_client_init(void)
  223. {
  224. xTaskCreate(tcp_client_task, "TCP_CLIENT", DEFAULT_THREAD_STACKSIZE, NULL, TCP_CLIENT_TASK_PRIO, NULL);
  225. }