2 Star 0 Fork 0

liangkz/OpenBrotherPzCar

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
net_tcp_server_entry.c 4.98 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* Copyright (C) 2023 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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/sockets.h"
#include "net_config.h"
static int sockfd = -1;
static int connfd = -1;
#define RECV_BUFF_SIZE (128)
void TcpSocketDeInit(void)
{
printf("TcpSocketDeInit: close(server socket)\n");
close(connfd);
close(sockfd);
}
void TcpServRecvTask(void)
{
int ret;
int msgLen = -1;
unsigned char recv_buff[RECV_BUFF_SIZE] = { 0 };
int backlog = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0); // TCP socket
struct sockaddr_in serverAddr = {0};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(TCP_SERVERPORT); // 端口号,从主机字节序转为网络字节序
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); // 允许任意主机接入,0.0.0.0
ret = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); // 绑定端口
if (ret < 0) {
printf("%s: bind failed, ret[%d]\n", __func__, ret);
close(sockfd);
return;
}
printf("%s: bind success, ret[%d]\n", __func__, ret);
ret = listen(sockfd, backlog); // 开始监听
if (ret < 0) {
printf("%s: listen failed, ret[%d]\n", __func__, ret);
close(sockfd);
return;
}
printf("%s: listen success, ret[%d]\n", __func__, ret);
while (1) {
// 接受客户端连接,成功会返回一个表示连接的 socket , clientAddr 参数将会携带客户端主机和端口信息 ;失败返回 -1
// 此后的 收、发 都在 表示连接的 socket 上进行;之后 sockfd 依然可以继续接受其他客户端的连接,
// UNIX系统上经典的并发模型是“每个连接一个进程”——创建子进程处理连接,父进程继续接受其他客户端的连接
// 鸿蒙liteos-a内核之上,可以使用UNIX的“每个连接一个进程”的并发模型
// liteos-m内核之上,可以使用“每个连接一个线程”的并发模型
struct sockaddr_in clientAddr = {0};
socklen_t clientAddrLen = sizeof(clientAddr);
connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (connfd < 0) {
printf("%s: accept failed, connfd[%d], errno[%d]\n", __func__, connfd, errno);
//close(sockfd);
//return;
continue;
}
printf("%s: accept success, connfd[%d]\n", __func__, connfd);
printf("%s: client info: addr[%s], port[%hu]\n", __func__,
inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
while (connfd > 0) {
memset(recv_buff, 0, sizeof(recv_buff));
msgLen = recv(connfd, recv_buff, sizeof(recv_buff), 0);
if (msgLen > 0) {
printf("%s: recv: [%d] %s\n", __func__, msgLen, recv_buff);
ProcessRecvMsg(recv_buff, msgLen);
} else {
printf("%s: recv: [%d] close current connect!\n", __func__, msgLen);
close(connfd);
connfd = -1;
break;
}
}
}
//TcpSocketDeInit();
}
void TcpServSendTask(void)
{
unsigned char send_buff[] = "Hello TCP Client!";
while (1) {
sleep(1);
if (connfd > 0) {
int msgLen = send(connfd, send_buff, strlen(send_buff), 0);
if (msgLen < 0) {
printf("%s: send failed! client disconnect\n", __func__);
close(connfd);
connfd = -1;
} else {
printf("%s: send: [%d] %s\n", __func__, msgLen, send_buff);
}
}
}
//TcpSocketDeInit();
}
osThreadId_t TcpServRecvTaskEntry(void)
{
//{.name, .attr_bits, .cb_mem, .cb_size, .stack_mem, .stack_size, .priority, .tz_module, .reserved}
osThreadAttr_t attr = {"TcpServRecvTask", 0, NULL, 0, NULL, 1024*6, 24, 0, 0};
osThreadId_t taskId = osThreadNew((osThreadFunc_t)TcpServRecvTask, NULL, &attr);
if (taskId == NULL) {
printf("[%s] create TcpServRecvTask Failed!\n", __func__);
}
return taskId;
}
osThreadId_t TcpServSendTaskEntry(void)
{
//{.name, .attr_bits, .cb_mem, .cb_size, .stack_mem, .stack_size, .priority, .tz_module, .reserved}
osThreadAttr_t attr = {"TcpServSendTask", 0, NULL, 0, NULL, 1024*2, 24, 0, 0};
osThreadId_t taskId = osThreadNew((osThreadFunc_t)TcpServSendTask, NULL, &attr);
if (taskId == NULL) {
printf("[%s] create TcpServSendTask Failed!\n", __func__);
}
return taskId;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liangkzgitee/PzCar.git
git@gitee.com:liangkzgitee/PzCar.git
liangkzgitee
PzCar
OpenBrotherPzCar
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385