代码拉取完成,页面将自动刷新
/*
* 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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。