小言_互联网的博客

HaaS100开发板局域网基本网络通信

424人阅读  评论(0)

1、概要

本文的目标:

1、在AliOS Things上如何连到指定wifi上

2、在AliOS Things上如何通过socket和连接在相同网络的pc机进行通信

通过这个实验,作为一个入门,能够使用aos以及HaaS开发板快速连接wifi并且和局域网进行通信,为进一步开发更多的网络相关应用打下基础。

本实验基于TCP实验,以PC机作为server,HaaS100开发板作为client,进行数据通信。

 

本文读者对象:

1、初次接触HaaS100开发板的开发者

2、需要联网的开发者

3、需要进行局域网通信的开发者

 

2、拓扑结构和基本流程原理

2.1、拓扑结构

 

2.2、基本流程原理:

3、实验效果

服务端监听连接,等待设备端连接后进行交互,服务端和设备端相互发送数据并接受数据

3.1、服务端

3.2、设备端

 

4、实验准备

该实验所有代码参见github链接:

https://github.com/alibaba/AliOS-Things/tree/dev_3.1.0_haas/application/example/tcp_demo

4.1、服务端

服务端的代码按基本流程是创建一个socket进行监听,然后等待client连接后进行用户输入交互。

代码如下:


  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. extern void communication(int fd);
  9. int main()
  10. {
  11. int serverfd, clientfd, len;
  12. struct sockaddr_in servaddr, clientaddr;
  13. memset(&servaddr, 0, sizeof(servaddr));
  14. memset(&clientaddr, 0, sizeof(clientaddr));
  15. serverfd = socket(AF_INET, SOCK_STREAM, 0);
  16. if (serverfd == -1) {
  17. printf( "socket called failed!\n");
  18. exit( 0);
  19. }
  20. else {
  21. printf( "socket successfully created..\n");
  22. }
  23. servaddr.sin_family = AF_INET;
  24. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  25. servaddr.sin_port = htons( 8080);
  26. //绑定server ip地址
  27. if ((bind(serverfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
  28. printf( "socket bind failed...\n");
  29. exit( 0);
  30. }
  31. else {
  32. printf( "Socket successfully binded..\n");
  33. }
  34. if ((listen(serverfd, 5)) != 0) {
  35. printf( "Listen failed...\n");
  36. exit( 0);
  37. }
  38. else
  39. printf( "Server listening..\n");
  40. len = sizeof(clientaddr);
  41. //等待设备连接
  42. clientfd = accept(serverfd, (struct sockaddr *)&clientaddr, &len);
  43. if (clientfd < 0) {
  44. printf( "acccept failed!\n");
  45. exit( 0);
  46. }
  47. else
  48. printf( "server acccept the client...\n");
  49. //主循环交流
  50. communication(clientfd);
  51. close(serverfd);
  52. }
  53. #define MAX_INPUT_CHAR 100
  54. void communication(int fd)
  55. {
  56. int index;
  57. char input[MAX_INPUT_CHAR];
  58. while ( 1) {
  59. memset(input, 0, MAX_INPUT_CHAR);
  60. index = 0;
  61. //等待client发送消息
  62. read(fd, input, sizeof(input));
  63. printf( "Receive from client: %s\n", input);
  64. printf( "Pluse input sending to client : ");
  65. memset(input, 0, MAX_INPUT_CHAR);
  66. while ((input[index++] = getchar()) != '\n')
  67. ;
  68. //将input发送给client
  69. write(fd, input, sizeof(input));
  70. if ( strncmp( "exit", input, 4) == 0) {
  71. printf( "Exit communication\n");
  72. break;
  73. }
  74. }
  75. }

编译命令:

gcc server.c -o s.o

执行命令:

./s.o

4.2、设备端

设备端作为client,主要流程是注册基本命令后进行连接服务端,并且介绍到服务端消息后回应receive报文。

设备端代码分为两个部分,一个是wifi连接初始化部分,一个是进行tcp模拟的部分。

wifi连接初始化代码:


  
  1. extern void handle_networktestcmd(char *pwbuf, int blen, int argc, char **argv);
  2. static struct cli_command networktestcmds[] = {
  3. {
  4. .name = "network",
  5. . help = "netowork { tcp_c|tcp_s remote_ip remote_port data [times] } | { domain domain_info [ remote_port ]}",
  6. . function = handle_networktestcmd
  7. }
  8. };
  9. int application_start(int argc, char *argv[])
  10. {
  11. printf( "%s-%d called\n", __FUNCTION__, __LINE__);
  12. wifi_service_init();
  13. wifi_service_wifi_connect( "ssid", "passwd", NULL, 10 * 1000);
  14. aos_cli_register_commands((const struct cli_command *)&networktestcmds[0], sizeof(networktestcmds) / sizeof(networktestcmds[0]));
  15. }
  16. void handle_networktestcmd(char *pwbuf, int blen, int argc, char **argv)
  17. {
  18. char *ptype = NULL;
  19. int ret = 0;
  20. aos_cli_init();
  21. printf( "tcp demo entry here!\r\n");
  22. if (argc < 2 || NULL == argv){
  23. printf( "invalid input netword test command argc %d argv %p \r\n", argc, argv);
  24. return;
  25. }
  26. ptype = argv[1];
  27. if (strcmp(ptype, "tcp_c") == 0) {
  28. ret = networktestcmd_tcp_client(argc, argv);
  29. if (ret){
  30. printf( "fail to execute tcp client test command \r\n");
  31. return;
  32. }
  33. } else if (strcmp(ptype, "tcp_s") == 0) {
  34. ret = networktestcmd_tcp_server(argc, argv);
  35. if (ret){
  36. printf( "fail to execute udp server test command \r\n");
  37. return;
  38. }
  39. } else {
  40. printf( "invalid netword test command input \r\n");
  41. }
  42. printf( "network command test successed \r\n");
  43. }

tcp部分代码:


  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <aos/kernel.h>
  5. #include "aos/init.h"
  6. #include "board.h"
  7. #include <k_api.h>
  8. #include "aos/kernel.h"
  9. #include <network/network.h>
  10. #include <netmgr.h>
  11. #include "aos/cli.h"
  12. #define BUFFER_MAX_SIZE 1512
  13. #define TCP_DEMO_TARGET_TCP_PORT 443
  14. #ifndef IPADDR_NONE
  15. #define IPADDR_NONE ((uint32_t)0xffffffffUL)
  16. #endif
  17. int networktestcmd_tcp_client(int argc, char **argv)
  18. {
  19. int ret = 0;
  20. int readlen = 0;
  21. int fd = 0;
  22. int time = 0;
  23. int testtimes = 10;
  24. char *pbuf = NULL;
  25. char *pcipaddr = NULL;
  26. char *pcdestport = NULL;
  27. char *pcdata = NULL;
  28. char *pctesttime = NULL;
  29. struct sockaddr_in addr;
  30. struct timeval timeout;
  31. if (argc < 5){
  32. printf( "invalid input tcp clinet test command \r\n");
  33. return -1;
  34. }
  35. pcipaddr = argv[ 2];
  36. pcdestport = argv[ 3];
  37. pcdata = argv[ 4];
  38. if (argc == 6){
  39. pctesttime = argv[ 5];
  40. testtimes = atoi(pctesttime);
  41. if ( 0 == testtimes){
  42. printf( "invalid input tcp client test time %s \r\n", pctesttime);
  43. return -1;
  44. }
  45. }
  46. memset(&addr, 0, sizeof(addr));
  47. addr.sin_port = htons(( short)atoi(pcdestport));
  48. if ( 0 == addr.sin_port){
  49. printf( "invalid input port info %s \r\n", pcdestport);
  50. return -1;
  51. }
  52. addr.sin_addr.s_addr = inet_addr(pcipaddr);
  53. if (IPADDR_NONE == addr.sin_addr.s_addr){
  54. printf( "invalid input addr info %s \r\n", pcipaddr);
  55. return -1;
  56. }
  57. addr.sin_family = AF_INET;
  58. fd = socket(AF_INET,SOCK_STREAM, 0);
  59. if (fd < 0){
  60. printf( "fail to creat socket errno = %d \r\n", errno);
  61. return -1;
  62. }
  63. printf( "client fd=%d, ip=%s, port=%d\n", fd, pcipaddr, addr.sin_port);
  64. timeout.tv_sec = 15;
  65. timeout.tv_usec = 0;
  66. if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, ( char *)&timeout,
  67. sizeof(timeout)) < 0) {
  68. printf( "setsockopt failed, errno: %d\r\n", errno);
  69. goto err;
  70. }
  71. if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
  72. printf( "Connect failed, errno = %d, ip %s port %s \r\n", errno, pcipaddr, pcdestport);
  73. goto err;
  74. }
  75. pbuf = aos_malloc(BUFFER_MAX_SIZE);
  76. if ( NULL == pbuf){
  77. printf( "fail to malloc memory %d at %s %d \r\n", BUFFER_MAX_SIZE, __FUNCTION__, __LINE__);
  78. goto err;
  79. }
  80. while( 1){
  81. // send-recv
  82. printf( "send data to server: \"%s\"\r\n", pcdata);
  83. if ((ret = send(fd, pcdata, strlen(pcdata), 0)) <= 0) {
  84. printf( "send data failed, errno = %d. for the %d time \r\n", errno, time);
  85. goto err;
  86. }
  87. memset(pbuf, 0, BUFFER_MAX_SIZE);
  88. printf( "read data from server...\r\n");
  89. readlen = read(fd, pbuf, BUFFER_MAX_SIZE - 1);
  90. if (readlen < 0){
  91. printf( "recv failed, errno = %d.\r\n", errno);
  92. goto err;
  93. }
  94. if (readlen == 0){
  95. printf( "recv buf len is %d \r\n", readlen);
  96. break;
  97. }
  98. printf( "recv server %d time reply len %d. str: %s\r\n", time, readlen, pbuf);
  99. time++;
  100. if (time >= testtimes){
  101. break;
  102. }
  103. }
  104. close(fd);
  105. aos_free(pbuf);
  106. return 0;
  107. err:
  108. close(fd);
  109. if ( NULL != pbuf){
  110. aos_free(pbuf);
  111. }
  112. return -1;
  113. }

编译方式:


  
  1. aos make tcp_demo@haas100 -c config
  2. aos make

烧录镜像:

参见:https://help.aliyun.com/document_detail/184184.html?spm=a2c4g.11186623.6.640.2fc57c26X2xKr3

 

执行命令:

network tcp_c 192.168.8.116 8080 received

5、后记

本实验作为一个入门,能够使用AliOS Things以及HaaS开发板板快速连接wifi并且和局域网进行通信,为进一步开发更多的网络相关应用打下基础。后续基于HaaS开发板的TCP server、TCP client、UDP server、UDP client都是类似的做法。希望在HaaS开发板上找到属于你的快乐。

 

如需更多技术支持,可加入钉钉开发者群,或者关注微信公众号

更多技术与解决方案介绍,请访问阿里云AIoT首页https://iot.aliyun.com/


转载:https://blog.csdn.net/HaaSTech/article/details/112380340
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场