-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
executable file
·192 lines (168 loc) · 5.09 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
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
#include <stdio.h> // fprintf(), perror(), fflush()
#include <stdlib.h> // atoi()
#include <assert.h> // assert()
#include <string.h> // memset(), memcmp(), strlen(), strcpy(), memcpy()
#include <unistd.h> // getopt(), STDIN_FILENO, gethostname()
#include <netdb.h> // gethostbyname()
#include <netinet/in.h> // struct in_addr
#include <arpa/inet.h> // htons()
#include <sys/types.h> // u_short
#include <sys/socket.h> // socket API
#include <sys/time.h>
#include <fstream>
#include <iostream>
#include <cstdio>
#include <ctime>
#include "client.h"
int sd; /* socket descriptor */
void client_usage(char *progname)
{
fprintf(stderr, "Usage: %s -s <server>:<port> -q <query_file>\n",
progname);
exit(1);
}
/*
* client_args: parses command line args.
* "*sname" : the server's name
* "port" : the port to connect at server, in network byte order.
* "*imagename" : the name of the image to search for
* "*vers" : the version used for query packet
*/
int client_args(int argc, char *argv[], char **sname, u_short *port, char *fname)
{
char c, *p;
extern char *optarg;
if (argc < 5) {
return (1);
}
while ((c = getopt(argc, argv, "s:q:")) != EOF) {
switch (c) {
case 's':
for (p = optarg+strlen(optarg)-1; // point to last character of addr:port arg
p != optarg && *p != CLIENT_PORTSEP; // search for ':' separating addr from port
p--);
net_assert((p == optarg), "client_args: server address malformed");
*p++ = '\0';
*port = htons((u_short) atoi(p)); // always stored in network byte order
*sname = optarg;
break;
case 'q':
memcpy(fname, optarg, strlen(optarg));
break;
default:
return(1);
break;
}
}
return (0);
}
/*
* client_sockinit: creates a new socket to connect to the provided server.
* The server's FQDN and port number are provided. The port number
* provided is assumed to already be in netowrk byte order.
*
* On success, the global socket descriptor sd is initialized.
* On error, terminates process.
*/
void client_sockinit(char *sname, u_short port)
{
/*
* create a new TCP socket, store the socket in the global variable sd
*/
if ((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
abort();
/*
* obtain the server's IPv4 address from sname and initialize the
* socket address with server's address and port number .
*/
struct sockaddr_in sin;
struct hostent *host = gethostbyname(sname);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = *(unsigned long *)host->h_addr_list[0];
sin.sin_port = port;
/* connect to server */
if (connect(sd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
abort();
return;
}
/*
* client_sendquery: send query to server
*/
int client_sendquery(char *query)
{
int bytes;
bytes = send(sd, query, QUERY_MAXLENGTH, 0);
if (bytes != QUERY_MAXLENGTH) {
return(0);
}
return(1);
}
/*
* client_sendquery: send termination message to server
*/
void client_sendtermination()
{
if (send(sd, "terminate", QUERY_MAXLENGTH, 0) < 0)
abort();
}
/*
* client_recvresponse: receive response from server
*/
int client_recvresponse(char* response)
{
if (recv(sd, response, RESPONSE_MAXLENGTH, 0) <= 0)
return 0;
else return 1;
}
int main(int argc, char *argv[])
{
int err;
char *server_name;
u_short port;
struct timeval startTime;
struct timeval endTime;
char query_file_name[100] = { 0 };
std::string result_file_name;
char query[QUERY_MAXLENGTH] = { 0 };
char response[RESPONSE_MAXLENGTH] = { 0 };
/* parse args */
if (client_args(argc, argv, &server_name, &port, query_file_name)) {
client_usage(argv[0]);
}
client_sockinit(server_name, port);
std::ifstream input_file(query_file_name);
result_file_name = "result/result_";
result_file_name.append(&query_file_name[4]);
result_file_name.replace(result_file_name.size()-3, 3, "txt");
// result_file_name.replace(result_file_name.size() - 3, 3, "result_parallel_P2P");
fprintf(stderr, "result file name = %s\n", result_file_name.c_str());
std::ofstream output_file(result_file_name);
if (input_file.is_open()) {
/* set the start time */
gettimeofday(&startTime, NULL);
/*
* read queries from input file line by line
* send each query to server and wait for the response
*/
while (!input_file.eof()) {
input_file.getline(query, QUERY_MAXLENGTH);
if (client_sendquery(query)) {
if (client_recvresponse(response) == 1) {
output_file << response << '\n';
} else {
fprintf(stderr, "query not found.\n");
}
}
else {
fprintf(stderr, "error in sending query.\n");
}
}
/* set the end time */
gettimeofday(&endTime, NULL);
output_file << "[client log:] end time = " << ((endTime.tv_sec - startTime.tv_sec)*1000000L+endTime.tv_usec - startTime.tv_usec)/1000000.0 << " s\n";
/* send the termination to server to close the connection */
client_sendtermination();
}
return(0);
}