Ai
4 Star 5 Fork 67

OpenHarmony/kernel_linux_common_modules
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
nip_udp_server_demo.c 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
liangbotong 提交于 2025-01-14 11:12 +08:00 . Clean code warnings for NewIP
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* Description: Demo example of NewIP udp server.
*
* Author: Yang Yanjun <yangyanjun@huawei.com>
*
* Data: 2022-09-06
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define __USE_GNU
#include <sched.h>
#include <pthread.h>
#include "nip_uapi.h"
#include "nip_lib.h"
#include "newip_route.h"
static void *recv_send(void *args)
{
char buf[BUFLEN] = {0};
int fd;
ssize_t recv_num, ret;
int count = 0;
socklen_t slen;
struct sockaddr_nin si_remote;
memcpy(&fd, args, sizeof(int));
while (count < PKTCNT) {
slen = sizeof(si_remote);
memset(buf, 0, sizeof(char) * BUFLEN);
memset(&si_remote, 0, sizeof(si_remote));
recv_num = recvfrom(fd, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, &slen);
if (recv_num < 0) {
printf("server recvfrom fail, recv_num=%zd\n", recv_num);
goto END;
} else if (recv_num == 0) { /* no data */
;
} else {
printf("Received -- %s -- from 0x%x:%d\n", buf,
si_remote.sin_addr.NIP_ADDR_FIELD16[0], ntohs(si_remote.sin_port));
slen = sizeof(si_remote);
ret = sendto(fd, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, slen);
if (ret < 0) {
printf("server sendto fail, ret=%zd\n", ret);
goto END;
}
printf("Sending -- %s -- to 0x%0x:%d\n", buf,
si_remote.sin_addr.NIP_ADDR_FIELD8[0], ntohs(si_remote.sin_port));
}
count++;
}
END: return NULL;
}
int main(int argc, char **argv)
{
int fd;
pthread_t th;
struct sockaddr_nin si_local;
fd = socket(AF_NINET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) {
perror("socket");
return -1;
}
memset((char *)&si_local, 0, sizeof(si_local));
si_local.sin_family = AF_NINET;
si_local.sin_port = htons(UDP_SERVER_PORT);
// 2-byte address of the server: 0xDE00
si_local.sin_addr.NIP_ADDR_FIELD8[INDEX_0] = 0xDE;
si_local.sin_addr.NIP_ADDR_FIELD8[INDEX_1] = 0x00;
si_local.sin_addr.bitlen = NIP_ADDR_BIT_LEN_16; // 2-byte: 16bit
if (bind(fd, (const struct sockaddr *)&si_local, sizeof(si_local)) < 0) {
perror("bind");
goto END;
}
printf("bind success, addr=0x%02x%02x, port=%d\n",
si_local.sin_addr.NIP_ADDR_FIELD8[INDEX_0],
si_local.sin_addr.NIP_ADDR_FIELD8[INDEX_1], UDP_SERVER_PORT);
pthread_create(&th, NULL, recv_send, &fd);
/* Wait for the thread to end and synchronize operations between threads */
pthread_join(th, NULL);
END: close(fd);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/kernel_linux_common_modules.git
git@gitee.com:openharmony/kernel_linux_common_modules.git
openharmony
kernel_linux_common_modules
kernel_linux_common_modules
master

搜索帮助