10a7ce71fSopenharmony_ci# HiSpark WiFi-IoT 套件样例开发--网络编程(tcpclient) 20a7ce71fSopenharmony_ci 30a7ce71fSopenharmony_ci 40a7ce71fSopenharmony_ci 50a7ce71fSopenharmony_ci[HiSpark WiFi-IoT开发套件](https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w5003-23341819265.1.bf644a82Da9PZK&id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持OpenHarmony 2.0的开发套件,亦是官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmony社区和广大OpenHarmony开发者中得到广泛应用。 60a7ce71fSopenharmony_ci 70a7ce71fSopenharmony_ci 80a7ce71fSopenharmony_ci 90a7ce71fSopenharmony_ci## netcat简介 100a7ce71fSopenharmony_ci 110a7ce71fSopenharmony_ci### netcat 是什么? 120a7ce71fSopenharmony_ci 130a7ce71fSopenharmony_cinetcat是一个非常强大的网络实用工具,可以用它来调试TCP/UDP应用程序; 140a7ce71fSopenharmony_ci 150a7ce71fSopenharmony_ci### netcat 如何安装? 160a7ce71fSopenharmony_ci 170a7ce71fSopenharmony_ciLinux上可以使用发行版的包管理器安装,例如Debian/Ubuntu上: 180a7ce71fSopenharmony_ci 190a7ce71fSopenharmony_ci* `sudo apt-get install netcat` 200a7ce71fSopenharmony_ci 210a7ce71fSopenharmony_ciWindows上,MobaXterm工具上也可以用 `apt-get install netcat` 安装; 220a7ce71fSopenharmony_ci 230a7ce71fSopenharmony_ci### netcat 如何使用? 240a7ce71fSopenharmony_ci 250a7ce71fSopenharmony_ci开始之前,先简单介绍一下 netcat 的几种用法: 260a7ce71fSopenharmony_ci 270a7ce71fSopenharmony_ci1. TCP服务端模式: `netcat -l 5678` ,会启动一个TCP服务器,监听`5678`端口,你可以换成其他端口; 280a7ce71fSopenharmony_ci2. TCP客户端模式: `netcat localhost 5678`, `localhost`是目标主机参数,可以换成其他你想要连接的主机(主机名、IP地址、域名都可以),`5678`是端口; 290a7ce71fSopenharmony_ci * 你如果在同一台机器的两个终端中分别执行上述两条命令,它们两者之间就会建立连接一条TCP连接,此时你在其中一个终端上输入字符,敲回车就会发送到另一个终端中; 300a7ce71fSopenharmony_ci3. UDP服务端模式: `netcat -u -l 6789`, 没错,只需要加一个`-u`参数,就可以启动一个UDP服务端; 310a7ce71fSopenharmony_ci4. UDP客户端模式: `netcat -u localhost 6789`, 320a7ce71fSopenharmony_ci * 类似的,在同一台机器的两个终端中分别执行上述两条命令,他们两者之间也可以收发消息,只不过是UDP报文; 330a7ce71fSopenharmony_ci 340a7ce71fSopenharmony_ci 350a7ce71fSopenharmony_ci 360a7ce71fSopenharmony_ci 370a7ce71fSopenharmony_ci## LwIP简介 380a7ce71fSopenharmony_ci 390a7ce71fSopenharmony_ci### LwIP是什么? 400a7ce71fSopenharmony_ci 410a7ce71fSopenharmony_ci> A Lightweight TCP/IP stack 420a7ce71fSopenharmony_ci> 一个轻量级的TCP/IP协议栈 430a7ce71fSopenharmony_ci 440a7ce71fSopenharmony_ci详细介绍请参考LwIP项目官网:https://savannah.nongnu.org/projects/lwip/ 450a7ce71fSopenharmony_ci 460a7ce71fSopenharmony_ci### LwIP在openharmony上的应用情况 470a7ce71fSopenharmony_ci 480a7ce71fSopenharmony_ci目前,openharmony源码树有两份LwIP: 490a7ce71fSopenharmony_ci 500a7ce71fSopenharmony_ci1. third_party/lwip 510a7ce71fSopenharmony_ci * 源码形式编译 520a7ce71fSopenharmony_ci * 供liteos-a内核使用 530a7ce71fSopenharmony_ci * 还有一部分代码在kernel/liteos_a中,一起编译 540a7ce71fSopenharmony_ci2. vendor/hisi/hi3861/hi3861/third_party/lwip_sack 550a7ce71fSopenharmony_ci * hi3861-sdk的一部分 560a7ce71fSopenharmony_ci * 静态库形式编译 570a7ce71fSopenharmony_ci * 不可修改配置 580a7ce71fSopenharmony_ci * 可以查看当前配置(vend 590a7ce71fSopenharmony_ci 600a7ce71fSopenharmony_ci 610a7ce71fSopenharmony_ci 620a7ce71fSopenharmony_ci## 下载源码 630a7ce71fSopenharmony_ci 640a7ce71fSopenharmony_ci建议将本教程的源码下载到OpenHarmony源码树的顶层目录,即和`applications`、`build`等目录平级的地方: 650a7ce71fSopenharmony_ci 660a7ce71fSopenharmony_ci* 执行命令`git clone https://gitee.com/hihopeorg/HarmonyOS-IoT-Application-Development.git` 670a7ce71fSopenharmony_ci 680a7ce71fSopenharmony_ci 690a7ce71fSopenharmony_ci 700a7ce71fSopenharmony_ci## 文件说明 710a7ce71fSopenharmony_ci 720a7ce71fSopenharmony_ci文件: 730a7ce71fSopenharmony_ci 740a7ce71fSopenharmony_ci| 文件名 | 说明 | 750a7ce71fSopenharmony_ci| ------------------ | --------------------------------------------------- | 760a7ce71fSopenharmony_ci| BUILD.gn | 构建脚本,支持Hi3861、Hi3518、Hi3516开发板 | 770a7ce71fSopenharmony_ci| demo_entry_cmsis.c | liteos-m程序入口,支持Hi3861 | 780a7ce71fSopenharmony_ci| demo_entry_posix.c | liteos-a和Unix系统程序入口,Hi3516、Hi3518、PC | 790a7ce71fSopenharmony_ci| Makefile | Unix系统构建脚本,支持Linux/Mac OS | 800a7ce71fSopenharmony_ci| net_common.h | 系统网络接口头文件 | 810a7ce71fSopenharmony_ci| net_demo.h | demo脚手架头文件 | 820a7ce71fSopenharmony_ci| net_params.h | 网络参数,包括WiFi热点信息,服务器IP、端口信息 | 830a7ce71fSopenharmony_ci| tcp_client_test.c | TCP客户端 | 840a7ce71fSopenharmony_ci| tcp_server_test.c | TCP服务端 | 850a7ce71fSopenharmony_ci| udp_client_test.c | UDP客户端 | 860a7ce71fSopenharmony_ci| udp_server_test.c | UDP服务端 | 870a7ce71fSopenharmony_ci| wifi_connecter.c | WiFi STA模式API的封装实现文件,比原始接口更容易使用 | 880a7ce71fSopenharmony_ci| wifi_connecter.h | WiFi STA模式API的封装头文件,比原始接口更容易使用 | 890a7ce71fSopenharmony_ci 900a7ce71fSopenharmony_ci 910a7ce71fSopenharmony_ci 920a7ce71fSopenharmony_ci## Linux主机实验指南 930a7ce71fSopenharmony_ci 940a7ce71fSopenharmony_ci### 编译测试程序 950a7ce71fSopenharmony_ci 960a7ce71fSopenharmony_ci使用如下命令进行编译: 970a7ce71fSopenharmony_ci 980a7ce71fSopenharmony_ci1. `make` 编译测试程序,该命令会生成4个可执行文件和几个.o文件 990a7ce71fSopenharmony_ci2. `make clean`删除可执行程序和.o文件 1000a7ce71fSopenharmony_ci 1010a7ce71fSopenharmony_ci### 运行测试程序 1020a7ce71fSopenharmony_ci 1030a7ce71fSopenharmony_ciLinux系统可以在同一台机器上,使用多个终端进行测试; 1040a7ce71fSopenharmony_ci 1050a7ce71fSopenharmony_ci* TCP客户端测试: 1060a7ce71fSopenharmony_ci 1. 在一个终端中使用netcat启动一个TCP服务端:`netcat -l 5678`; 1070a7ce71fSopenharmony_ci 2. 在另一个终端中启动TCP客户端测试程序:`./tcp_client_test 5678 127.0.0.1` ; 1080a7ce71fSopenharmony_ci 3. 在netcat终端中应该会出现TCP客户端测试程序发来的:`Hello`,输入`World`并回车,`World`将会发送到测试程序所在终端; 1090a7ce71fSopenharmony_ci* TCP服务端测试: 1100a7ce71fSopenharmony_ci 1. 在一个终端中启动TCP服务端测试程序:`./tcp_server_test 5678` ; 1110a7ce71fSopenharmony_ci 2. 在另一个终端中使用netcat启动一个TCP客户端,并尝试连接测试程序:`netcat 127.0.0.1 5678`; 1120a7ce71fSopenharmony_ci 3. 在netcat终端中输入`Hello`并回车,终端应该会再输出一行`Hello`,后面一行是TCP服务端测试程序发送回来的,同时终端上会有相关打印; 1130a7ce71fSopenharmony_ci* UDP客户端测试: 1140a7ce71fSopenharmony_ci 1. 在一个终端中使用netcat启动一个UDP服务端:`netcat -u -l 5678`; 1150a7ce71fSopenharmony_ci 2. 在另一个终端中启动UDP客户端测试程序:`./udp_client_test 5678 127.0.0.1` ; 1160a7ce71fSopenharmony_ci 3. 在netcat终端中应该会出现UDP客户端测试程序发来的`Hello.`,输入`World`并回车,`World`将会发送到测试程序所在终端; 1170a7ce71fSopenharmony_ci* UDP服务端测试: 1180a7ce71fSopenharmony_ci 1. 在一个终端中启动UDP服务端测试程序:`./udp_server_test 5678` ; 1190a7ce71fSopenharmony_ci 2. 在另一个终端中使用netcat启动一个UDP客户端,并尝试连接测试程序:`netcat -u 127.0.0.1 5678`; 1200a7ce71fSopenharmony_ci 3. 在netcat终端中输入`Hello.`并回车,终端应该会再输出一行`Hello.`,后面一行是UDP服务端测试程序是发送回来的,同时终端上会有相关打印; 1210a7ce71fSopenharmony_ci 1220a7ce71fSopenharmony_ci 1230a7ce71fSopenharmony_ci 1240a7ce71fSopenharmony_ci## Hi3861开发板实验指南 1250a7ce71fSopenharmony_ci 1260a7ce71fSopenharmony_ci### 准备网络环境 1270a7ce71fSopenharmony_ci 1280a7ce71fSopenharmony_ci在Hi3861开发板上运行上述四个测试程序之前,需要先准备网络环境: 1290a7ce71fSopenharmony_ci 1300a7ce71fSopenharmony_ci1. 准备一个无线路由器, 1310a7ce71fSopenharmony_ci2. 将Linux系统的PC连接到这个无线路由器, 1320a7ce71fSopenharmony_ci * 如果是虚拟机运行的Linux系统,需要通过网线连接路由器,并且将虚拟机网口设置为“桥接网卡”,确保**能够从路由器分到IP地址**(这一点非常重要,因为默认是NAT模式,只能通过虚拟机访问外部环境,外部环境无法通过网络访问虚拟机); 1330a7ce71fSopenharmony_ci 1340a7ce71fSopenharmony_ci### 修改网络参数 1350a7ce71fSopenharmony_ci 1360a7ce71fSopenharmony_ci在Hi3861开发板上运行上述四个测试程序之前,需要根据你的无线路由、Linux系统IP修改`net_params.h`文件的相关代码: 1370a7ce71fSopenharmony_ci 1380a7ce71fSopenharmony_ci* PARAM_HOTSPOT_SSID 修改为你的热点名称 1390a7ce71fSopenharmony_ci* PARAM_HOTSPOT_PSK 修改为你的热点密码; 1400a7ce71fSopenharmony_ci* PARAM_SERVER_ADDR 修改为你的Linux主机IP地址; 1410a7ce71fSopenharmony_ci 1420a7ce71fSopenharmony_ci### 编译和烧录测试程序 1430a7ce71fSopenharmony_ci 1440a7ce71fSopenharmony_ci在Hi3861开发板上运行上述四个测试程序,需要分别编译、烧录程序; 1450a7ce71fSopenharmony_ci 1460a7ce71fSopenharmony_ci1. 需要修改`BUILD.gn`代码,取消你想要执行的测试程序的注释: 1470a7ce71fSopenharmony_ci * 编译TCP客户端测试程序,取消 `sources = ["tcp_client_test.c"]` 的注释,保留其余三行的`sources`注释; 1480a7ce71fSopenharmony_ci * 编译TCP服务端测试程序,取消 `sources = ["tcp_server_test.c"]` 的注释,保留其余三行的`sources`注释; 1490a7ce71fSopenharmony_ci * 编译UDP客户端测试程序,取消 `sources = ["udp_client_test.c"]` 的注释,保留其余三行的`sources`注释; 1500a7ce71fSopenharmony_ci * 编译UDP服务端测试程序,取消 `sources = ["udp_server_test.c"]` 的注释,保留其余三行的`sources`注释; 1510a7ce71fSopenharmony_ci2. 确认你已经修改了hi3861开发板的编译配置文件`build/lite/product/wifiiot.json`: 1520a7ce71fSopenharmony_ci * `"//applications/sample/wifi-iot/app"`替换为:`"//HarmonyOS-IoT-Application-Development:app"` 1530a7ce71fSopenharmony_ci3. 使用`python build.py wifiiot`进行编译; 1540a7ce71fSopenharmony_ci4. 使用DevEco Device Tool或者HiBurn将二进制程序烧录到Hi3861开发板上; 1550a7ce71fSopenharmony_ci5. 烧录成功后暂时不要复位程序; 1560a7ce71fSopenharmony_ci 1570a7ce71fSopenharmony_ci 1580a7ce71fSopenharmony_ci 1590a7ce71fSopenharmony_ci### 运行测试程序 1600a7ce71fSopenharmony_ci 1610a7ce71fSopenharmony_ciHi3861开发板上运行测试程序的操作流程和Linux上大体相同,只是Linux终端执行测试程序变成了——开发板复位后自动运行; 1620a7ce71fSopenharmony_ci 1630a7ce71fSopenharmony_ci* TCP客户端测试: 1640a7ce71fSopenharmony_ci 1. 在Linux终端中使用netcat启动一个TCP服务端:`netcat -l 5678`; 1650a7ce71fSopenharmony_ci 2. 连接开发板串口,复位开发板,板上程序启动后,首先会连接WiFi热点,然后会尝试连接到Linux上用netcat启动的TCP服务端; 1660a7ce71fSopenharmony_ci 3. 在Linux终端中应该会出现开发板上TCP客户端通过发来的`Hello`,输入`World`并回车,`World`将会发送到开发板上,同时开发板的串口会有相关打印; 1670a7ce71fSopenharmony_ci* TCP服务端测试: 1680a7ce71fSopenharmony_ci 1. 重新修改BUILD.gn,放开`sources = ["tcp_server_test.c"]`的注释,保留其他三行的注释,重新编译、烧录到开发板上; 1690a7ce71fSopenharmony_ci 2. 连接开发板串口,复位开发板,板上程序启动后,会首先连接WiFi热点,然后会启动TCP服务端,并监听`5678`端口; 1700a7ce71fSopenharmony_ci * 这里需要从串口日志上查看开发板的IP地址,下一步操作需要用到 1710a7ce71fSopenharmony_ci 3. 在Linux终端中使用netcat启动一个TCP客户端,并尝试连接到开发板:`netcat board_ip 5678`,其中`board_ip`是开发板的IP地址; 1720a7ce71fSopenharmony_ci 4. 在Linux终端中输入`Hello`并回车,终端应该会再输出一行`Hello`,后面一行是开发板上TCP服务端序发送回来的,同时开发板的串口会有相关打印; 1730a7ce71fSopenharmony_ci* UDP客户端测试: 1740a7ce71fSopenharmony_ci 1. 重新修改BUILD.gn,放开`sources = ["udp_client_test.c"]`的注释,保留其他三行的注释,重新编译、烧录到开发板上; 1750a7ce71fSopenharmony_ci 2. 在Linux终端中使用netcat启动一个UDP服务端:`netcat -u -l 5678`; 1760a7ce71fSopenharmony_ci 3. 连接开发板串口,复位开发板,板上程序启动后,首先会连接WiFi热点,然后会尝试连接到Linux上用netcat启动的UDP服务端; 1770a7ce71fSopenharmony_ci 4. 在Linux终端中应该会出现UDP客户端测试程序发来的`Hello.`,输入`World`并回车,`World`将会发送到开发板上,同时开发板的串口会有相关打印; 1780a7ce71fSopenharmony_ci* UDP服务端测试: 1790a7ce71fSopenharmony_ci 1. 重新修改BUILD.gn,放开`sources = ["udp_server_test.c"]`的注释,保留其他三行的注释,重新编译、烧录到开发板上; 1800a7ce71fSopenharmony_ci 2. 连接开发板串口,复位开发板,板上程序启动后,会首先连接WiFi热点,然后会启动UDP服务端,并监听`5678`端口; 1810a7ce71fSopenharmony_ci 3. 在Linux终端中使用netcat启动一个UDP客户端,并尝试连接到开发板:`netcat -u 127.0.0.1 5678`; 1820a7ce71fSopenharmony_ci 4. 在Linux终端中输入`Hello.`并回车,终端应该会再输出一行`Hello.`,后面一行是UDP服务端测试程序是发送回来的,同时开发板的串口会有相关打印; 1830a7ce71fSopenharmony_ci 1840a7ce71fSopenharmony_ci 1850a7ce71fSopenharmony_ci 1860a7ce71fSopenharmony_ci### 【套件支持】 1870a7ce71fSopenharmony_ci 1880a7ce71fSopenharmony_ci##### 1. 套件介绍 http://www.hihope.org/pro/pro1.aspx?mtt=8 1890a7ce71fSopenharmony_ci 1900a7ce71fSopenharmony_ci##### 2. 套件购买 https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop 1910a7ce71fSopenharmony_ci 1920a7ce71fSopenharmony_ci##### 3. 技术资料 1930a7ce71fSopenharmony_ci 1940a7ce71fSopenharmony_ci- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg** 1950a7ce71fSopenharmony_ci 1960a7ce71fSopenharmony_ci- HiHope官网-资源中心(SDK包、技术文档下载)[**www.hihope.org**](http://www.hihope.org/) 1970a7ce71fSopenharmony_ci 1980a7ce71fSopenharmony_ci##### 4. 互动交流 1990a7ce71fSopenharmony_ci 2000a7ce71fSopenharmony_ci- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群) 2010a7ce71fSopenharmony_ci- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429** 2020a7ce71fSopenharmony_ci- 润和HiHope售后服务群(QQ:980599547) 2030a7ce71fSopenharmony_ci- 售后服务电话(025-52668590)