-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtt_pb_client.c
98 lines (76 loc) · 2.13 KB
/
rtt_pb_client.c
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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
typedef unsigned long long tick;
static __inline__ tick gettick (void) {
unsigned a, d;
__asm__ __volatile__("rdtsc": "=a" (a), "=d" (d) );
return (((tick)a) | (((tick)d) << 32));
}
int main(int argc, char *argv[])
{
int sockRes;
char* sendBuf;
struct hostent *host;
long bufSize, arrLen;
struct sockaddr_in serverAddress;
long maxIterations = 1000;
long count,i = 0;
long long t1, t2 = 0;
long double t;
char sBuffer[64], rBuffer[64];
host = gethostbyname("127.0.0.1");
if ((sockRes = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "socket creation Failure!\n");
exit(1);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(2154);
serverAddress.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
bzero(&(serverAddress.sin_zero),8);
if (connect(sockRes, (struct sockaddr *)&serverAddress,
sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "connect failed\n");
exit(1);
}
sBuffer[0] = 'b';
sBuffer[1] = '\0';
t1 = gettick();
for (count = 0; count < maxIterations; count++)
{
send(sockRes, sBuffer, 2, 0);
recv(sockRes, rBuffer, 2, 0);
rBuffer[1] = '\0';
}
t2 = gettick();
sBuffer[0] = 'q';
send(sockRes,sBuffer,strlen(sBuffer), 0);
long double overhead = ((long double) (t2 - t1)) / ((long double) maxIterations);
printf("Round-trip time = %.2LF us\n", overhead / (2.4e3));
fflush(stdout);
bufSize = 1073;
arrLen = bufSize / ((long) sizeof(char));
sendBuf = (char*) malloc(bufSize);
for (i = 0; i < arrLen-1; i++) {
sendBuf[i] = 'a';
}
sendBuf[arrLen-1] = '\0';
t1 = gettick();
send(sockRes, sendBuf, arrLen, 0);
recv(sockRes, rBuffer, 2, 0);
t2 = gettick();
printf("I am stuck");
free(sendBuf);
close(sockRes);
t = ((long double) bufSize) / (((long double) (t2 - t1)) - overhead);
printf("Bandwidth = %.2LF MB/sec\n", t * (2.4e9/1.0e6));
return 0;
}