-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
173 lines (139 loc) · 3.72 KB
/
Copy pathClient.cpp
File metadata and controls
173 lines (139 loc) · 3.72 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
#include "server_client_common.h"
using namespace std;
using u32 = uint32_t;
int send_request_to(char *request, u32 ip, u32 port);
int set_connection(u32 ip, u32 port);
int display_response(int sd);
bool check_exit(char *request);
void trim(char **str);
int send_string_to(int to, const char *from);
int read_res_type(int from, char &store);
int write_req_type(int from, char store);
int redirect_to(int &sd);
int prepare_request(char *request_buffer, char &empty_command);
static u32 ip;
static u32 port;
int main(int argc, char *argv[])
{
std::signal(SIGPIPE, SIG_IGN);
if (argc < 3)
{
printf("%s <server_aaddress> <port>\n", argv[0]);
return -1;
}
port = htons(atoi(argv[2]));
ip = inet_addr(argv[1]);
char *request = (char *)malloc(REQUEST_MAXLEN);
request[0] = 0;
int sd = set_connection(ip, port);
char running = 1;
char empty_command = 0;
while (running)
{
if (!empty_command)
printf("Enter command : ");
fflush(stdout);
if (prepare_request(request, empty_command) != 1)
continue;
char type = responseType::REDIRECT;
char reqtype = requestType::CLIENT_REQUEST;
while (type == responseType::REDIRECT)
{
write_req_type(sd, reqtype);
send_string_to(sd, request);
read_res_type(sd, type);
if (type == responseType::REDIRECT)
{
redirect_to(sd);
}
else if (type == responseType::OK)
{
display_response(sd);
}
else if (type == responseType::UNRECOGNIZED)
{
printf("Unrecognized command\n");
fflush(stdout);
}
}
}
}
int display_response(int sd)
{
char response[RESPONSE_MAXLEN];
read_string_from(sd, response, RESPONSE_MAXLEN);
printf("%s\n", response);
fflush(stdout);
return 0;
}
bool check_exit(char *request)
{
if (strcmp(request, "exit") == 0)
exit(0);
return 0;
}
void trim(char **str)
{
char *start = *str;
while (*start == ' ')
{
start++;
}
*str = start;
char *end = *str + strlen(*str) - 1;
while (end > start && *end == ' ')
{
end--;
}
*(end + 1) = 0;
}
int read_res_type(int from, char &store)
{
if (read(from, &store, 1) < 0)
HANDLE_EXIT("error when reading response type from server.\n");
return 0;
}
int write_req_type(int to, char store)
{
if (write(to, &store, 1) <= 0)
HANDLE_EXIT("error when sending request to server.\n");
return 0;
}
int redirect_to(int &sd)
{
u32 redirect_ip, redirect_port;
if (read(sd, &redirect_ip, 4) < 0)
HANDLE_EXIT("error when reading redirect server address .\n");
if (read(sd, &redirect_port, 4) < 0)
HANDLE_EXIT("error when reading redirect server address .\n");
struct in_addr addr;
addr.s_addr = ip;
char ipString[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr, ipString, sizeof(ipString));
printf("Redirecting to %s:%d\n", ipString, ntohs(redirect_port));
if (ntohl(redirect_ip) != ip || ntohs(redirect_port) != port)
{
close(sd);
sd = set_connection(redirect_ip, redirect_port);
}
return 0;
}
int prepare_request(char *request, char &empty_command)
{
bzero(request, REQUEST_MAXLEN);
read(0, request, REQUEST_MAXLEN);
if (request[strlen(request) - 1] == '\n')
request[strlen(request) - 1] = 0;
trim(&request);
check_exit(request);
int request_len = strlen(request);
if (request_len == 0)
{
empty_command = 1;
printf("\n");
return 0;
}
else
empty_command = 0;
return 1;
}