-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
95 lines (86 loc) · 2.67 KB
/
Copy pathclient.cpp
File metadata and controls
95 lines (86 loc) · 2.67 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string>
#include <iostream>
using namespace std;
#define MAXBUFLEN 10000
#define AWS_TCP_PORT 24472 // the port client will be connecting to
int main(int argc, char *argv[])
{
printf("The client is up and running.\n");
// need below for socket
int sockfd, numbytes;
char buf[MAXBUFLEN];
struct addrinfo hints, *servinfo, *p;
int rv;
if (argc != 4)
{
fprintf(stderr, "Input error!\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// connect to aws
if ((rv = getaddrinfo("127.0.0.1", to_string(AWS_TCP_PORT).c_str(), &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
perror("client: connect");
close(sockfd);
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "client: failed to connect\n");
return 2;
}
freeaddrinfo(servinfo); // all done with this structure
string map(argv[1]);
string src(argv[2]);
string fsize(argv[3]);
// send to aws
if (send(sockfd, (map + " " + src + " " + fsize).c_str(), MAXBUFLEN, 0) == -1)
perror("send");
struct sockaddr getPort;
socklen_t len = sizeof(getPort);
getsockname(sockfd, &getPort, &len);
printf("The client has sent query to AWS using TCP over port %d: start vertex %s; map %s; file size %s.\n", ((struct sockaddr_in *)&getPort)->sin_port, argv[2], argv[1], argv[3]);
// receive data from aws
if ((numbytes = recv(sockfd, buf, MAXBUFLEN - 1, 0)) == -1)
{
perror("recv");
exit(1);
}
printf("The client has received results from AWS:\n");
printf("-----------------------------------------------------\n");
printf("Destination\tMin Length\tTt\tTp\tDelay\n");
printf("-----------------------------------------------------\n");
buf[numbytes] = '\0';
cout << buf;
printf("-----------------------------------------------------\n");
close(sockfd); // close socket
return 0;
}