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

本篇使用的是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进行烧录。

相关推荐
钛态5 小时前
Flutter 组件 ews 的适配 鸿蒙Harmony 实战 - 深度对接企业级 Exchange 服务、实现鸿蒙端邮件与日程的高效分发及 SOAP 协议连接方案
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx5 小时前
Flutter 组件 sse_stream 的适配 鸿蒙Harmony 深度进阶 - 驾驭高并发 Server-Sent Events 背压处理、实现鸿蒙端工业级 AI 响应流与长效链路治理方案
flutter·harmonyos·鸿蒙·openharmony·sse_stream
钛态5 小时前
Flutter 三方库 tftp 的鸿蒙化适配指南 - 实现 RFC 1350 标准的极简文件传输协议、支持端侧嵌入式设备固件更新与局域网数据交换实战
flutter·harmonyos·鸿蒙·openharmony
雷帝木木5 小时前
Flutter 组件 metalink 的适配 鸿蒙Harmony 深度进阶 - 驾驭节点负载热力均衡、实现鸿蒙端跨域传输安全 (TLS) 与 HAP 原子化精准推送方案
flutter·harmonyos·鸿蒙·openharmony
王码码20355 小时前
Flutter 三方库 soundcloud_explode_dart 的鸿蒙化适配指南 - 实现高性能的 SoundCloud 媒体内容解析、支持音频流下载与全量元数据透传
flutter·harmonyos·鸿蒙·openharmony·soundcloud_explode_dart
里欧跑得慢5 小时前
Flutter 组件 dart_sdl 的适配 鸿蒙Harmony 实战 - 驾驭底层原生渲染、实现鸿蒙端高性能游戏图形与硬件级多轴交互方案
flutter·harmonyos·鸿蒙·openharmony·dart_sdl
添砖java‘’7 小时前
传输层协议UDP和TCP
网络·tcp/ip·udp
冉佳驹9 小时前
Linux ——— 网络开发核心知识与协议实现详解
linux·http·https·udp·json·tcp·端口号
Yupureki10 小时前
《Linux网络编程》2.Socket编程(UDP/TCP)
linux·服务器·c语言·网络·c++·tcp/ip·udp
spencer_tseng1 天前
Wed Apr 01 08:55:02 2026 read UDP: Unknown error (code=10054)
udp