-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtcpclient.cpp
More file actions
447 lines (376 loc) · 11.4 KB
/
tcpclient.cpp
File metadata and controls
447 lines (376 loc) · 11.4 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */
#include <string.h> /* memset() */
#include <stdlib.h>
#include <sys/time.h> /* select() */
#include <netinet/tcp.h>
#include <getopt.h> /* Variable calling of main */
#include <iostream>
#include <time.h>
#include <math.h>
#include "rnd.h"
#include "rndunif.h"
#include "rndexp.h"
#include "rnddet.h"
#include "rndunid.h"
#include "udpgen.h"
using namespace std;
void uPause(double noUsec);
void closePrg(int sig);
void killPrg(int sig);
double estimateCPU(int samples, int sleeptime, char* fname);
static inline u_int64_t realcc(void){
u_int64_t cc = __builtin_ia32_rdtsc();
return cc;
}
struct timeval *s;
struct timeval start,data,stop;
double runPkts,runPkts_1;
int size,noBreak,size1;
int difference_size,sample_length;
#define MAX_MSG 65536
int SERVER_PORT=1500;
int main (int argc, char *argv[]) {
int option_index,op,sd, rc, hflag, reqFlag;
double waittime,sleepTime,waittime1, sleepTime1;
struct sockaddr_in servAddr; //cliAddr,
struct sockaddr_in myAddr;
struct hostent *h;
char *serverName=0;
u_int32_t exp_id,run_id,key_id;
exp_id=run_id=key_id=0;
sample_length=1;
// double CPU_before, CPU_after;
struct timeval PktDept; //GTOD_before, GTOD_after,
// u_int64_t TSC_before,TSC_after;
int direction=0;
int runType; /* 0= default, forever, 1= nopkts, 2=time */
noBreak=1;
waittime1= 1000000;
char psd;
char wtd;
psd ='x';
wtd ='x';
static struct option long_options[] = {
{"expid ",required_argument,0,'e'},
{"keyid ",required_argument,0,'r'},
{"runid ",required_argument,0,'k'},
{"server",required_argument, 0, 's'},
{"port", required_argument,0,'p'},
{"pkts", required_argument,0,'n'},
{"pktdist", required_argument,0,'m'},
{"pktLenmin", required_argument, 0, 'l'},
{"pktLenMax", required_argument, 0, 'L'},
{"waittimemin", required_argument, 0, 'w'},
{"waittimemax", required_argument, 0, 'W'},
{"waitdist", required_argument, 0, 'v'},
{"samplelength", required_argument, 0, 'z'},
{"down", required_argument, 0, 'd'},
{"help", required_argument, 0, 'h'},
{0, 0, 0, 0}
};
/* check command line args, so that we are atleast in the correct "area" */
if(argc<1){
printf("use %s -h for help\n",argv[0]);
exit(EXIT_FAILURE);
}
hflag=0;
runType=0;
reqFlag=0;
size=1224;
size1 = 1224;
sleepTime=-1;
waittime1 = 1000000;
waittime=0;
while ( (op =getopt_long(argc, argv, "k:e:r:s:p:m:n:l:L:v:w:W:z:d:h",long_options, &option_index))!=EOF) {
switch (op){
case 'e':/*exp_id*/
exp_id=(u_int32_t)atoi(optarg);
reqFlag++;
break;
case 'r':/*exp_id*/
run_id=(u_int32_t)atoi(optarg);
reqFlag++;
break;
case 'k':/*key_id*/
key_id=(u_int32_t)atoi(optarg);
reqFlag++;
break;
case 's': /* Server */
serverName=optarg;
reqFlag++;
break;
case 'p': /* Port number */
SERVER_PORT=atoi(optarg);
break;
case 'n': /* number of pkts */
runPkts=atof(optarg);
runType=1;
break;
break;
case 'm': /*pkt size distribution*/
psd=*optarg;
cout <<" PSD is"<<psd <<"\n" ;
break;
case 'l': /*pkt length*/
size=atoi(optarg);
break;
case 'L': /*pkt length maxima*/
size1=atoi(optarg);
cout<< "Max packet Size is "<<size1 <<"\n";
break;
case 'd': /* download */
direction=1;
break;
case 'v': /*waittime distribution*/
wtd=*optarg;
break;
case 'w': /*waittime*/
sleepTime=atoi(optarg);
waittime=sleepTime;
reqFlag=4;
break;
case 'W': /*wait time maxima*/
sleepTime1=atoi(optarg);
cout<<"sleeptime Max is "<<sleepTime<<"\n";
waittime1=sleepTime1;
reqFlag=4;
break;
case 'z': /*sample_length*/
sample_length=atoi(optarg);
cout<<"sample_length is "<<sample_length<<"\n";
break;
case 'h': /*Help*/
hflag=1;
printf("%s\n",argv[0]);
printf(" (C)2003 Patrik.Carlsson@bth.se\n http://www.bth.se/its/staff/pca\n");
printf(" This tool was developed in the INGA project, based on funding from VINNOVA.\n\n");
printf(" -h help (this text)\n");
printf(" -e(--expid) Experiment id [required]\n");
printf(" -r(--runid) Run id [required]\n");
printf(" -k(--keyid) Key id [required]\n");
printf(" -s (--server) Destination Server [required] \n");
printf(" -p (--port) <Destination Port> [optional default = 1500] \n");
printf(" -n (--pkts) <Number of packets to send> [optional default = forever]\n");
printf(" -l (--pktLen) <Packet Length> [bytes] [optional default = 1224]\n\n");
printf(" -m (--pktsize distribution) e- exponential u- uniform d- discrete uniform default- deterministic\n\n");
printf(" -w (--waittime) <Inter frame gap, in usec.> [optional, but if set, voids desired]\n");
printf(" -v (--wait time distribution) e- exponential u- uniform d- discrete uniform default- deterministic\n\n");
printf(" -d (--down) Download, do not upload.\n");
printf(" -z Enter the sample length (integer)\n");
printf(" The -t and -n options are exclusive, if both are defined unknown behaviour might occur.\n");
printf(" If neither is defined the software will run forever, or atleast until terminated. \n\n");
break;
default:
hflag=1;
printf("Use -h for instructions.\n");
break;
}
}
if (hflag) exit(EXIT_SUCCESS);
if (reqFlag<4) {
printf("Missing required arguments.\nRun %s -h for arguments.\n",argv[0]);
exit(EXIT_FAILURE);
}
RND* myRND1;// packet size distribution
RND* myRND2; // wait time distribution
switch(psd){
case 'e':
printf("Expontial...");
myRND1=new RNDEXP(size1);
break;
case 'u':
printf("Uniform...");
myRND1=new RNDUNIF(size,size1);
break;
case 'd':
printf ("uniform discrete");
myRND1 = new RNDUNID(size,size1);
break;
default:
printf("DEfault is to deterministic ");
myRND1=new RNDDET(size1);
break;
}
switch(wtd){
case 'e':
printf("Expontial...");
myRND2=new RNDEXP(waittime1);
break;
case 'u':
printf("Uniform...");
myRND2=new RNDUNIF(waittime,waittime1);
break;
case 'd':
printf("uniform discrete");
myRND2=new RNDUNID(waittime,waittime1);
break;
default:
printf("DEfaults to determ");
myRND2=new RNDDET(waittime1);
break;
}
(*myRND1).printseed();
(*myRND2).printseed();
double pps= (1e6/(double)(waittime1));
/* get server IP address (no check if input is IP address or DNS name */
h = gethostbyname(serverName);
if(h==NULL) {
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket to");
exit(1);
}
/* bind any port number */
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
myAddr.sin_port = htons(0);
printf("Connecting to: %s:%d \n", serverName, SERVER_PORT);;
rc = bind(sd, (struct sockaddr *) &myAddr, sizeof(myAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error ");
exit(1);
}
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
printf("Connected.\n");
int flag=1;
int resultTCP=setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
if(resultTCP<0){
perror("problem.");
}
transfer_data sender;
u_int64_t istart,istop;//,istart0,istop0;/*Var used for send start-stop time*/
string test(1483,'x');
strcpy(sender.junk, test.c_str());
//----------------
difference_size = size1 -size;
runPkts_1 = floor (((difference_size)*runPkts)/sample_length) + runPkts;
printf(" difference_size = %d sample_length = %d \n", difference_size, sample_length);
printf("will run %g pkts, each %d bytes.\n",runPkts,size);
cout << "Experiment will run an overall of " << runPkts_1 <<" samples" << endl;
double di=0;
sender.exp_id=htonl(exp_id);
sender.run_id=htonl(run_id);
sender.key_id=htonl(key_id);
printf("Sending:\n");
printf("Experiment id=%d, run id=%d and key id = %d\n", exp_id,run_id,key_id);
/*
printf("HORD:%d:%d:%d\n", exp_id,run_id,key_id);
printf("NORD%d:%d:%d\n", sender.exp_id,sender.run_id,sender.key_id);
*/
PktDept.tv_sec=0;
PktDept.tv_usec=0;
istart=0;
istop=0;
printf("should take %g seconds .\n", (double)(runPkts_1)/pps);
while(di<runPkts_1){
// size=int(myRND1->Rnd());
// cout<< size<<"\n";
waittime = (int) (myRND2->Rnd());
//cout<<waittime<<" wait time\n";
//waittime = waittime*1000;
sender.counter=htonl((int)di);
sender.starttime=istart;
sender.stoptime= istop;
sender.depttime.tv_sec=PktDept.tv_sec;
sender.depttime.tv_usec=PktDept.tv_usec;
istart=realcc();
rc =write(sd, &sender,size);
istop=realcc();
gettimeofday(&PktDept,NULL);
// cout<<PktDept.tv_sec<<"."<<PktDept.tv_usec <<"\n";
if(rc<0) {
printf("%s: cannot send data, Packet N# %d, size was %d bytes, sender %p \n",argv[0],(int)(di-1), size,&sender );
close(sd);
exit(1);
}
printf("%d\t %d bytes, %llu\t %llu\n", ntohl(sender.counter), rc, istart,istop);
di++;
if (int (di) %(int)runPkts == 0)
{
size = size + sample_length;
//sleep(1);
}
if(int(di)%1000==0) {
cout << di << " pkts." <<endl;
}
uPause(waittime);
}
//----------------
printf("Closing.\n");
close(sd);
return 0;
}
void uPause(double noUsec){
// printf(">uPause(%g)\t", noUsec);
struct timeval s;
struct timeval e;
int secJump=0;
int loops=0;
int secs=(int)floor((double)noUsec/1000000.0);
double usecs=noUsec-secs*1000000;
if(noUsec>10000) {
// printf("Look Im slow.\n");
usleep(noUsec);
} else {
gettimeofday(&s,NULL);
e.tv_sec=s.tv_sec+secs;
if(secs>0) {
secJump=1;
if(secs>1){
printf("Sec %d s!!",secs);
}
}
if(s.tv_usec+usecs>1000000) {
e.tv_sec=e.tv_sec+1;
e.tv_usec=s.tv_usec+(long int)usecs-1000000;
secJump=1;
} else {
e.tv_usec=s.tv_usec+(long int)usecs;
}
if(secJump==1) {
while(s.tv_sec<e.tv_sec && loops<1000000 ){
gettimeofday(&s,NULL);
loops++;
}
if(loops>=1000000){
printf("sec loops 100000.\n ");
}
}
loops=0;
while(s.tv_sec<=e.tv_sec && s.tv_usec<e.tv_usec && loops<1000000 ){
gettimeofday(&s,NULL);
loops++;
}
if(loops>=1000000){
printf("usec loops 100000.\n ");
printf("Current %06ld s target %06ld s \t ",s.tv_sec,e.tv_sec );
printf("Current %06ld target us too %06ld us\n",s.tv_usec,e.tv_usec );
}
if(s.tv_sec>e.tv_sec){
printf("s sec > e sec.\n ");
printf("Current %06ld s target %06ld s \t ",s.tv_sec,e.tv_sec );
printf("Current %06ld target us too %06ld us\n",s.tv_usec,e.tv_usec );
}
gettimeofday(&s,NULL);
}
// printf("<uPause(%g)\n", noUsec);
}