• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef SP_SERVERSOCKET_H
16 #define SP_SERVERSOCKET_H
17 #include <iostream>
18 #include <netinet/in.h>
19 namespace OHOS {
20 namespace SmartPerf {
21 enum class ProtoType {
22     TCP,
23     UDP
24 };
25 class SpServerSocket {
26 public:
27     SpServerSocket();
28     ~SpServerSocket();
29     // 创建一个套接字
30     int Init(ProtoType type);
31     // UDP
32     int Sendto(std::string &sendBuf);
33     int Recvfrom();
34     // TCP
35     int Accept();
36     int Send(const std::string &sendBuf) const;
37     int Recv();
38     // 关闭
39     void Close();
40     std::string RecvBuf() const;
41 
42 private:
43     int sock = -1;
44     struct sockaddr_in local;
45     struct sockaddr_in client;
46     int sockPort;
47     int connFd = -1;
48     const int listenCount = 5;
49     const static int buffSizeRecv = 256;
50     char rbuf[buffSizeRecv] = "";
51     const int udpPort = 8283;
52     const int tcpPort = 8284;
53 };
54 }
55 }
56 
57 #endif // !SPSERVERSOCKET