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 UDPEX 25 }; 26 class SpServerSocket { 27 public: 28 SpServerSocket(); 29 ~SpServerSocket(); 30 // 创建一个套接字 31 int Init(ProtoType type); 32 // UDP 33 int Sendto(std::string &sendBuf); 34 int Recvfrom(); 35 // TCP 36 int Accept(); 37 int Send(const std::string &sendBuf) const; 38 int Recv(); 39 // 关闭 40 void Close(); 41 std::string RecvBuf() const; 42 43 private: 44 int sock = -1; 45 struct sockaddr_in local; 46 struct sockaddr_in client; 47 int sockPort; 48 int connFd = -1; 49 const int listenCount = 5; 50 const static int buffSizeRecv = 1024; 51 char rbuf[buffSizeRecv] = ""; 52 const int udpPort = 8283; 53 const int tcpPort = 8284; 54 const int udpExPort = 8285; 55 }; 56 } 57 } 58 59 #endif // !SPSERVERSOCKET