微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > 射频无线通信设计 > open1081 TCP UDP ECHO例程测试

open1081 TCP UDP ECHO例程测试

时间:10-02 整理:3721RD 点击:


IAR的例程不知道为什么打不开,这里使用的是MDK版本。
首先修改AP_NAME和AP_PASSWORD




启动后,程序自动连接无线网络,一旦连接上即回调socket_connected,不知道这个函数是怎么被回调的,没有看到注册这个函数的地方。
然后开始读取www.baidu.com的数据。



例程太乱,为了单纯测试TCP和UDP的echo功能,我将其他代码都去掉。只保留基本功能。
下面是我修改后的代码。
TCP监听8080端口,UDP监听8090端口

  1. #include "stdio.h"
  2. #include "ctype.h"

  3. #include "stm32f2xx.h"
  4. #include "platform.h"
  5. #include "mxchipWNET.h"


  6. /*
  7. Enable IEEE powersave mode,
  8. */
  9. //#define LowPowerMode

  10. /*
  11. Under this configuration, you can allocate every socket's send/recv buffer for application's requirement.
  12. The more buffer is allocated, faster the speed would be, but the max connection number would be less.
  13. If there is not enough memory for a connection:
  14. TCP client: function: connect would return fail
  15. TCP server: Any connection to the server would reset
  16. If the buffer is not setted, stack would use the default size: 2048bytes.
  17. */
  18. //#define DynamicMemAlloc
  19. #define AP_NAME "TP-604"
  20. #define AP_PASSWORD "robot604"
  21. #define WEB_SERVER "www.baidu.com"
  22. #define APP_INFO "Open1081: TCP[8080] UDP[8090] ECHO"

  23. //#define BlockMode

  24. network_InitTypeDef_st wNetConfig;
  25. lib_config_t libConfig;
  26. int wifi_up = 0;
  27. int webserverTest = 1;
  28. int cloud_ip_addr = -1;
  29. int cloud_connected = 0;
  30. int dns_pending = 0;
  31. extern u32 MS_TIMER;

  32. void print_msg(void);

  33. /* ========================================
  34. User provide callback functions
  35. ======================================== */
  36. void system_version(char *str, int len)
  37. {
  38. snprintf(str, len, "%s", APP_INFO);
  39. }

  40. void WifiStatusHandler(int event)
  41. {
  42. switch (event) {
  43. case MXCHIP_WIFI_UP:
  44. wifi_up = 1;
  45. printf("Station up \r\n");
  46. break;
  47. case MXCHIP_WIFI_DOWN:
  48. printf("Station down \r\n");
  49. break;
  50. case MXCHIP_UAP_UP:
  51. break;
  52. case MXCHIP_UAP_DOWN:
  53. break;
  54. default:
  55. break;
  56. }
  57. }

  58. void NetCallback(net_para_st *pnet)
  59. {
  60. printf("IP address: %s \r\n", pnet->ip);
  61. printf("NetMask address: %s \r\n", pnet->mask);
  62. printf("Gateway address: %s \r\n", pnet->gate);
  63. printf("DNS server address: %s \r\n", pnet->dns);
  64. printf("MAC address: %s \r\n", pnet->mac);
  65. }

  66. //#ifndef BlockMode
  67. #if 0
  68. void dns_ip_set(u8 *hostname, u32 ip)
  69. {
  70. char ipstr[16];
  71. cloud_ip_addr = ip;
  72. dns_pending = 0;
  73. if(cloud_ip_addr == -1)
  74. printf("DNS test: %s failed \r\n", WEB_SERVER);
  75. else {
  76. inet_ntoa(ipstr, ip);
  77. printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr);
  78. }
  79. }

  80. void socket_connected(int fd)
  81. {
  82. printf("Connect to web server success! Reading web pages...\r\n");
  83. cloud_connected = 1;
  84. }
  85. #endif


  86. int main(void)
  87. {
  88. int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1;
  89. char *buf, ip_address[16];
  90. int len;
  91. int con = -1;

  92. int clientfd[8];
  93. fd_set readfds, exceptfds;
  94. struct timeval_t t;
  95. struct sockaddr_t addr;
  96. socklen_t addrLen;

  97. int bufferSize;
  98. libConfig.tcp_buf_dynamic = mxEnable;
  99. libConfig.tcp_max_connection_num = 12;
  100. libConfig.tcp_rx_size = 2048;
  101. libConfig.tcp_tx_size = 2048;
  102. libConfig.hw_watchdog = 0;
  103. libConfig.wifi_channel = WIFI_CHANNEL_1_13;
  104. lib_config(&libConfig);

  105. for(i = 0; i < 8; i++)
  106. clientfd[i] = -1;

  107. buf = (char*)malloc(3*1024);

  108. mxchipInit();
  109. UART_Init();

  110. printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());

  111. ps_enable();

  112. memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
  113. wNetConfig.wifi_mode = Station;
  114. strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
  115. strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
  116. wNetConfig.dhcpMode = DHCP_Client;
  117. StartNetwork(&wNetConfig);
  118. printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid);

  119. t.tv_sec = 0;
  120. t.tv_usec = 100;

  121. set_tcp_keepalive(3, 60);

  122. for (;;) {

  123. mxchipTick();

  124. /* Establish a TCP server that accept the tcp clients connections */
  125. if (fd_listen == -1) {

  126. fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  127. bufferSize = 5*1024;
  128. setsockopt(fd_listen, 0, SO_RDBUFLEN, &bufferSize, 4);
  129. bufferSize = 5*1024;
  130. setsockopt(fd_listen, 0, SO_WRBUFLEN, &bufferSize, 4);
  131. addr.s_port = 8080;
  132. bind(fd_listen, &addr, sizeof(addr));
  133. listen(fd_listen, 0);
  134. printf("TCP server established at port: %d \r\n", addr.s_port);
  135. }

  136. /* Establish a UDP port to receive any data sent to this port */
  137. if (fd_udp == -1) {
  138. fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
  139. addr.s_port = 8090;
  140. bind(fd_udp, &addr, sizeof(addr));
  141. printf("Open UDP port %d\r\n", addr.s_port);
  142. }

  143. /* Check status on erery sockets */
  144. FD_ZERO(&readfds);
  145. FD_SET(fd_listen, &readfds);
  146. FD_SET(fd_udp, &readfds);

  147. if(fd_client!=-1)
  148. FD_SET(fd_client, &readfds);

  149. for(i = 0; i < 8; i++) {
  150. if (clientfd[i] != -1)
  151. FD_SET(clientfd[i], &readfds);
  152. }

  153. select(1, &readfds, NULL, &exceptfds, &t);

  154. /* Check tcp connection requests */
  155. if(FD_ISSET(fd_listen, &readfds)) {
  156. j = accept(fd_listen, &addr, &len);
  157. if (j > 0) {
  158. inet_ntoa(ip_address, addr.s_ip);
  159. printf("Client %s:%d connected\r\n", ip_address, addr.s_port);

  160. for(i = 0; i < 8; i++) {
  161. if (clientfd[i] == -1) {
  162. clientfd[i] = j;
  163. break;
  164. }
  165. }
  166. }
  167. }

  168. /* Read data from tcp clients and send data back */
  169. for(i = 0; i < 8; i++) {
  170. if (clientfd[i] != -1) {
  171. if (FD_ISSET(clientfd[i], &readfds)) {
  172. con = recv(clientfd[i], buf, 1*1024, 0);
  173. if (con > 0)
  174. send(clientfd[i], buf, con, 0);
  175. else {
  176. close(clientfd[i]);
  177. clientfd[i] = -1;
  178. }
  179. }
  180. else if (FD_ISSET(clientfd[i], &exceptfds))
  181. clientfd[i] = -1;
  182. }
  183. }

  184. /* Read data from udp and send data back */
  185. if (FD_ISSET(fd_udp, &readfds)) {
  186. con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
  187. sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
  188. }
  189. }
  190. }

复制代码



从log中可以看到open1081的IP地址是192.168.1.107




TCP echo测试,发送12,返回12




UDP echo测试,发送df,回复df




通过这个简单的echo测试例程,我们了解了socket编程的基本知识。

这个怎么看不到?

带有外链的帖子一般需要经过后台审核的,平时会很快,周末我们有人值班,可能略慢一些。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top