-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_client.cpp
More file actions
214 lines (188 loc) · 5.41 KB
/
tcp_client.cpp
File metadata and controls
214 lines (188 loc) · 5.41 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "tcp_client.h"
#include <iostream>
#include <cstdint>
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include "debug_print.h"
using namespace std;
/**
* @brief emerg_exit - This function is called when a system call fails.
* It displays a message about the error on stderr and then aborts the program.
* @param trigger
*/
void emerg_exit(bool trigger, std::string msg){
if(trigger){
perror(msg.c_str());
exit(1);
}
return;
}
/**
* @brief Network::connection::connection
*/
Network::connection::connection():address(),sock(0),port(0)
{
//Zero mngt struct
u8* bptr = reinterpret_cast<u8*>(&server);
u8* eptr = &bptr[sizeof(inet_sock_t)];
std::fill(bptr,eptr,0);
}
/**
* @brief Network::connection::~connection
*/
Network::connection::~connection()
{
if(reterr!=sock){
close(sock);
sock = reterr;
}
return;
}
//-----
/**
* @brief Network::tcp_client::connect_to - Connect to a host on a certain port number
* @param address - IP or domain string
* @param port - IP Port
* @return true on error
*/
void Network::tcp_client::prepare_con(Network::connection &con, string a, in_port_t p)
{
//Zero mngt struct
u8* bptr = reinterpret_cast<u8*>(&con.server);
u8* eptr = &bptr[sizeof(inet_sock_t)];
std::fill(bptr,eptr,0);
con.sock = reterr;
con.address = a;
con.port = p;
return;
}
bool Network::tcp_client::connect_to(connection& con){
bool err = false;
//create socket if it is not already created
if(con.sock == reterr){
//Create nonblocking socket
con.sock = socket(AF_INET , SOCK_NONBLOCK | SOCK_STREAM , 0);
err = (con.sock == reterr);
}
emerg_exit(err,"Network::tcp_client::connect_to - Could not create socket");
if(!err){
DBGOUT("Socket created")
//setup address structure
struct in_addr ipadr;
if(0==inet_aton(con.address.c_str(),&ipadr)){ //domain string which needs to be resolved
struct hostent *he;
struct in_addr **addr_list;
//resolve the hostname, its not an ip address
if ( (he = gethostbyname( con.address.c_str() ) ) == nullptr){
err = true;
//gethostbyname failed
herror("gethostbyname");
cout << "Failed to resolve hostname" << "\n";
return false;
}
{ //Print Infos for resolved hostname
cout << "Hostname: " << he->h_name << "\n";
if(AF_INET == he->h_addrtype) cout << "IPv4" << " --> sizeof(" << he->h_length << ")" << "\n";
if(AF_INET6 == he->h_addrtype) cout << "IPv6" << " --> sizeof(" << he->h_length << ")" << "\n";
size_t ii = 0;
for(char* adr = he->h_addr_list[ii]; adr!=nullptr; adr=he->h_addr_list[ii++]){
cout << "IP: " << inet_ntoa(*reinterpret_cast<struct in_addr*>(adr)) << "\n";
}
}
{ //set Internet Adress
if(AF_INET == he->h_addrtype){
//Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only
addr_list = reinterpret_cast<struct in_addr **>(he->h_addr_list);
con.server.sin_addr = *addr_list[0];
}else{
err = true;
cout << "IPv6 not implemented yet" << "\n";
}
}
}else{ //plain ip address
con.server.sin_addr.s_addr = ipadr.s_addr;
}
}
if(!err){
con.server.sin_family = AF_INET;
con.server.sin_port = htons( con.port );
//Connect to remote server
if (connect(con.sock , reinterpret_cast<struct sockaddr *>(&con.server), sizeof(inet_sock_t)) < 0){
close(con.sock);
con.sock = reterr;
err = true;
}
emerg_exit(err,"Network::tcp_client::connect_to - connect failed. Error");
cout << "Connected" << "\n";
}
return err;
}
bool Network::tcp_client::listen_to(connection& con)
{
bool err = true;
listen(con.sock, 128);
return err;
}
bool Network::tcp_client::is_connected(connection& con){
return (con.sock!=reterr);
}
/**
* @brief Network::tcp_client::send_data - Send data to the connected host
* @param data - data to send
* @return true on error
*/
bool Network::tcp_client::send_data(connection& con, string data){
bool err = true;
//Send some data
if(is_connected(con)){
err = false;
if(send(con.sock , data.c_str() , data.size() , 0) < 0){
err = true;
}else{
cout << "Data send" << "\n";
}
}
emerg_exit(err,"Network::tcp_client::connect_to - Send failed: ");
return err;
}
/**
* @brief Network::tcp_client::receive - Receive data from the connected host
* @param size - buffer size for message
* @return
*/
string Network::tcp_client::receive(connection& con, u32 const size=512){
string reply;
if(is_connected(con)){
char* buffer = new char[size+1];
//Receive a reply from the server
uint16_t timeout = 2400; // 2400*25ms = 60s
ssize_t res = reterr;
while (timeout>0) {
res = recv(con.sock , buffer , sizeof(buffer) , MSG_DONTWAIT);
if(res > 0) break;
if(res == reterr){
if(errno==EAGAIN) goto iter;
if(errno==EWOULDBLOCK) goto iter;
break;
}
iter:
struct timespec const ts = {0,25000000};
nanosleep(&ts, nullptr);
--timeout;
}
//if( recv(sock , buffer , sizeof(buffer) , MSG_DONTWAIT) < 0){
if(res==reterr){
cout << "recv failed" << "\n";
}else{
cout << "Bytes received: " << res << "\n";
reply = buffer;
}
delete [] buffer;
}
return reply;
}