-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
370 lines (323 loc) · 11.2 KB
/
Copy pathclient.cpp
File metadata and controls
370 lines (323 loc) · 11.2 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Blake Berry
// 05/30/2022
// Homework 4
// This file is an implimentation of a client using TCP protcols for socket
// communication. Each service request generates a thread to address the client
//------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <chrono>
#include <math.h>
using namespace std;
const int BUFFSIZE = 1500;
// An enum type for the passed in write style
enum writeType
{
MULTIPLE = 1,
ATOMIC_MULTIPLE = 2,
ONE_WRITE = 3
};
//----------------------- intializeClientSocket -------------------------------
// Initializes the client socket using TCP communication protocol
// Preconditions : Assumes the server name and server port are not null
// Postconditions: if the socket connection is successful the client socket
// descriptor is returned otherwise the clinet program is
// exited
int intializeClientSocket(char *serverName, char *serverPort)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int clientSD = -1;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6*/
hints.ai_socktype = SOCK_STREAM; /* TCP */
hints.ai_flags = 0; /* Optional Options*/
hints.ai_protocol = 0; /* Allow any protocol*/
int rc = getaddrinfo(serverName, serverPort, &hints, &result);
if (rc != 0)
{
cerr << "ERROR: " << gai_strerror(rc) << endl;
exit(EXIT_FAILURE);
}
/*
* Iterate through addresses and connect
*/
for (rp = result; rp != NULL; rp = rp->ai_next)
{
clientSD = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (clientSD == -1)
{
continue;
}
/*
* A socket has been successfully created
*/
rc = connect(clientSD, rp->ai_addr, rp->ai_addrlen);
if (rc < 0)
{
cerr << "Connection Failed" << endl;
close(clientSD);
return -1;
}
else // success
{
break;
}
if (rp == NULL)
{
cerr << "No valid address" << endl;
exit(EXIT_FAILURE);
}
else
{
cout << "Client Socket: " << clientSD << endl;
}
freeaddrinfo(result);
}
return clientSD;
}
//---------------------- validateClientArguments-------------------------------
// Ensures that the users input arguments are valid
// Preconditions : the arguments are valid integers
// Postconditions: exits the client program if invalid input is used
void validateClientArguments(int numItters, int numBufs, int numBufSize,
int writeType)
{
// we need to be assuredof 1500
if (numBufs * numBufSize != BUFFSIZE)
{
cerr << "Incorrect num byes " << numBufs * numBufSize
<< " != " << BUFFSIZE << endl;
exit(EXIT_FAILURE);
}
if (numItters <= 0)
{
cerr << "Incorrect number of Itters <= 0" << numItters << endl;
exit(EXIT_FAILURE);
}
if (writeType != MULTIPLE && writeType != ATOMIC_MULTIPLE &&
writeType != ONE_WRITE)
{
cerr << "Incorrect write type" << writeType << endl;
exit(EXIT_FAILURE);
}
}
//-------------------------- intializeMessage ---------------------------------
// Initalizes an arbitrary message that will be sent from the client to the
// server
// Preconditions : the values for numBufs and bufSize have been validated
// Postconditions: returns a matrix message
char **intializeMessage(int numBufs, int bufSize)
{
char **message = new char *[numBufs];
for (int i = 0; i < numBufs; i++)
{
message[i] = new char[bufSize];
for (int j = 0; j < bufSize; j++)
{
message[i][j] = 'z';
}
}
return message;
}
//-------------------------- freeMessage --------------------------------------
// Frees the message created to the sent to the server
// Preconditions : the message has been created and numBufs has been validated
// Postconditions: frees the memory of the message back to the OS and removes
// any dangling pointers
void freeMessage(char **message, int numBufs)
{
for (int i = 0; i < numBufs; i++)
{
delete[] message[i];
message[i] = nullptr;
}
delete[] message;
message = nullptr;
}
//-------------------------- getServerReads -----------------------------------
// Reads from the server the number of system read calls performed
// Preconditions : Assumes a valid server connection has been established
// Postconditions: returns the number of reads performed by the server
int getSeverReads(int clientSD)
{
int numRead = 0;
uint32_t serverNumReads;
while (numRead != sizeof(uint32_t))
{
numRead += read(clientSD, &serverNumReads, sizeof(uint32_t));
}
int numReads = ntohl(serverNumReads);
return numReads;
}
//----------------------- AlertServerOfReps -----------------------------------
// Writes to server the number of read itterations that need to be performed
// Preconditions : Assumes a valid server connection has been established
// Assumes that the number of itters is validated
// Postconditions: returns the number bytes written to the server
int alertServerOfReps(int clientSD, int numItters)
{
int numWritten = 0;
uint32_t clientNumItters = htonl(numItters);
while (numWritten < sizeof(uint32_t))
{
numWritten += write(clientSD, &clientNumItters, sizeof(uint32_t));
}
return numWritten;
}
//----------------------- MultipleWrites ----------------------------------
// Writes to server the message in databuf -- where each write writes
// bufsize number of bytes nbuf times
// Preconditions : Assumes a valid server connection has been established
// Assumes that the bufsize and nbufs are valid
// Postconditions: returns the number bytes written to the server
int multipleWrites(int clientSD, char **databuf, int bufsize, int nbufs)
{
int totalBytes = 0;
for (int i = 0; i < nbufs; i++)
{
int numBytesPerWrite = 0;
while (numBytesPerWrite != bufsize)
{
numBytesPerWrite += write(clientSD, databuf[i], bufsize);
}
totalBytes += numBytesPerWrite;
}
return totalBytes;
}
//----------------------- atomicMultipleWrites --------------------------------
// Writes to server the message in databuf -- where each write writes
// bufsize number of bytes nbuf times. Each write is atomic
// Preconditions : Assumes a valid server connection has been established
// Assumes that the bufsize and nbufs are valid
// Postconditions: returns the number bytes written to the server
int atomicMultipleWrites(int clientSD, char **databuf, int bufsize, int nbufs)
{
struct iovec vector[nbufs];
int totalBytes = 0;
for (int i = 0; i < nbufs; i++)
{
vector[i].iov_base = databuf[i];
vector[i].iov_len = bufsize;
}
while (totalBytes < BUFFSIZE)
{
totalBytes += writev(clientSD, vector, nbufs);
}
return totalBytes;
}
//----------------------- SingleWrite -----------------------------------------
// Writes to server the message in databuf -- only one write is performed
// Preconditions : Assumes a valid server connection has been established
// Assumes that the number of itters is validated
// Postconditions: returns the number bytes written to the server
int singleWrite(int clientSD, char **databuf, int bufsize, int nbufs)
{
int totalWritten = 0;
while (totalWritten != bufsize * nbufs)
{
totalWritten += write(clientSD, databuf, bufsize * nbufs);
}
return totalWritten;
}
//----------------------- WriteToServer ---------------------------------------
// Writes to the server a given the type of write and the number of itterations
// Preconditions : Assumes a valid server connection has been established
// Assumes that the number of itters is validated
// Postconditions: returns the number bytes written to the server
int writeToServer(int clientSD, char **databuf, int bufsize, int nbufs,
int type, int numItters)
{
int numBytesWritten = 0;
int ittersCompleted = 0;
switch (type)
{
case MULTIPLE:
while (ittersCompleted < numItters)
{
ittersCompleted++;
numBytesWritten += multipleWrites(clientSD, databuf, bufsize,
nbufs);
}
break;
case ATOMIC_MULTIPLE:
while (ittersCompleted < numItters)
{
ittersCompleted++;
numBytesWritten += atomicMultipleWrites(clientSD, databuf,
bufsize, nbufs);
}
break;
case ONE_WRITE:
while (ittersCompleted < numItters)
{
ittersCompleted++;
numBytesWritten += singleWrite(clientSD, databuf, bufsize, nbufs);
}
break;
default:
return -1;
}
return numBytesWritten;
}
//----------------------------------- main ------------------------------------
// Runs the client on a given port, establishes the connection and reads and
// writes to the server
// Preconditions : size of messages is always BUFFSIZE
// Postconditions: completes server client read writes
typedef chrono::_V2::steady_clock::time_point clockTime;
int main(int argc, char *argv[])
{
/*
* Argument validation
*/
if (argc != 7)
{
cerr << "Usage: " << argv[0] << "serverName" << endl;
return -1;
}
// store the argv arguments for clairty
char *serverName = argv[1];
char *serverPort = argv[2];
char *repetition = argv[3];
char *nbufs = argv[4];
char *bufsize = argv[5];
char *type = argv[6];
int numItters = atoi(repetition);
int numBufs = atoi(nbufs);
int numBufSize = atoi(bufsize);
int writeType = atoi(type);
validateClientArguments(numItters, numBufs, numBufSize, writeType);
char **dataBuf = intializeMessage(numBufs, numBufSize);
int clientSD = intializeClientSocket(serverName, serverPort);
int repsSent = alertServerOfReps(clientSD, numItters);
if (repsSent <= 0)
{
cerr << "Unable to send number of reps to server" << endl;
}
clockTime start = chrono::steady_clock::now();
int bytesWritten = writeToServer(clientSD, dataBuf, numBufSize, numBufs,
writeType, numItters);
int numReads = getSeverReads(clientSD);
clockTime end = chrono::steady_clock::now();
chrono::duration<double> time = end - start;
double gb = (8 * numItters * BUFFSIZE) / pow(10.0, 9.0);
double totalTime = time.count();
double gbs = gb / totalTime;
cout << "Test = " << type << " Total Time = " << totalTime << " μs,"
<< "#reads = " << numReads << ", throughput = " << gbs << " Gbps"
<< endl;
freeMessage(dataBuf, numBufs);
close(clientSD);
return 0;
}