1 /*
2 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include "cmsis_os2.h"
20 #include "ohos_init.h"
21
22 #include "lwip/sockets.h"
23 #include "wifi_connect.h"
24
25 #define TASK_STACK_SIZE (1024 * 10)
26 #define TASK_DELAY_2S 2
27 #define CONFIG_WIFI_SSID "BearPi" // 要连接的WiFi 热点账号
28 #define CONFIG_WIFI_PWD "123456789" // 要连接的WiFi 热点密码
29 #define CONFIG_CLIENT_PORT 8888 // 要连接的服务器端口
30 #define TCP_BACKLOG 10
31
32 char recvbuf[512];
33 char *buf = "Hello! I'm BearPi-HM_Nano TCP Server!";
34
TCPServerTask(void)35 static void TCPServerTask(void)
36 {
37 // 在sock_fd 进行监听,在 new_fd 接收新的链接
38 int sock_fd, new_fd;
39
40 // 服务端地址信息
41 struct sockaddr_in server_sock;
42
43 // 客户端地址信息
44 struct sockaddr_in client_sock, *cli_addr;
45 int sin_size;
46
47 // 连接Wifi
48 WifiConnect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD);
49
50 // 创建socket
51 if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
52 perror("socket is error\r\n");
53 exit(1);
54 }
55
56 bzero(&server_sock, sizeof(server_sock));
57 server_sock.sin_family = AF_INET;
58 server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
59 server_sock.sin_port = htons(CONFIG_CLIENT_PORT);
60
61 // 调用bind函数绑定socket和地址
62 if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1) {
63 exit(1);
64 }
65
66 // 调用listen函数监听(指定port监听)
67 if (listen(sock_fd, TCP_BACKLOG) == -1) {
68 exit(1);
69 }
70
71 printf("start accept\n");
72
73 // 调用accept函数从队列中
74 while (1) {
75 sin_size = sizeof(struct sockaddr_in);
76
77 if ((new_fd = accept(sock_fd, (struct sockaddr *)&client_sock, (socklen_t *)&sin_size)) == -1) {
78 perror("accept");
79 continue;
80 }
81
82 cli_addr = malloc(sizeof(struct sockaddr));
83
84 printf("accept addr\r\n");
85
86 if (cli_addr != NULL) {
87 int ret;
88 if (ret = memcpy_s(cli_addr, sizeof(struct sockaddr), &client_sock, sizeof(struct sockaddr)) != 0) {
89 perror("memcpy is error\r\n");
90 exit(1);
91 }
92 }
93 // 处理目标
94 ssize_t ret;
95
96 while (1) {
97 memset_s(recvbuf, sizeof(recvbuf), 0, sizeof(recvbuf));
98 if ((ret = recv(new_fd, recvbuf, sizeof(recvbuf), 0)) == -1) {
99 printf("recv error \r\n");
100 }
101 printf("recv :%s\r\n", recvbuf);
102 sleep(TASK_DELAY_2S);
103 if ((ret = send(new_fd, buf, strlen(buf) + 1, 0)) == -1) {
104 perror("send : ");
105 }
106 sleep(TASK_DELAY_2S);
107 }
108 close(new_fd);
109 }
110 }
111
TCPServerDemo(void)112 static void TCPServerDemo(void)
113 {
114 osThreadAttr_t attr;
115
116 attr.name = "TCPServerTask";
117 attr.attr_bits = 0U;
118 attr.cb_mem = NULL;
119 attr.cb_size = 0U;
120 attr.stack_mem = NULL;
121 attr.stack_size = TASK_STACK_SIZE;
122 attr.priority = osPriorityNormal;
123
124 if (osThreadNew((osThreadFunc_t)TCPServerTask, NULL, &attr) == NULL) {
125 printf("[TCPServerDemo] Failed to create TCPServerTask!\n");
126 }
127 }
128
129 APP_FEATURE_INIT(TCPServerDemo);
130