本文来源电子发烧友社区,作者:大菠萝Alpha, 帖子地址:https://bbs.elecfans.com/jishu_2307368_1_1.html
这个功能反反复复折腾了好几天,多亏开鸿智谷原厂工程师支持。
基于LWIP,在NiobeU4开发板上部署TCP Client,连接测试服务器,获取服务器发来的信息。
Server直接在计算机上,用Python实现了个简单的,功能就是client连接上后,给其发送一段字符。
import socket
# 指定协议
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 让端口可以重复使用
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 绑定ip和端口
server.bind(('0.0.0.0', 8080))
# 监听
server.listen(1)
# 等待消息
while1:
clientsocket, address = server.accept()
clientsocket.send(address[0].encode("utf-8")+b'HELLO world~')
clientsocket.close()
server.close()
代码比较简单,读读也清晰。
开发板端实现TCP Client功能即可,如上图,首先创建socket,再connect server,然后send 数据,最后close socket,一个典型的TCP Server-Client案例就好了,这次Client不发送而是接收(recv)信息,得益于OpenHarmony的LWIP协议栈,Client实现跟Linux编程毫无区别,具体代码如下:
void wifi_test(void)
{
int sock = -1;
struct sockaddr_in client_addr;
char recv_data[768] = {0};
int recv_data_len;
WifiConnect(WIFI_NAME, WIFI_PWD);
while (1) {
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("Socket errorn");
osDelay(OD_DELAY_100);
continue;
}
memset_s(&(client_addr), sizeof(client_addr), 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(SERVER_PORT);
client_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
printf("try connect to server "SERVER_IP" :%d n", SERVER_PORT);
if (connect(sock, (struct sockaddr *)&client_addr, sizeof(struct sockaddr)) == -1) {
closesocket(sock);
osDelay(OD_DELAY_1000);
continue;
}
printf("Connect to tcp server successful!n");
while (1) {
recv_data_len = recv(sock, recv_data, RECV_LEN, 0);
if (recv_data_len <= 0) {
break;
} else {
recv_data[recv_data_len] = '';
}
printf("recv: %sn", recv_data);
write(sock, recv_data, recv_data_len);
}
close(sock);
break;
}
}
反正hb set,hb build -f一顿操作猛如虎,直接烧写OHOS Img即可,其他的bootloader和partition因为没有变化,不必重新烧录。
从系统log中,可以看出,NiobeU4板子启动后收到了Server发来的数据,HELLO world~。
[20:07:45:841]<--System Init-->
[20:07:45:841]<--Wifi Init-->
[20:07:45:841]register wifi event succeed!
[20:07:45:841]I (161) wifi_init: WiFi RX IRAM OP enabled
[20:07:45:992]I (558) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
[20:07:45:992]I (2378) wifi:mode :
[20:07:48:981]WaitScanResult:wait success[3]s
[20:07:48:981]starlink
[20:07:48:981]
[20:07:49:835]sta (44:17:93:96:e6:34)
[20:07:49:835]I (2378) wifi:enable tsf
[20:07:49:835]I (6228) wifi:new:<8,0>, old:<1,0>, ap:<255,255>, sta:<8,0>, prof:1
[20:07:49:841]I (6228) wifi:state: init -> auth (b0)
[20:07:49:866]I (6238) wifi:state: auth -> assoc (0)
[20:07:49:866]I (6248) wifi:state: assoc -> run (10)
[20:07:49:866]I (6248) wifi:connected with starlink, aid = 6, channel 8, BW20, bssid = 3e:87:46:16:13:6e
[20:07:49:880]I (6248) wifi:security: Open Auth, phy: bgn, rssi: -50
[20:07:49:880]I (6callback function for wifi connect
[20:07:49:880]248) wifi:pm start, type: 1
[20:07:49:954]
[20:07:49:954]W (6268) wifi:idx:0 (ifx:0, 3e:87:46:16:13:6e), tid:0, ssn:0, winSize:64
[20:07:49:969]I (6348) wifi:AP's
[20:07:52:482]WaitConnectResult:wait success[1]s
[20:07:52:482]WiFi connect succeed!
[20:07:52:482]
[20:07:53:485]start wifi_test test
[20:07:53:485]try connect to server 192.168.43.159 :8080
[20:07:53:485]Connect to tcp server successful!
[20:07:53:495]recv: 192.168.43.158HELLO world~
[20:07:53:505]try connect to server 192.168.43.159 :8080
[20:07:53:505]Connect to tcp server successful!
[20:07:53:509]recv: 192.168.43.158HELLO world~
[20:07:53:523]try connect to server 192.168.43.159 :8080
[20:07:53:523]Connect to tcp server successful!
[20:07:53:523]recv: 192.168.43.158HELLO world~
##感谢阅读##
全部0条评论
快来发表一下你的评论吧 !