HI3861学习笔记(21)——TCP服务端
Posted Leung_ManWah
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HI3861学习笔记(21)——TCP服务端相关的知识,希望对你有一定的参考价值。
一、TCP与UDP优缺点
1、TCP面向连接(如打电话要先拨号建立连接);UDP是无连接的,即发送数据之前不需要建立连接。
2、TCP提供可靠的服务。也就是说,通过TCP连接传送的数据,无差错,不丢失,不重复,且按序到达;UDP尽最大努力交付,即不保证可靠交付。
TCP通过校验和,重传控制,序号标识,滑动窗口、确认应答实现可靠传输。如丢包时的重发控制,还可以对次序乱掉的分包进行顺序控制。3、UDP具有较好的实时性,工作效率比TCP高,适用于对高速传输和实时性有较高的通信或广播通信。
4、每一条TCP连接只能是点到点的;UDP支持一对一,一对多,多对一和多对多的交互通信。
5、TCP对系统资源要求较多,UDP对系统资源要求较少。
二、API说明
以下 TCP 接口位于 vendor\\hisi\\hi3861\\hi3861\\third_party\\lwip_sack\\include\\sockets.h。
业务BUILD.gn中包含路径
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
"//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
]
socket()
bind()
accept()
shutdown()
getpeername()
getsockopt()
&setsockopt()
close()
read()
,readv()
,write()
,writev()
recv()
send()
,sendmsg()
,sendto()
select()
fcntl()
三、TCP服务端
3.1 主要流程
3.1.1 第一步:新建socket
//在sock_fd 进行监听
int sock_fd;
/**
* 1.创建socket
* AF_INET:ipv4
* SOCK_STRAM:tcp协议
*/
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("socket is error\\r\\n");
exit(1);
3.1.2 第二步:配置服务器信息
#define TCP_PORT 3333 // TCP服务器端口号
//2.服务端地址信息
struct sockaddr_in server_sock;
bzero(&server_sock, sizeof(server_sock));
server_sock.sin_family = AF_INET;
server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
server_sock.sin_port = htons(TCP_PORT);
3.1.3 第三步:绑定地址
/*3.调用bind函数绑定socket和地址*/
if(bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
perror("bind is error\\r\\n");
exit(1);
/**
- 4:调用listen函数监听(指定port监听)
- 通知操作系统区接受来自客户端链接请求
- 第二个参数:指定队列长度
*/
3.1.4 第四步:开始监听
if(listen(sock_fd, TCP_BACKLOG) == -1)
perror("listen is error\\r\\n");
exit(1);
printf("start accept\\n");
3.1.5 第五步:等待客户端连接
//客户端地址信息
struct sockaddr_in client_sock;
int sin_size;
struct sockaddr_in *cli_addr;
/**
* 5:调用accept函数从队列中
* 获得一个客户端的请求链接
*/
while(1)
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd, (struct sockaddr *)&client_sock, (socklen_t *)&sin_size)) == -1)
perror("accept");
continue;
cli_addr = malloc(sizeof(struct sockaddr));
printf("accept addr\\r\\n");
if(cli_addr != NULL)
memcpy(cli_addr, &client_sock, sizeof(struct sockaddr));
···
3.1.6 第六步:接收数据
//处理目标
ssize_t ret;
while (1)
if((ret = recv(new_fd, recvbuf, sizeof(recvbuf), 0)) == -1)
printf("recv error \\r\\n");
printf("recv :%s\\r\\n", recvbuf);
sleep(2);
···
3.1.7 第七步:发送数据
if ((ret = send(new_fd, buf, strlen(buf) + 1, 0)) == -1)
perror("send : ");
3.1.8 第八步:关闭连接
//关闭这个 socket
close(new_fd);
3.2 配置SSID和密码连接WIFI创建TCP客户端
wifi_connect.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"
#include "cmsis_os2.h"
#include "hos_types.h"
#include "wifi_device.h"
#include "wifiiot_errno.h"
#include "ohos_init.h"
#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
static void WiFiInit(void);
static void WaitSacnResult(void);
static int WaitConnectResult(void);
static void OnWifiScanStateChangedHandler(int state, int size);
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static int g_staScanSuccess = 0;
static int g_ConnectSuccess = 0;
static int ssid_count = 0;
WifiEvent g_wifiEventHandler = 0;
WifiErrorCode error;
#define SELECT_WLAN_PORT "wlan0"
int WifiConnect(const char *ssid, const char *psk)
WifiScanInfo *info = NULL;
unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
static struct netif *g_lwip_netif = NULL;
osDelay(200);
printf("<--System Init-->\\r\\n");
//初始化WIFI
WiFiInit();
//使能WIFI
if (EnableWifi() != WIFI_SUCCESS)
printf("EnableWifi failed, error = %d\\r\\n", error);
return -1;
//判断WIFI是否激活
if (IsWifiActive() == 0)
printf("Wifi station is not actived.\\r\\n");
return -1;
//分配空间,保存WiFi信息
info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
if (info == NULL)
return -1;
//轮询查找WiFi列表
do
//重置标志位
ssid_count = 0;
g_staScanSuccess = 0;
//开始扫描
Scan();
//等待扫描结果
WaitSacnResult();
//获取扫描列表
error = GetScanInfoList(info, &size);
while(g_staScanSuccess != 1);
//打印WiFi列表
printf("********************\\r\\n");
for(uint8_t i = 0; i < ssid_count; i++)
printf("no:%03d, ssid:%-30s, rssi:%5d\\r\\n", i+1, info[i].ssid, info[i].rssi/100);
printf("********************\\r\\n");
//连接指定的WiFi热点
for(uint8_t i = 0; i < ssid_count; i++)
if (strcmp(ssid, info[i].ssid) == 0)
int result;
printf("Select:%3d wireless, Waiting...\\r\\n", i+1);
//拷贝要连接的热点信息
WifiDeviceConfig select_ap_config = 0;
strcpy(select_ap_config.ssid, info[i].ssid);
strcpy(select_ap_config.preSharedKey, psk);
select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS)
if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1)
printf("WiFi connect succeed!\\r\\n");
g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
break;
if(i == ssid_count-1)
printf("ERROR: No wifi as expected\\r\\n");
while(1) osDelay(100);
//启动DHCP
if (g_lwip_netif)
dhcp_start(g_lwip_netif);
printf("begain to dhcp\\r\\n");
//等待DHCP
for(;;)
if(dhcp_is_bound(g_lwip_netif) == ERR_OK)
printf("<-- DHCP state:OK -->\\r\\n");
//打印获取到的IP信息
netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
break;
printf("<-- DHCP state:Inprogress -->\\r\\n");
osDelay(100);
osDelay(100);
return 0;
static void WiFiInit(void)
printf("<--Wifi Init-->\\r\\n");
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
error = RegisterWifiEvent(&g_wifiEventHandler);
if (error != WIFI_SUCCESS)
printf("register wifi event fail!\\r\\n");
else
printf("register wifi event succeed!\\r\\n");
static void OnWifiScanStateChangedHandler(int state, int size)
if (size > 0)
ssid_count = size;
g_staScanSuccess = 1;
printf("callback function for wifi scan:%d, %d\\r\\n", state, size);
return;
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
if (info == NULL)
printf("WifiConnectionChanged:info is null, stat is %d.\\n", state);
else
if (state == WIFI_STATE_AVALIABLE)
g_ConnectSuccess = 1;
else
g_ConnectSuccess = 0;
static void OnHotspotStaJoinHandler(StationInfo *info)
(void)info;
printf("STA join AP\\n");
return;
static void OnHotspotStaLeaveHandler(StationInfo *info)
(void)info;
printf("HotspotStaLeave:info is null.\\n");
return;
static void OnHotspotStateChangedHandler(int state)
printf("HotspotStateChanged:state is %d.\\n", state);
return;
static void WaitSacnResult(void)
int scanTimeout = DEF_TIMEOUT;
while (scanTimeout > 0)
sleep(ONE_SECOND);
scanTimeout--;
if (g_staScanSuccess == 1)
printf("WaitSacnResult:wait success[%d]s\\n", (DEF_TIMEOUT - scanTimeout));
break;
if (scanTimeout <= 0)
printf("WaitSacnResult:timeout!\\n");
static int WaitConnectResult(void)
int ConnectTimeout = DEF_TIMEOUT;
while (ConnectTimeout > 0)
sleep(ONE_SECOND);
ConnectTimeout--;
if (g_ConnectSuccess == 1)
printf("WaitConnectResult:wait success[%d]s\\n", (DEF_TIMEOUT - ConnectTimeout));
break;
if (ConnectTimeout <= 0)
printf("WaitConnectResult:timeout!\\n");
return 0;
return 1;
wifi_connect.h
#ifndef __WIFI_CONNECT_H__
#define __WIFI_CONNECT_H__
int WifiConnect(const char *ssid,const char *psk);
#endif /* __WIFI_CONNECT_H__ */
四、完整代码
编译时在业务BUILD.gn中包含路径
static_library("myapp")
sources = [
"wifi_connect.c",
"hello_world.c"
]
cflags = [ "-Wno-unused-variable" ]
cflags += [ "-Wno-unused-but-set-variable" ]
cflags += [ "-Wno-unused-parameter" ]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
"//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
]
hello_world.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "lwip/sockets.h"
#include "wifi_connect.h"
#define TCP_PROT 8888
#define TCP_BACKLOG 10
//在sock_fd 进行监听,在 new_fd 接收新的链接
int sock_fd, new_fd;
char recvbuf[512];
char *buf = "Hello! I'm BearPi-HM_Nano TCP Server!";
static void TCPServerTask(void)
//服务端地址信息
struct sockaddr_in server_sock;
//客户端地址信息
struct sockaddr_in client_sock;
int sin_size;
struct sockaddr_in *cli_addr;
//连接Wifi
WifiConnect("TEST", "12345678");
//创建socket
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("socket is error\\r\\n");
exit(1);
bzero(&server_sock, sizeof(server_sock));
server_sock.sin_family = AF_INET;
server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
server_sock.sin_port = htons(TCP_PROT);
//调用bind函数绑定socket和地址
if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
perror("bind is error\\r\\n");
exit(1);
//调用listen函数监听(指定port监听)
if (listen(sock_fd, TCP_BACKLOG) == -1)
perror("listen is error\\r\\n");
exit(1);
printf("start accept\\n");
//调用accept函数从队列中
while (1)
sin_size = sizeof(stru以上是关于HI3861学习笔记(21)——TCP服务端的主要内容,如果未能解决你的问题,请参考以下文章