-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_client.cpp
More file actions
434 lines (362 loc) · 10.8 KB
/
Copy pathgame_client.cpp
File metadata and controls
434 lines (362 loc) · 10.8 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
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <cctype>
using namespace std;
int getServerAdd (char* argv[], struct sockaddr_in* addr);
int sendHelp(int sock, char* buffer, unsigned long numB);
int recvHelp(int sock, char* buffer, unsigned long numB);
void sendName (int sock);
void sendGrade (int sock, double grade);
int recvWordLen(int sock);
void recvLeaderBoard (int sock);
void sendGuess (int sock, char guess);
void modifyWord (char* w, char* indexChar, int arrS, char guess);
void recvResult (int sock, char* w, char guess, int& restIndex);
const unsigned int NAME_SIZE = 1500;
const unsigned int WORD_SIZE = 1500;
const unsigned int GUESS_TIMES = 1500;
int main (int argc, char* argv[])
{
if (argc < 3 || argc > 3) {
cout << "Usage: " << argv[0] << " server_host server_port\n";
return 1;
}
struct sockaddr_in serverAdd;
if (getServerAdd (argv, &serverAdd) < 0) {
cout << "ERROR: failed to look up DNS for " << argv[1] << ":" << argv[2] << endl;
return 1;
}
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
return 1;
}
if (connect(sock, (struct sockaddr*) &serverAdd, sizeof(serverAdd)) < 0) {
perror("connect");
return 1;
}
cout << "Successfully connected with the server...\n";
// send Name first
sendName (sock);
// receive the length of the word
int wordL = recvWordLen(sock);
int indexToGuess = wordL;
if (wordL == -1)
exit (1);
char* word = new char [wordL + 1];
word[wordL] = '\0';
// start game
int turn = 0;
string guessInput;
char guess;
while (indexToGuess > 0 && turn < GUESS_TIMES) {
turn++;
cout << "Turn " << turn << endl;
cout << "Word: " << word << endl;
if (turn == 1)
memset (word, '-', wordL);
cout << "Enter your guess (1 character only): ";
getline (cin, guessInput);
while (guessInput.length() > 1) {
cout << "Please enter your guess again (1 character only): ";
getline (cin, guessInput);
}
guess = toupper(guessInput[0]);
// send guess
sendGuess (sock, guess);
// recv result of guess as an array
// if 1 then correct -> modifyWord. Else nothing
recvResult (sock, word, guess, indexToGuess);
}
if (turn >= GUESS_TIMES && indexToGuess > 0) {
cout << "\nYou reach the limit of how many guesses you can make!\n";
cout << "You failed to guess the word\n";
return 1;
}
// send grades to the server
sendGrade (sock, (double)turn/wordL);
cout << "Congratualtions! You guessed the word " << word << endl; // the word
cout << "It took " << turn << " turns to guess the word correctly.\n\n";
// show leaderboard
cout << "Leader Board:\n";
cout << "Closing connection...\n";
close (sock);
delete word;
return 0;
}
int recvWordLen(int sock) // without the \n
{
int wordL;
if (recvHelp(sock, (char*) &wordL, sizeof(wordL)) < 0) {
cout << "ERROR: failed to receive word's length\n";
return -1;
}
wordL = ntohl(wordL);
if (wordL <= 0) {
cout << "ERROR: received invalid word's length value " << wordL << " from the server\n";
errno = EINVAL;
return -1;
}
return wordL;
}
void modifyWord (char* w, char* indexChar, int arrS, char guess)
{
int w_i = 0;
for (int i = 0; i < arrS; i++) {
w_i = indexChar[i] - '0';
w[w_i] = guess;
}
}
void sendGuess (int sock, char guess)
{
char send_buffer [1] = {guess};
int sendLen = sendHelp(sock, send_buffer, 1);
if (sendLen < 0) {
cout << "ERROR: failed to send guess\n";
exit(1);
}
}
void recvResult (int sock, char* w, char guess, int& restIndex)
{
// will receive an array
// first index indicates whether the guess is correct (1 if correct, 0 is non)
// if correct, the next consecutive indexes will be the indexes of where the correct guess should be
// recv the length of array first
int arrS = 0;
if (recvHelp(sock, (char*) &arrS, sizeof(arrS)) < 0) {
cout << "ERROR: failed to receive the guess response array's length\n";
exit(1);
}
arrS = ntohl(arrS);
if (arrS <= 0) {
cout << "ERROR: received invalid array length of the guess response array " << arrS << " from the server\n";
errno = EINVAL;
exit(1);
}
// recv the array
char* arr = new char [arrS];
if (recvHelp(sock, arr, arrS) < 0) {
cout << "ERROR: failed to receive the guess response array\n";
delete arr;
exit(1);
}
int i_guessRsp = 0;
if (arr[0] == '1') {
restIndex -= (arrS - 1);
char* guessResponse = new char [arrS - 1];
for (int i = 1; i < arrS; i++) {
arr[i_guessRsp] = guessResponse[i];
i_guessRsp++;
}
modifyWord (w, guessResponse, arrS - 1, guess);
delete guessResponse;
}
delete arr;
}
void sendGrade (int sock, double grade)
{
// convert grade to string
string gr = to_string(grade);
// send length first
int len = htonl((int)gr.length()); // not including null-terminated
int sendLen = sendHelp(sock, (char*) &len, sizeof(len));
if (sendLen < 0) {
cout << "ERROR: failed to send grade string length\n";
exit(1);
}
// send grade
char* gradeStr = new char [gr.length()];
strncpy(gradeStr, gr.c_str(), gr.length());
sendLen = sendHelp(sock, gradeStr, (int) gr.length());
if (sendLen < 0) {
cout << "ERROR: failed to send grade string\n";
delete gradeStr;
exit(1);
}
delete gradeStr;
}
void recvLeaderBoard (int sock)
{
int boardLen = 0;
if (recvHelp(sock, (char*) &boardLen, sizeof(boardLen)) < 0) {
cout << "ERROR: failed to receive the leader board's length\n";
exit(1);
}
boardLen = ntohl(boardLen);
if (boardLen <= 0) {
cout << "ERROR: received invalid length of leader board " << arrS << " from the server\n";
errno = EINVAL;
exit(1);
}
// recv the array
char* boardName = new char [boardLen];
double* boardGrade = new char [boardLen];
int counter = 0;
while (counter != boardLen) {
counter++;
// recv name of the first, the second, third (or less)
int nameLen = 0;
if (recvHelp(sock, (char*) &nameLen, sizeof(nameLen)) < 0) {
cout << "ERROR: failed to receive the length of client's name who is in rank " << counter << "\n";
exit(1);
}
nameLen = ntohl(nameLen);
if (counter <= 0) {
cout << "ERROR: received invalid length of client's name who is in rank " << counter << " from the server\n";
errno = EINVAL;
exit(1);
}
char* name = new char [nameLen + 1];
if (recvHelp(sock, name, nameLen) < 0) {
cout << "ERROR: failed to receive the guess response array\n";
delete name;
exit(1);
}
name[nameLen] = '\0';
// receive grade
int gradeL = 0;
if (recvHelp(sock, (char*) &gradeL, sizeof(gradeL)) < 0) {
cout << "ERROR: failed to receive grade string length of client in rank " << counter << "\n";
exit(1);
}
gradeL = ntohl(gradeL);
if (gradeL <= 0) {
cout << "ERROR: received invalid grade string length value " << gradeL << " of client in rank " << counter << " from the server\n";
errno = EINVAL;
exit(1);
}
// recv the grade as string
char* gr = new char [gradeL + 1];
if (recvHelp(sock, gr, gradeL) < 0) {
cout << "ERROR: failed to receive the grade of client in rank " << counter << "\n";
delete gr;
exit(1);
}
gr[gradeL] = '\0';
// convert string to double
double grade = atof (gr);
// print the record right here
cout << setw(6) << counter << ". " << name << setw (4) << grade << endl;
delete gr;
delete name;
}
}
int getServerAdd (char* argv[], struct sockaddr_in* addr)
{
if (argv[1] == NULL || argv[2] == NULL || addr == NULL) {
cout << "ERROR: argument is NULL\n";
exit(1);
}
// check passed-in port number first
char* endptr = NULL;
unsigned long portN = strtoul(argv[2], &endptr, 10);
if (endptr != NULL && *endptr != '\0') {
cout << "ERROR: " << argv[2] << " is not a number\n";
exit(1);
}
else if (portN == ULONG_MAX && errno == ERANGE) {
cout << "ERROR: " << strerror(ERANGE)<< ": " << argv[1] << endl;
exit(1);
}
else if (portN > USHRT_MAX || portN < 0) {
cout << "ERROR: " << strerror(ERANGE) << ": " << portN << " is not a valid port number\n";
exit(1);
}
// check the IP
struct addrinfo* addrL = NULL;
int internetAddr = getaddrinfo(argv[1], argv[2], NULL, &addrL);
if (internetAddr != 0) {
cout << "getaddrinfo: " << gai_strerror(internetAddr) << endl;
exit(1);
}
struct addrinfo* addr_temp = addrL;
while (addr_temp != NULL && addr_temp->ai_family != AF_INET) {
addr_temp = addr_temp->ai_next;
}
if (addr_temp == NULL) {
cout << "ERROR: Address not found\n";
freeaddrinfo(addrL);
exit(1);
}
assert(addr_temp->ai_addrlen == sizeof(struct sockaddr_in));
memcpy(addr, addr_temp->ai_addr, sizeof(*addr));
freeaddrinfo(addrL);
return internetAddr;
}
int sendHelp(int sock, char* buffer, unsigned long numB) {
int sendLen = 0;
while (numB > 0) {
sendLen = send(sock, buffer, numB, 0);
if (sendLen < 0) {
perror("send");
return -1;
}
else if (sendLen == 0) {
cout << "ERROR: client closed the connection unexpectedly\n";
return -1;
}
assert(sendLen > 0);
buffer += sendLen;
numB -= sendLen;
assert(numB >= 0);
}
return 0;
}
int recvHelp(int sock, char* buffer, unsigned long numB) {
int recvLen;
while (numB > 0) {
recvLen = recv(sock, buffer, numB, 0);
if (recvLen < 0) {
perror("recv");
return -1;
}
else if (recvLen == 0) {
cout << "ERROR: client closed the connection unexpectedly\n";
return -1;
}
assert(recvLen > 0);
buffer += recvLen;
numB -= recvLen;
assert(numB >= 0);
}
return 0;
}
void sendName (int sock)
{
char* name;
unsigned long len = 0;
cout << "Enter your name: ";
ssize_t readLen = getline(&name, &len, stdin);
if (readLen == -1) {
perror("getline");
exit(1);
}
if (readLen > NAME_SIZE) {
cout << "ERROR: message exceeds maximum length\n";
exit(1);
}
if (name[readLen - 1] == '\n')
name[readLen - 1] = '\0';
// send the length first
int nameLen = htonl(readLen - 1);
int sendLen = sendHelp(sock, (char*) &nameLen, sizeof(nameLen));
if (sendLen < 0) {
cout << "ERROR: failed to send string length\n";
exit(1);
}
// send name
sendLen = sendHelp(sock, name, readLen - 1);
if (sendLen < 0) {
cout << "ERROR: failed to send the string\n";
exit(1);
}
}