tcp_client.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
  46. struct recev_packet
  47. {
  48. int length;
  49. char bytes[MAX_BUF_SIZE];
  50. };
  51. static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf);
  52. /*!
  53. \brief called when a data is received
  54. \param[in] conn: the TCP netconn over which to send data
  55. \param[in] buf: the received data
  56. \param[out] none
  57. \retval err_t: error value
  58. */
  59. static err_t tcp_client_recv(struct netconn *conn, struct netbuf *buf)
  60. {
  61. struct pbuf *q;
  62. struct recev_packet recev_packet;
  63. uint32_t data_len = 0;
  64. /* we perform here any necessary processing on the buf */
  65. if(NULL != buf){
  66. for(q = buf->p; q != NULL; q = q->next){
  67. /* if the received data size is larger than the size we want to get */
  68. if(q->len > (MAX_BUF_SIZE - data_len)){
  69. /* only copy MAX_BUF_SIZE bytes data */
  70. memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len));
  71. data_len = MAX_BUF_SIZE;
  72. }else{
  73. /* copy q->len bytes data */
  74. memcpy((recev_packet.bytes + data_len), q->payload, q->len);
  75. data_len += q->len;
  76. }
  77. if(data_len >= MAX_BUF_SIZE){
  78. break;
  79. }
  80. }
  81. recev_packet.length = data_len;
  82. netconn_write(conn, (void *)&(recev_packet.bytes), recev_packet.length, NETCONN_COPY);
  83. }
  84. return ERR_OK;
  85. }
  86. /*!
  87. \brief tcp_client task
  88. \param[in] arg: user supplied argument
  89. \param[out] none
  90. \retval none
  91. */
  92. static void tcp_client_task(void *arg)
  93. {
  94. struct netconn *conn;
  95. struct ip_addr ipaddr;
  96. struct netbuf *buf;
  97. err_t ret, recv_err;
  98. IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
  99. while(1){
  100. /* creat TCP connection */
  101. conn = netconn_new(NETCONN_TCP);
  102. /* bind the new netconn to any IP address and port 1026 */
  103. recv_err = netconn_bind(conn, IP_ADDR_ANY, 1026);
  104. if((ERR_USE != recv_err)&&(ERR_ISCONN != recv_err)){
  105. /* connect the new netconn to remote server and port 1026 */
  106. ret = netconn_connect(conn, &ipaddr, 1026);
  107. if(ERR_OK == ret) {
  108. recv_err = netconn_recv(conn, &buf);
  109. while(ERR_OK == recv_err){
  110. /* handle the received data */
  111. tcp_client_recv(conn, buf);
  112. netbuf_delete(buf);
  113. recv_err = netconn_recv(conn, &buf);
  114. }
  115. }
  116. }
  117. /* close connection and discard connection identifier */
  118. netconn_close(conn);
  119. netconn_delete(conn);
  120. }
  121. }
  122. #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */
  123. #if LWIP_SOCKET
  124. #include "lwip/sockets.h"
  125. #define SERVER_IP_ADDRESS "192.168.2.22"
  126. #define SERVER_PORT 8080
  127. #define MESSAGE "hello, world"
  128. static void tcp_client_task(void *arg)
  129. {
  130. int ret, sockfd = -1;
  131. struct sockaddr_in svr_addr;
  132. char buf[100];
  133. /* Create the socket outside the loop */
  134. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  135. if (sockfd < 0)
  136. {
  137. perror("Socket creation error");
  138. return;
  139. }
  140. struct sockaddr_in local_addr;
  141. local_addr.sin_family = AF_INET;
  142. local_addr.sin_port = htons(12345); // Set your desired local port
  143. local_addr.sin_addr.s_addr = INADDR_ANY;
  144. /* Bind the socket to a local address and port */
  145. if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
  146. {
  147. perror("Bind error");
  148. close(sockfd);
  149. return;
  150. }
  151. /* Set up the server address to connect to */
  152. svr_addr.sin_family = AF_INET;
  153. svr_addr.sin_port = htons(SERVER_PORT);
  154. inet_aton(SERVER_IP_ADDRESS, &(svr_addr.sin_addr));
  155. /* Connect to the server */
  156. ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  157. if (ret < 0)
  158. {
  159. perror("Connect error");
  160. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  161. }
  162. while (1)
  163. {
  164. /* Send "hello, world" to the server */
  165. ret = send(sockfd, MESSAGE, sizeof(MESSAGE) - 1, 0);
  166. if (ret <= 0)
  167. {
  168. perror("Send error");
  169. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  170. ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  171. {
  172. perror("Connect error");
  173. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  174. }
  175. continue;
  176. }
  177. /*
  178. //Receive response from the server (if needed)
  179. ret = recv(sockfd, buf, sizeof(buf), 0);
  180. if (ret <= 0)
  181. {
  182. perror("Receive error");
  183. vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  184. continue;
  185. }
  186. */
  187. /* Process the received data if needed */
  188. vTaskDelay(pdMS_TO_TICKS(5000)); // Delay for 5 seconds before the next iteration
  189. }
  190. /* Close the socket after the loop */
  191. close(sockfd);
  192. }
  193. #endif /* LWIP_SOCKET */
  194. /*!
  195. \brief initialize the tcp_client application
  196. \param[in] none
  197. \param[out] none
  198. \retval none
  199. */
  200. void tcp_client_init(void)
  201. {
  202. xTaskCreate(tcp_client_task, "TCP_CLIENT", DEFAULT_THREAD_STACKSIZE, NULL, TCP_CLIENT_TASK_PRIO, NULL);
  203. }