1 /*
2 * Copyright (c) 2024 Huawei Device 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 <cstdio>
17 #include <string>
18 #include <iostream>
19
20 #include "loop_event.h"
21 #include "le_socket.h"
22
23
main()24 int main()
25 {
26 printf("请输入创建socket的类型:(pipe, tcp)\n");
27 std::string socket_type;
28 std::cin >> socket_type;
29
30 int type;
31 std::string path;
32 if (socket_type == "pipe") {
33 type = TASK_STREAM | TASK_PIPE |TASK_SERVER | TASK_TEST;
34 path = "/data/testpipe";
35 } else if (socket_type == "tcp") {
36 type = TASK_STREAM | TASK_TCP |TASK_SERVER | TASK_TEST;
37 path = "127.0.0.1:7777";
38 } else {
39 printf("输入有误,请输入pipe或者tcp!");
40 system("pause");
41 return 0;
42 }
43
44 char *server = path.c_str();
45 int fd = CreateSocket(type, server);
46 if (fd == -1) {
47 printf("Create socket failed!\n");
48 system("pause");
49 return 0;
50 }
51
52 int ret = listenSocket(fd, type, server);
53 if (ret != 0) {
54 printf("Failed to listen socket %s\n", server);
55 system("pause");
56 return 0;
57 }
58
59 while (true) {
60 int clientfd = AcceptSocket(fd, type);
61 if (clientfd == -1) {
62 printf("Failed to accept socket %s\n", server);
63 continue;
64 }
65
66 std::cout << "Accept connection from %d" << clientfd << std::endl;
67 break;
68 }
69 return 0;
70 }