硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3847|回复: 0
收起左侧

[RL-TCPnet V7.X] 测试BSD Socket服务器closesocket时间和原始Socket的netTCP_Close时间

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2021-1-16 09:51:35 | 显示全部楼层 |阅读模式

BSD Socket时间测试:


  1. /*
  2. *********************************************************************************************************
  3. *
  4. *        模块名称 : TCPnet网络协议栈测试
  5. *        文件名称 : app_tcpnet_lib.c
  6. *        版    本 : V1.0
  7. *        说    明 : 测试的功能说明
  8. *              1. 强烈推荐将网线接到路由器或者交换机上面测试,因为已经使能了DHCP,可以自动获取IP地址。
  9. *              2. 本例程创建了一个socket服务器,采用的TCP通信协议,而且使能了局域网域名NetBIOS,用户只
  10. *                 需在电脑端ping armfly就可以获得板子的IP地址,本地端口被设置为1024。
  11. *              3. 用户可以在电脑端用网络调试软件创建TCP Client连接此服务器。
  12. *              4. 网络调试助手发送命令字符1,板子回复字符1到8以及回车和换行两个字符,共10个。
  13. *              5. 网络调试助手发送命令字符2,板子回复1024个字符,前4个字符是abcd,最后4个字符是efgh,
  14. *                 中间的1016个全部是字符0。
  15. *
  16. *        修改记录 :
  17. *                版本号   日期         作者        说明
  18. *                V1.0    2017-04-17   Eric2013     首发
  19. *
  20. *        Copyright (C), 2015-2020, 安富莱电子 www.armfly.com
  21. *
  22. *********************************************************************************************************
  23. */                        
  24. #include "includes.h"        



  25. /*
  26. *********************************************************************************************************
  27. *                                          用于本文件的调试
  28. *********************************************************************************************************
  29. */
  30. #if 1
  31.         #define printf_debug printf
  32. #else
  33.         #define printf_debug(...)
  34. #endif


  35. /*
  36. *********************************************************************************************************
  37. *                                  宏定义,本地端口
  38. *********************************************************************************************************
  39. */
  40. /* 这个是本地端口 */
  41. #define LocalPort_NUM    1001


  42. /*
  43. *********************************************************************************************************
  44. *                                             变量
  45. *********************************************************************************************************
  46. */
  47. /* RL-TCPnet API的返回值 */
  48. const char * ReVal_Table[]=
  49. {
  50.         " 0: SCK_SUCCESS       Success                             ",
  51.         "-1: SCK_ERROR         General Error                       ",
  52.         "-2: SCK_EINVALID      Invalid socket descriptor           ",
  53.         "-3: SCK_EINVALIDPARA  Invalid parameter                   ",
  54.         "-4: SCK_EWOULDBLOCK   It would have blocked.              ",
  55.         "-5: SCK_EMEMNOTAVAIL  Not enough memory in memory pool    ",
  56.         "-6: SCK_ECLOSED       Connection is closed or aborted     ",
  57.         "-7: SCK_ELOCKED       Socket is locked in RTX environment ",
  58.         "-8: SCK_ETIMEOUT      Socket, Host Resolver timeout       ",
  59.         "-9: SCK_EINPROGRESS   Host Name resolving in progress     ",
  60.         "-10: SCK_ENONAME      Host Name not existing              ",
  61. };

  62. uint8_t sendbuf[1024];


  63. /*
  64. *********************************************************************************************************
  65. *        函 数 名: TCPnetTest
  66. *        功能说明: TCPnet应用
  67. *        形    参: 无
  68. *        返 回 值: 无
  69. *********************************************************************************************************
  70. */
  71. void TCPnetTest(void)
  72. {  
  73.         char dbuf[10];
  74.         int len;
  75.         int sock, sd, res;
  76.         SOCKADDR_IN addr;
  77.         SOCKADDR_IN ReAddr;
  78.    
  79.     uint32_t tick1;
  80.         uint32_t tick2, i=0;

  81.                 /* 创建一个socket
  82.                    第1个参数AF_INET:当前仅支持这个类型的地址族。
  83.                    第2个参数SOCK_STREAM:表示数据流通信类型,即使用的TCP。
  84.                    第3个参数0 :配置为0的话,自动跟第2个参数进行协议匹配,这里就是TCP协议。
  85.                 */
  86.                 sock = socket (AF_INET, SOCK_STREAM, 0);

  87.                 /* 端口号设置为1001 */
  88.                 addr.sin_port        = htons(LocalPort_NUM);
  89.                
  90.                 /* 与函数socket中的AF_INET作用一样 */
  91.                 addr.sin_family      = PF_INET;
  92.                 /*
  93.                    INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或所有地址,
  94.                    任意地址。用在这里的话就表示监控端口号为ddr.sin_port的所有IP地址消息。一般主要用
  95.                    于有多个网卡或者IP地址的情况。开发板只用了DM9161的网口,就是监听这个网口的IP地址。
  96.                 */
  97.                 addr.sin_addr.s_addr = INADDR_ANY;
  98.                
  99.                 /* 给socket绑定IP和端口号 */
  100.                 bind (sock, (SOCKADDR *)&addr, sizeof(addr));

  101.                 /* 设置监听,最大监听1个连接 */
  102.                 listen (sock, 1);
  103.                
  104.                 /*
  105.                    等待soket连接请求,有的话,自动创建1个新的socket进行连接通信,没有的话,等待连接。
  106.                    注意,能够accept的个数受到listen函数的限制,而listen函数又受到Net_Config.c中宏定义
  107.                    BSD_NUMSOCKS 的限制。
  108.                 */
  109.                 len = sizeof(ReAddr);
  110.     while (1)
  111.         {
  112.         if ((sd = accept(sock, (SOCKADDR *)&ReAddr, &len)) < 0) {
  113.        continue; // Error
  114.     }
  115. //                printf_debug ("远程客户端请求连接IP: %d.%d.%d.%d\n", ReAddr.sin_addr.s_b1,
  116. //                                                             ReAddr.sin_addr.s_b2,
  117. //                                                                                                                         ReAddr.sin_addr.s_b3,
  118. //                                                             ReAddr.sin_addr.s_b4);
  119. //                printf_debug ("远程客户端端口号: %d\n", ntohs(ReAddr.sin_port));
  120. //               
  121. //                /* 关闭监听socket,这个监听socket是调用函数socket后自动创建的 */
  122. //                closesocket (sock);

  123.                
  124.                 while (1)
  125.                 {
  126.                         /*
  127.                           socket数据接收函数,如果recv工作在阻塞模式,使用这个函数注意以下事项:
  128.                           1. 此函数的溢出时间受到Net_Config.c中宏定义 BSD_RCVTOUT 的限制。溢出时间到会自动退出。
  129.                           2. 这个函数接收到一次数据包就会返回,大于或者小于设置的缓冲区大小都没有关系,如果数据量
  130.                              大于接收缓冲区大小,用户只需多次调用函数recv进行接收即可。
  131.                           3. 实际接收到数据大小通过判断此函数的返回值即可。
  132.                         */
  133.                         res = recv (sd, dbuf, sizeof(dbuf), 0);
  134.                         if (res <= 0)
  135.                         {
  136.                                 //printf_debug("退出接收函数,重新开始监听%s\r\n", ReVal_Table[abs(res)]);
  137.                                 break;
  138.                         }
  139.                         else
  140.                         {
  141.                                 printf_debug("Receive Data Length = %d\r\n", res);
  142.                                 switch(dbuf[0])
  143.                                 {
  144.                                         /* 字符命令 1 */
  145.                                         case '1':
  146.                                                 sendbuf[0] = '1';
  147.                                                 sendbuf[1] = '2';
  148.                                                 sendbuf[2] = '3';
  149.                                                 sendbuf[3] = '4';
  150.                                                 sendbuf[4] = '5';
  151.                                                 sendbuf[5] = '6';
  152.                                                 sendbuf[6] = '7';
  153.                                                 sendbuf[7] = '8';
  154.                                                 sendbuf[8] = '\r';
  155.                                                 sendbuf[9] = '\n';                                                
  156.                                                 res = send (sd, (char *)sendbuf, 10, 0);
  157.                                                 if (res < 0)
  158.                                                 {
  159.                                                         printf_debug("函数send发送数据失败\r\n");
  160.                                                 }
  161.                                                 else
  162.                                                 {
  163.                                                         printf_debug("函数send发送数据成功\r\n");                                                        
  164.                                                 }
  165.                                                 break;
  166.                                 
  167.                                         /* 其它数值不做处理 */
  168.                                         default:                     
  169.                                                 break;
  170.                                 }
  171.                         }

  172.                 }
  173.                
  174.                 /*
  175.                    溢出时间到,远程设备断开连接等,程序都会执行到这里,我们在这里关闭socket,
  176.                    程序返回到第一个大while循环的开头重新创建socket并监听。
  177.                 */
  178.         tick1 = osKernelGetTickCount();
  179.         closesocket (sd);
  180.         memset(&ReAddr, 0, sizeof(struct sockaddr_in));
  181.         tick2 = osKernelGetTickCount();
  182.         tick2 =  tick2  - tick1;
  183.         printf("------%d---------------- = %d ms\r\n", i++, tick2 );        
  184.         }
  185. }

  186. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/
复制代码



原始TCP Socket:

  1. /*
  2. *********************************************************************************************************
  3. *
  4. *        模块名称 : TCPnet网络协议栈测试
  5. *        文件名称 : app_tcpnet_lib.c
  6. *        版    本 : V1.0
  7. *        说    明 : 测试的功能说明
  8. *              1. 强烈推荐将网线接到路由器或者交换机上面测试,因为已经使能了DHCP,可以自动获取IP地址。
  9. *              2. 创建了一个TCP Server,而且使能了局域网域名NetBIOS,用户只需在电脑端ping armfly
  10. *                 就可以获得板子的IP地址,端口号1001。
  11. *              3. 用户可以在电脑端用网络调试软件创建TCP Client连接此服务器端。
  12. *              4. 按键K1按下,发送8字节的数据给TCP Client。
  13. *              5. 按键K2按下,发送1024字节的数据给TCP Client。
  14. *              6. 按键K3按下,发送5MB字节的数据给TCP Client。
  15. *
  16. *        修改记录 :
  17. *                版本号   日期         作者        说明
  18. *                V1.0    2019-10-22   Eric2013     首发
  19. *
  20. *        Copyright (C), 2018-2030, 安富莱电子 www.armfly.com
  21. *
  22. *********************************************************************************************************
  23. */        
  24. #include "includes.h"        



  25. /*
  26. *********************************************************************************************************
  27. *                                          用于本文件的调试
  28. *********************************************************************************************************
  29. */
  30. #if 1
  31.         #define printf_debug printf
  32. #else
  33.         #define printf_debug(...)
  34. #endif


  35. /*
  36. *********************************************************************************************************
  37. *                                          宏定义
  38. *********************************************************************************************************
  39. */
  40. #define PORT_NUM       1001    /* TCP服务器监听端口号 */


  41. /*
  42. *********************************************************************************************************
  43. *                                             变量
  44. *********************************************************************************************************
  45. */
  46. int32_t tcp_sock;

  47. /* TCPnet API的返回值 */
  48. static const char * ReVal_Table[]=
  49. {
  50.         "netOK: Operation succeeded",
  51.         "netBusy: Process is busy",
  52.         "netError: Unspecified error",
  53.         "netInvalidParameter: Invalid parameter specified",
  54.         "netWrongState: Wrong state error",
  55.         "netDriverError: Driver error",
  56.         "netServerError: Server error",
  57.         "netAuthenticationFailed: User authentication failed",
  58.         "netDnsResolverError: DNS host resolver failed",
  59.         "netFileError: File not found or file r/w error",
  60.         "netTimeout: Operation timeout",
  61. };

  62. uint32_t tick1=0;
  63. uint32_t tick2=0, i=0;

  64. /*
  65. *********************************************************************************************************
  66. *        函 数 名: tcp_cb_server
  67. *        功能说明: TCP Socket的回调函数
  68. *        形    参: socket  句柄
  69. *             event   事件类型
  70. *             addr    NET_ADDR类型变量,记录IP地址,端口号。
  71. *             buf     ptr指向的缓冲区记录着接收到的TCP数据。
  72. *             len     记录接收到的数据个数。
  73. *        返 回 值:
  74. *********************************************************************************************************
  75. */
  76.     int flag = 0;
  77. uint32_t tcp_cb_server (int32_t socket, netTCP_Event event,
  78.                         const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
  79. {
  80.         switch (event)
  81.         {
  82.                 /*
  83.                         远程客户端连接消息
  84.                     1、数组ptr存储远程设备的IP地址,par中存储端口号。
  85.                     2、返回数值1允许连接,返回数值0禁止连接。
  86.                 */
  87.                 case netTCP_EventConnect:
  88.                         if (addr->addr_type == NET_ADDR_IP4)
  89.                         {
  90.                                 printf_debug("远程客户端请求连接IP: %d.%d.%d.%d  端口号:%d\r\n",
  91.                                                                                                                                 addr->addr[0],
  92.                                                                                                                                 addr->addr[1],
  93.                                                                                                                                 addr->addr[2],
  94.                                                                                                                                 addr->addr[3],                 
  95.                                                                                                                                 addr->port);
  96.                                 return (1);
  97.                         }
  98.                         else if (addr->addr_type == NET_ADDR_IP6)  
  99.                         {
  100.                                 return (1);
  101.                         }
  102.                         
  103.                         return(0);

  104.                 /* Socket远程连接已经建立 */
  105.                 case netTCP_EventEstablished:
  106.                         printf_debug("Socket is connected to remote peer\r\n");
  107.             flag = 1;
  108.             
  109.                         break;

  110.                 /* 连接断开 */
  111.                 case netTCP_EventClosed:
  112.                         printf_debug("Connection has been closed\r\n");
  113.                         break;

  114.                 /* 连接终止 */
  115.                 case netTCP_EventAborted:
  116.             printf_debug("Connection has been Aborted\r\n");
  117.                         break;

  118.                 /* 发送的数据收到远程设备应答 */
  119.                 case netTCP_EventACK:
  120.                         break;

  121.                 /* 接收到TCP数据帧,ptr指向数据地址,par记录数据长度,单位字节 */
  122.                 case netTCP_EventData:
  123.                         printf_debug("Data length = %d\r\n", len);
  124.                         printf ("%.*s\r\n",len, buf);
  125.                         break;
  126.         }
  127.         return (0);
  128. }

  129. /*
  130. *********************************************************************************************************
  131. *        函 数 名: TCPnetTest
  132. *        功能说明: TCPnet应用
  133. *        形    参: 无
  134. *        返 回 值: 无
  135. *********************************************************************************************************
  136. */   
  137. void TCPnetTest(void)
  138. {
  139.         int32_t iCount;
  140.         uint8_t *sendbuf;
  141.         uint32_t maxlen;
  142.         netStatus res;
  143.         const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
  144.         uint32_t EvtFlag;
  145.     netTCP_State state;
  146.         
  147.         tcp_sock = netTCP_GetSocket (tcp_cb_server);
  148.         
  149.         if (tcp_sock > 0)
  150.         {
  151.                 res = netTCP_Listen (tcp_sock, PORT_NUM);
  152.                 printf_debug("tcp listen res = %s\r\n", ReVal_Table[res]);
  153.                
  154.                 /* 使能TCP_TYPE_KEEP_ALIVE,会一直保持连接 */
  155.                 netTCP_SetOption (tcp_sock, netTCP_OptionKeepAlive, 1);
  156.         }
  157.         

  158.         while (1)
  159.         {
  160.         if(flag == 1)
  161.         {
  162.             flag= 0;
  163.             netTCP_State state;
  164.         osDelay(100);
  165.         tick1 = osKernelGetTickCount();   
  166.         // This TCP connection is no longer needed
  167.         netTCP_Close (tcp_sock);
  168.          
  169.         tick2 = osKernelGetTickCount();
  170.         tick2 =  tick2  - tick1;
  171.         printf("------%d---------------- = %d ms\r\n", i++, tick2 );        
  172.     }
  173.       osDelay(1);
  174.         }
  175. }

  176. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/

复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-28 20:11 , Processed in 0.149024 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表