-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpSocket.cpp
144 lines (117 loc) · 3.32 KB
/
TcpSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by marcelghisi on 15/09/22.
//
#include "TcpSocket.h"
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
TcpSocket::TcpSocket() {
socket_desc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_desc == -1) {
perror("socket failed ");
return;
}
int num = 1;
int keepidle = 3;
if (setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &num, sizeof(int)) == -1)
print_error("setsockopt(SO_REUSEADDR) failed ");
if (setsockopt(socket_desc, SOL_SOCKET, SO_KEEPALIVE, &num, sizeof(int)) == -1)
print_error("setsockopt(SO_KEEPALIVE) failed ");
if (setsockopt(socket_desc, SOL_TCP, TCP_KEEPINTVL, &keepidle, sizeof(int)) == -1)
print_error("setsockopt(TCP_KEEPINTVL) failed ");
}
TcpSocket::TcpSocket(int descriptor) {
socket_desc = descriptor;
int num = 1;
int keepidle = 3;
if (setsockopt(socket_desc, SOL_SOCKET, SO_KEEPALIVE, &num, sizeof(int)) == -1)
print_error("setsockopt(SO_KEEPALIVE) failed ");
if (setsockopt(socket_desc, SOL_TCP, TCP_KEEPINTVL, &keepidle, sizeof(int)) == -1)
print_error("setsockopt(TCP_KEEPINTVL) failed ");
}
bool TcpSocket::connect(const std::string& ip_address, uint16_t port)
{
sockaddr_in *server = (sockaddr_in*)calloc(sizeof(sockaddr_in), 1);
server->sin_family = AF_INET;
server->sin_port = htons(port);
server->sin_addr.s_addr = inet_addr(ip_address.c_str());
if (::connect(socket_desc, (const sockaddr*)server, sizeof(sockaddr_in)) == -1)
{
print_error("connect failed ");
return false;
}
return true;
}
bool TcpSocket::bind(uint16_t port)
{
sockaddr_in server;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port);
server.sin_family = AF_INET;
return ::bind(socket_desc, (sockaddr *) &server, sizeof(sockaddr_in)) >= 0;
}
bool TcpSocket::listen(int numConnections)
{
if (::listen(socket_desc, numConnections) == -1){
print_error("listen failed ");
return false;
}
return true;
}
int TcpSocket::accept()
{
socklen_t c = sizeof(struct sockaddr_in);
sockaddr_in client;
int new_socket;
do {
new_socket = ::accept(socket_desc, (struct sockaddr *)&client, &c);
if (new_socket == -1 && errno != EINTR)
{
print_error("accept failed ");
return 0;
}
} while(new_socket == -1);
return new_socket;
}
ssize_t TcpSocket::send(cstring message, int flags)
{
ssize_t tr;
if((tr = ::send(socket_desc, message.c_str(), message.size(), 0)) == -1)
{
print_error("send failed ");
return 0;
}
return tr;
}
std::string TcpSocket::recv()
{
char buffer[BUF_SIZE + 1];
int res;
if ((res = ::recv(socket_desc, buffer, BUF_SIZE, 0)) == -1)
{
print_error("recv failed ");
return "";
}
if (res == 0)
return "";
buffer[res] = 0;
return buffer;
}
void TcpSocket::close() {
::close(socket_desc);
}
void TcpSocket::shutdown() {
::shutdown(socket_desc, SHUT_RDWR);
}
int TcpSocket::recv_to_buffer(char *buffer, int size) {
int res;
if ((res = ::recv(socket_desc, buffer, size, 0)) == -1)
{
print_error("read failed ");
}
return res;
}
int TcpSocket::getFD() {
return socket_desc;
}