-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.cpp
139 lines (127 loc) · 3.78 KB
/
client.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
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <limits>
#include <algorithm>
#include <thread>
#include <atomic>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "common.h"
#define SERVER_PORT "32345"
int getsock(const std::string &server_addr, const std::vector<char> &msg_to_send)
{
struct addrinfo hint, *res;
std::memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
int e = getaddrinfo(server_addr.c_str(), SERVER_PORT, &hint, &res);
if( e == -1 )
{
cerror("getaddrinfo");
std::exit(EXIT_FAILURE);
}
int sock;
struct addrinfo *rp;
for( rp = res; rp != nullptr; rp = rp->ai_next )
{
// Need to create the socket again when EINTR is returned
redo:
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if( sock == -1 )
continue;
ssize_t r = sendto(sock, &msg_to_send[0], msg_to_send.size(),
MSG_FASTOPEN, rp->ai_addr, rp->ai_addrlen);
if( r == -1 && errno == EINTR )
{
close(sock);
goto redo;
}
// Exit the loop when sendto succeeded
if( r != -1 )
break;
// Try without TCP FastOpen
r = connect(sock, rp->ai_addr, rp->ai_addrlen);
if( r == -1 && errno == EINTR )
{
close(sock);
goto redo;
}
if( r != -1 )
{
ssize_t sent;
do
{
sent = send(sock, &msg_to_send[0], msg_to_send.size(), 0);
} while( sent == -1 && sent == EINTR );
// Success
if( r != -1 )
break;
}
close(sock);
}
freeaddrinfo(res);
if( rp == nullptr )
{
std::cerr << "Couldn't get a socket." << std::endl;
std::exit(EXIT_FAILURE);
}
return sock;
}
void send_recv(const std::string &server_addr, const int msg_size, std::atomic_int &count)
{
std::vector<char> msg_buf(msg_size), recv_buf(msg_size);
std::mt19937 rand_engine;
std::uniform_int_distribution<char> distribution(
std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
auto generator = std::bind(distribution, rand_engine);
while( count-- > 0 )
{
std::generate(msg_buf.begin(), msg_buf.end(), generator);
FD sock(getsock(server_addr, msg_buf));
shutdown(sock, SHUT_WR);
ssize_t read_bytes = 0;
while( read_bytes != msg_size )
{
ssize_t r;
do
{
r = recv(sock, &recv_buf[read_bytes], recv_buf.size() - read_bytes, 0);
} while( r == -1 && errno == EINTR );
if( r == 0 ) break;
if( r == -1 )
{
cerror("recv");
std::exit(EXIT_FAILURE);
}
read_bytes += r;
}
if( msg_buf != recv_buf )
std::cerr << "Receive mismatch." << std::endl;
}
}
int main(int argc, char *argv[])
{
if( argc <= 4 )
{
std::cerr << "Usage: client [server addr] [msg size] [count] [thread]" << std::endl;
return EXIT_FAILURE;
}
std::string server_addr(argv[1]);
int msg_size = std::atoi(argv[2]);
std::atomic_int count(std::atoi(argv[3]));
int nthreads = std::atoi(argv[4]);
std::vector<std::thread> threads;
threads.reserve(nthreads);
for( int i = 0; i < nthreads; ++i )
threads.push_back(std::thread(send_recv, server_addr, msg_size, std::ref(count)));
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
}