sys_tcpdemo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "includes.h"
  2. #include "lwip/timers.h"
  3. #include "lwip/tcp.h"
  4. #include "lwip/sockets.h"
  5. #include "string.h"
  6. //socket编程示例 需要操作系统
  7. //socket 客户端编程
  8. #define SERVER_PORT 30000
  9. #define SERVER_IP "192.168.0.104"
  10. void socket_client(void)
  11. {
  12. int sock = -1;
  13. struct sockaddr_in serverAddr;
  14. uint8_t send_buf[] = "This is a TCP client test ... \n";
  15. while(1)
  16. {
  17. sock = socket(AF_INET, SOCK_STREAM, 0);
  18. if(sock < 0)
  19. {
  20. OSTimeDly(100);
  21. continue;
  22. }
  23. serverAddr.sin_family = AF_INET;
  24. serverAddr.sin_port = htons(SERVER_PORT);
  25. serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
  26. memset(&(serverAddr.sin_zero), 0, sizeof(serverAddr.sin_zero));
  27. if( connect(sock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr) )!= ERR_OK)
  28. {
  29. closesocket(sock);
  30. OSTimeDly(1000);
  31. continue;
  32. }
  33. while(1)
  34. {
  35. if(write(sock, send_buf, sizeof(send_buf)) < 0) break;
  36. OSTimeDly(2000);
  37. }
  38. closesocket(sock);
  39. }
  40. }
  41. //server 编程
  42. uint8_t recvdata[256];
  43. void sock_tcpServer(void)
  44. {
  45. int sock = -1;
  46. int connected = -1;
  47. struct sockaddr_in server_addr, client_addr;
  48. socklen_t sin_size;
  49. int recv_data_len;
  50. sock = socket(AF_INET, SOCK_STREAM, 0);
  51. if(sock < 0) goto __exit;
  52. server_addr.sin_family = AF_INET;
  53. server_addr.sin_addr.s_addr = INADDR_ANY;
  54. server_addr.sin_port = htons(SERVER_PORT);
  55. memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  56. if(bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) goto __exit;
  57. if(listen(sock, 5) == -1) goto __exit;
  58. while(1)
  59. {
  60. sin_size = sizeof(struct sockaddr_in);
  61. connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
  62. printf("new client connected from(%s, %d) \n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
  63. while(1)
  64. {
  65. memset(recvdata, 0, 256);
  66. recv_data_len = recv(connected, recvdata, 256, 0);
  67. if(recv_data_len <= 0) break;
  68. printf("recv a data: %s \n", (char *)recvdata);
  69. write(connected, recvdata, recv_data_len);
  70. }
  71. if(connected > 0) closesocket(connected);
  72. connected = -1;
  73. }
  74. __exit:
  75. if(sock >= 0) closesocket(sock);
  76. }
  77. /*RAW 编程示例, 只可以在无操作系统的情况下使用*/
  78. /*
  79. static err_t tcpecho_newrecv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  80. {
  81. if(p != NULL)
  82. {
  83. tcp_recved(tpcb, p->tot_len);
  84. tcp_write(tpcb, p->payload, p->len, TCP_WRITE_FLAG_COPY);
  85. printf("receive a data: %s \n", (char *)p->payload);
  86. memset(p->payload, 0, p->len);
  87. pbuf_free(p);
  88. }
  89. else if(err == ERR_OK)
  90. {
  91. printf("client close \n");
  92. return tcp_close(tpcb);
  93. }
  94. return ERR_OK;
  95. }
  96. static err_t tcpecho_newtcp(void *arg, struct tcp_pcb *newpcb, err_t err)
  97. {
  98. tcp_recv(newpcb, tcpecho_newrecv);
  99. printf("connect new client \n");
  100. return ERR_OK;
  101. }
  102. void tcp_serverInit(void)
  103. {
  104. struct tcp_pcb *pcb = NULL;
  105. ip_addr_t local_ip;
  106. IP4_ADDR(&local_ip, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  107. pcb = tcp_new();
  108. tcp_bind(pcb, &local_ip, 2000);
  109. pcb = tcp_listen(pcb);
  110. tcp_accept(pcb, tcpecho_newtcp);
  111. }*/
  112. /* RAW TCP CLIENT
  113. #define TCP_CLIENT_PORT 40000
  114. #define TCP_SERVER_PORT 40001
  115. static err_t TCPClientConnected(void *arg, struct tcp_pcb *pcb, err_t err);
  116. static void TCPClientConnectError(void *arg, err_t err);
  117. static err_t TCPClientCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_recv_pbuf, err_t err);
  118. void Tcp_Client_Initialization(void)
  119. {
  120. struct tcp_pcb *tcp_client_pcb;
  121. ip_addr_t ipaddr;
  122. IP4_ADDR(&ipaddr, 192,168,0,187);
  123. tcp_client_pcb = tcp_new();
  124. tcp_bind(tcp_client_pcb, IP_ADDR_ANY, TCP_CLIENT_PORT);
  125. if (tcp_client_pcb != NULL)
  126. {
  127. tcp_err(tcp_client_pcb, TCPClientConnectError);
  128. tcp_connect(tcp_client_pcb, &ipaddr, TCP_SERVER_PORT, TCPClientConnected);
  129. }
  130. }
  131. static err_t TCPClientConnected(void *arg, struct tcp_pcb *pcb, err_t err)
  132. {
  133. char clientString[]="This is a new client connection.\r\n";
  134. tcp_recv(pcb, TCPClientCallback);
  135. tcp_write(pcb,clientString, strlen(clientString),0);
  136. return ERR_OK;
  137. }
  138. static void TCPClientConnectError(void *arg, err_t err)
  139. {
  140. Tcp_Client_Initialization();
  141. }
  142. static err_t TCPClientCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_recv_pbuf, err_t err)
  143. {
  144. struct pbuf *tcp_send_pbuf;
  145. char echoString[]="This is the server content echo:\r\n";
  146. if (tcp_recv_pbuf != NULL)
  147. {
  148. tcp_recved(pcb, tcp_recv_pbuf->tot_len);
  149. tcp_write(pcb,echoString, strlen(echoString), 1);
  150. tcp_send_pbuf = tcp_recv_pbuf;
  151. tcp_write(pcb, tcp_send_pbuf->payload, tcp_send_pbuf->len, 1);
  152. pbuf_free(tcp_recv_pbuf);
  153. }
  154. else if (err == ERR_OK)
  155. {
  156. tcp_close(pcb);
  157. Tcp_Client_Initialization();
  158. return ERR_OK;
  159. }
  160. return ERR_OK;
  161. }*/