Hi3861 OpenHarmony嵌入式应用入门--UDP Server

news/2024/7/8 2:39:30 标签: OpenHarmony, udp

本篇使用的是lwip编写udp服务端。需要提前准备好一个PARAM_HOTSPOT_SSID宏定义的热点,并且密码为PARAM_HOTSPOT_PSK。

修改网络参数

在Hi3861开发板上运行上述四个测试程序之前,需要根据你的无线路由、Linux系统IP修改 net_params.h文件的相关代码:

  • PARAM_HOTSPOT_SSID 修改为你的热点名称
  • PARAM_HOTSPOT_PSK 修改为你的热点密码;
  • PARAM_SERVER_PORT 修改为你的udp服务器端口号;

LwIP Socket API编程udp服务端主要步骤

创建Socket socket()

设置服务器地址和端口 sockaddr_in

绑定Socket bind()

发送和接收数据 sendto() recv() recvfrom()

关闭Socket close()

代码编写

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

import("//build/lite/config/component/lite_component.gni")

lite_component("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    #"base_03_irqkey:base_irqkey_example",
    #"base_04_adc:base_adc_example",
    #"base_05_pwm:base_pwm_example",
    #"base_06_ssd1306:base_ssd1306_example",
    #"kernel_01_task:kernel_task_example",
    #"kernel_02_timer:kernel_timer_example",
    #"kernel_03_event:kernel_event_example",
    #"kernel_04_mutex:kernel_mutex_example",
    #"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",
    #"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",
    #"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",
    #"kernel_08_message_queue:kernel_message_queue_example",
    #"wifi_09_hotspot:wifi_hotspot_example",
    #"wifi_10_sta:wifi_sta_example",
    #"tcp_11_server:tcp_server_example",
    #"tcp_12_client:tcp_client_example",
    "udp_13_server:udp_server_example",
  ]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server\BUILD.gn文件

#Copyright (C) 2021 HiHope Open Source Organization .
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#
#limitations under the License.

static_library("udp_server_example") {
    # uncomment one of following line, to enable one test:
    sources = ["udp_server_example.c"]

    sources += ["wifi_connecter.c"]
    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal",
        "//foundation/communication/wifi_lite/interfaces/wifiservice",
    ]
}

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server\net_common.h文件,文件主要引入一些头文件。文件同tcp_11_server\net_common.h

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server\wifi_connecter.h文件,该头文件包含wifi连接的宏。文件同tcp_11_server\wifi_connecter.h

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server\wifi_connecter.c文件,文件同tcp_11_server\wifi_connecter.c

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\udp_13_server\udp_server_example.c文件

/*
 * Copyright (C) 2021 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
// #include <stddef.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "net_common.h"
#include "wifi_connecter.h"

#define DELAY_TICKS_10     (10)

static char message[128] = "";

void UdpServerTest(void)
{
    WifiDeviceConfig config = {0};

    // 准备AP的配置参数
    // strcpy(config.ssid, PARAM_HOTSPOT_SSID);
    // strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);
    strcpy_s(config.ssid, WIFI_MAX_SSID_LEN, PARAM_HOTSPOT_SSID);
    strcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, PARAM_HOTSPOT_PSK);
    config.securityType = PARAM_HOTSPOT_TYPE;

    osDelay(DELAY_TICKS_10);

    int netId = ConnectToHotspot(&config);

    ssize_t retval = 0;
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket

    struct sockaddr_in clientAddr = {0};
    socklen_t clientAddrLen = sizeof(clientAddr);
    struct sockaddr_in serverAddr = {0};
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PARAM_SERVER_PORT);
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    retval = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    if (retval < 0) {
        printf("bind failed, %ld!\r\n", retval);
        goto do_cleanup;
    }
    printf("bind to port %hu success!\r\n", PARAM_SERVER_PORT);

    retval = recvfrom(sockfd, message, sizeof(message), 0, (struct sockaddr *)&clientAddr, &clientAddrLen);
    if (retval < 0) {
        printf("recvfrom failed, %ld!\r\n", retval);
        goto do_cleanup;
    }
    printf("recv message {%s} %ld done!\r\n", message, retval);
    printf("peer info: ipaddr = %s, port = %hu\r\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));

    retval = sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
    if (retval <= 0) {
        printf("send failed, %ld!\r\n", retval);
        goto do_cleanup;
    }
    printf("send message {%s} %ld done!\r\n", message, retval);

do_cleanup:
    printf("do_cleanup...\r\n");

    close(sockfd);
    DisconnectWithHotspot(netId);
}

SYS_RUN(UdpServerTest);

使用build,编译成功后,使用upload进行烧录。


http://www.niftyadmin.cn/n/5536206.html

相关文章

pnpm的坑

请问pnpm的两个坑怎么解决&#xff1a; 第一个坑&#xff1a;没有节省磁盘空间 我已经配置了依赖的存储位置&#xff0c; 但我在项目里pnpm install以后&#xff0c;发现依赖包还是很大&#xff0c; 然后发现里面的链接并不是指向先前配置的依赖存储位置&#xff0c;而是指…

百度出品_文心快码Comate提升程序猿效率

1.文心快码 文心快码包含指令、插件 和 知识三种功能&#xff0c; 1&#xff09;指令包含Base64编码、Base64解码、JSON转TS类型、JSON转YAML、JWT解码喂JSON。 2&#xff09;插件包含 3&#xff09;指令包含如下功能&#xff1a; 官网链接

CentOS 7.6升级安装PHP 7.4解决方案

在CentOS 7.6上安装PHP 7.4&#xff0c;你可以按照以下步骤进行&#xff1a; 一、添加EPEL和Remi仓库 安装EPEL仓库&#xff1a; EPEL&#xff08;Extra Packages for Enterprise Linux&#xff09;仓库提供了额外的软件包&#xff0c;这些软件包在CentOS的默认仓库中可能找不…

ArcGIS Pro SDK (七)编辑 12 编辑模版

ArcGIS Pro SDK &#xff08;七&#xff09;编辑 12 编辑模版 文章目录 ArcGIS Pro SDK &#xff08;七&#xff09;编辑 12 编辑模版1 在图层上按名称查找编辑模板2 查找属于独立表的表模板3 当前模板4 更改模板的默认编辑工具5 隐藏或显示模板上的编辑工具6 使用图层创建新模…

小程序 npm 支持

使用 npm 包 目前小程序已经支持使用 npm 安装第三方包&#xff0c;因为 node_modules 目录中的包不会参与小程序项目的编译、 上传和打包&#xff0c;因此在小程序 项目中要使用的 npm 包&#xff0c;必须走一遍构建 npm 的过程。在构建成功以后&#xff0c;默认 会在小程序目…

VSCode常用的一些插件

Chinese (Simplified) 汉语&#xff08;简体&#xff09;拓展包。 Auto Close Tag 可以自动增加xml/html的闭合标签。 CodeSnap 截图神器。截图效果在下面。 Dracula Official vscode一个很好看的主题。 Git Graph git管理工具。 GitHub Repositories 有了它&#xff0c;不…

山西车间应用LP-LP-SCADA系统的好处有哪些

关键字:LP-SCADA系统, 传感器可视化, 设备可视化, 独立SPC系统, 智能仪表系统,SPC可视化,独立SPC系统 LP-SCADA&#xff08;监控控制与数据采集&#xff09;系统是工业控制系统的一种&#xff0c;主要用于实时监控、控制和管理工业生产过程。 在车间应用LP-SCADA系统&#xf…

图像练习-识别中圆形锡点 (04)

图片 代码 cv::Mat src cv::imread("light_point.png", cv::IMREAD_COLOR);cv::Mat draw src.clone();cv::Rect rt0(20, 80, src.cols - 30, 190);cv::Rect rt1(20, 480, src.cols - 30, 190);cv::Mat gray;cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);cv::Mat …