• 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 #include <sstream>
16 #include <cstring>
17 #include <sys/socket.h>
18 #include "include/sp_server_socket.h"
19 namespace OHOS {
20 namespace SmartPerf {
SpServerSocket()21 SpServerSocket::SpServerSocket() {}
22 
~SpServerSocket()23 SpServerSocket::~SpServerSocket()
24 {
25     Close();
26 }
27 
Init(ProtoType type)28 int SpServerSocket::Init(ProtoType type)
29 {
30     if (type == ProtoType::TCP) {
31         sock = socket(AF_INET, SOCK_STREAM, 0);
32         sockPort = tcpPort;
33     }
34     if (type == ProtoType::UDP) {
35         sock = socket(AF_INET, SOCK_DGRAM, 0);
36         sockPort = udpPort;
37     }
38     if (sock < 0) {
39         std::cout << "Socket Create Failed:" << sock << std::endl;
40         return -1;
41     }
42     local.sin_family = AF_INET;
43     local.sin_port = htons(sockPort);
44     local.sin_addr.s_addr = htonl(INADDR_ANY);
45     if (::bind(sock, reinterpret_cast<struct sockaddr *>(&local), sizeof(local)) < 0) {
46         std::cout << "Socket Bind failed:" << sock << std::endl;
47         return -1;
48     }
49     if (type == ProtoType::TCP) {
50         if (listen(sock, listenCount) < 0) {
51             std::cout << "Socket Listen failed:" << sock << std::endl;
52             return -1;
53         }
54     }
55     return 0;
56 }
57 
Accept()58 int SpServerSocket::Accept()
59 {
60     connFd = accept(sock, nullptr, nullptr);
61     return connFd;
62 }
63 
Sendto(std::string & sendBuf)64 int SpServerSocket::Sendto(std::string &sendBuf)
65 {
66     socklen_t len = sizeof(sockaddr_in);
67     sendto(sock, sendBuf.c_str(), sendBuf.size(), 0, reinterpret_cast<struct sockaddr*>(&client), len);
68     return 0;
69 }
70 
Send(std::string sendBuf)71 int SpServerSocket::Send(std::string sendBuf)
72 {
73     int sendBytes = send(connFd, sendBuf.c_str(), sendBuf.size(), 0);
74     if (sendBytes < 0) {
75         std::cout << "Send:Error" << std::endl;
76         return -1;
77     }
78     return 0;
79 }
80 
Close() const81 void SpServerSocket::Close() const
82 {
83     shutdown(sock, SHUT_RD);
84 }
85 
Recvfrom()86 int SpServerSocket::Recvfrom()
87 {
88     bzero(rbuf, sizeof(rbuf));
89     socklen_t len = sizeof(sockaddr_in);
90     int l = recvfrom(sock, rbuf, sizeof(rbuf) - 1, 0, reinterpret_cast<struct sockaddr*>(&client), &len);
91     if (l > 0) {
92         std::cout << "Client:" << rbuf << std::endl;
93     }
94     return l;
95 }
96 
Recv()97 int SpServerSocket::Recv()
98 {
99     bzero(rbuf, sizeof(rbuf));
100     int l = recv(connFd, rbuf, sizeof(rbuf) - 1, 0);
101     if (l > 0) {
102         std::cout << "Client:" << rbuf << std::endl;
103     } else {
104         perror("recv error:");
105         return -1;
106     }
107     return l;
108 }
109 
RecvBuf() const110 std::string SpServerSocket::RecvBuf() const
111 {
112     std::string recvBuf = rbuf;
113     return recvBuf;
114 }
115 }
116 }
117