forked from 0x300/cs202-pig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
481 lines (392 loc) · 16.7 KB
/
server.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*****************************************************************************/
/* Josh Lindoo */
/* Login ID: lind6441 */
/* CS-202, Winter 2015 */
/* Programming Assignment 7 */
/* Pig Server - server for playing the pig game! */
/*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <strings.h>
#include <signal.h>
#define HOSTLEN 256
#define BACKLOG 1
#define PORTNUM 50000
int serverEn = 1; //enable for server loop to allow for clean shutdowns
//function declarations
int make_server_socket_q(int , int );
/*****************************************************************************/
/* Function: make_server_socket */
/* Purpose: call the func to create a socket */
/* Parameters: int portnum */
/* Returns: int */
/*****************************************************************************/
int make_server_socket(int portnum) {
return make_server_socket_q(portnum, BACKLOG);
}
/*****************************************************************************/
/* Function: make_server_socket_q */
/* Purpose: make server socket with port and backlog */
/* Parameters: int portnum, int backlog */
/* Returns: int */
/*****************************************************************************/
int make_server_socket_q(int portnum, int backlog) {
struct sockaddr_in saddr; //socket address info
int sock_id; //server socket
//create server socket
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if ( sock_id == -1 ) return -1;
bzero((void *)&saddr, sizeof(saddr));
//socket configuration
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(portnum);
saddr.sin_family = AF_INET;
if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 ) return -1;
if ( listen(sock_id, backlog) != 0 ) return -1;
return sock_id;
}
/*****************************************************************************/
/* Function: handleUsernames */
/* Purpose: get usernames and send to opponents */
/* Parameters: int fd1, int fd2 */
/* Returns: void */
/*****************************************************************************/
void handleUsernames(int fd1, int fd2)
{
char username1[256], username2[256], response[256]; // client usernames and response
// get usernames
bzero( &username1, 256 );
recv(fd1, username1, 256, 0);
printf("username1: %s", username1);
bzero( &username2, 256 );
recv(fd2, username2, 256, 0);
printf("username2: %s\n", username2);
printf("Sending msg -> %s\n", username1);
write(fd2, username1, strlen(username1));
printf("Sending msg -> %s\n", username2);
write(fd1, username2, strlen(username2));
// wait for client response
printf("Waiting for responses..\n");
bzero( &response, 256 );
recv(fd1, response, 256, 0);
printf("response: %s\n", response);
bzero( &response, 256 );
recv(fd2, response, 256, 0);
printf("response: %s\n\n", response);
}
/*****************************************************************************/
/* Function: sendRoll */
/* Purpose: send the roll to the clients */
/* Parameters: int fd, int roll */
/* Returns: int */
/*****************************************************************************/
int sendRoll(int fd, int roll)
{
char msg[256], response[256]; // msg to be sent and client response
bzero( &msg, 256 );
sprintf(msg, "Roll: %d", roll);
printf("\n------ New Roll -----\n");
printf("Sending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
// recv returns 0 if disconnected, or -1 if error
// either essentially means the player is gone in most cases
bzero( &response, 256 );
recv(fd, response, 256, 0);
printf("response: %s\n\n", response);
return 1;
}
/*****************************************************************************/
/* Function: requestMove */
/* Purpose: send message to client to get user move */
/* Parameters: int fd */
/* Returns: int */
/*****************************************************************************/
int requestMove(int fd)
{
char msg[256], response[256]; // msg to be sent and client response
bzero( &msg, 256 );
sprintf(msg, "Move");
printf("\nSending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
// wait for client to respond with move
bzero( &response, 256 );
recv(fd, response, 256, 0);
printf("response: %s\n", response);
if(strncmp("1", response, 1) == 0)
{ //roll
return 1;
}
else if(strncmp("2", response, 1) == 0)
{ //hold
return 2;
}
else
{ //rubbish, but this shouldn't happen since this is checked client-side
//printf("WARN: Invalid move sent: %s\n", response);
return -1;
}
}
/*****************************************************************************/
/* Function: sendMoveToOpponent */
/* Purpose: send move to opponent */
/* Parameters: int fd, int move */
/* Returns: int */
/*****************************************************************************/
int sendMoveToOpponent(int fd, int move)
{
char msg[256], response[256]; // msg to be sent and client response
bzero( &msg, 256 );
if(move == 1)
{
sprintf(msg, "Your opponent decided to roll.");
}
else
{
sprintf(msg, "Your opponent decided to hold.");
}
printf("Sending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
// recv returns 0 if disconnected, or -1 if error
// either essentially means the player is gone in most cases
bzero( &response, 256 );
recv(fd, response, 256, 0);
printf("response: %s\n", response);
return 1;
}
/*****************************************************************************/
/* Function: sendLose */
/* Purpose: send lose message to loser */
/* Parameters: int fd, int winnerScore, int loserScore */
/* Returns: void */
/*****************************************************************************/
void sendWin(int fd, int winnerScore, int loserScore)
{
char msg[256]; // msg to be sent
bzero( &msg, 256 );
sprintf(msg, "You are the winner with a score of %d! Your opponent only had %d points.. nice work!"
, winnerScore, loserScore);
printf("Sending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
}
/*****************************************************************************/
/* Function: sendLose */
/* Purpose: send lose message to loser */
/* Parameters: int fd, int winnerScore, int loserScore */
/* Returns: void */
/*****************************************************************************/
void sendLose(int fd, int winnerScore, int loserScore)
{
char msg[256]; // msg to be sent
bzero( &msg, 256 );
sprintf(msg, "Sorry.. you lost. Try to get more points next time ;)\nYour score: %d, Your opponent's score: %d", loserScore, winnerScore);
printf("Sending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
}
/*****************************************************************************/
/* Function: sendTie */
/* Purpose: send tie message to players */
/* Parameters: int fd1, int fd2, int score */
/* Returns: void */
/*****************************************************************************/
void sendTie(int fd1, int fd2, int score)
{
char msg[256]; // msg to be sent
bzero( &msg, 256 );
sprintf(msg, "Wow! You both tied with a score of %d!! Nice.", score);
printf("Sending msg -> %s\n", msg);
write(fd1, msg, strlen(msg));
write(fd2, msg, strlen(msg));
}
/*****************************************************************************/
/* Function: sendScore */
/* Purpose: send the score to one of the players */
/* Parameters: int fd, int score, int opponentScore */
/* Returns: int */
/*****************************************************************************/
int sendScore(int fd, int score, int opponentScore)
{
char msg[256]; // msg to be sent
bzero( &msg, 256 );
sprintf(msg, "Your score: %d, Opponent's score: %d\n\n", score, opponentScore);
printf("Sending msg -> %s\n", msg);
write(fd, msg, strlen(msg));
}
/*****************************************************************************/
/* Function: runGame */
/* Purpose: handle game logic */
/* Parameters: int fd1, int fd2 */
/* Returns: void */
/*****************************************************************************/
void runGame(int fd1, int fd2)
{
int winning = 100;
int scoreP1 = 0;
int scoreP2 = 0;
int scoreRoundP1 = 0;
int scoreRoundP2 = 0;
int isStandingP1 = 1;
int isStandingP2 = 1;
// get client usernames and send back to opponent
handleUsernames(fd1,fd2);
while(1)
{
// send roll to each user
srand((unsigned) time(NULL));
int currentRoll = rand() % 6 + 1; // roll for this round
if( scoreP1 >= winning && scoreP2 >= winning )
{ //tie?
if(scoreP1 > scoreP2)
{ //P1 wins
sendWin(fd1, scoreP1, scoreP2);
sendLose(fd2, scoreP1, scoreP2);
}
else if(scoreP2 > scoreP1)
{ //P2 wins
sendWin(fd2, scoreP2, scoreP1);
sendLose(fd1, scoreP2, scoreP1);
}
else
{ //tie
sendTie(fd1, fd2, scoreP1);
}
exit(EXIT_SUCCESS);
}
else if( scoreP1 >= winning)
{ //P1 wins
sendWin(fd1, scoreP1, scoreP2);
sendLose(fd2, scoreP1, scoreP2);
exit(EXIT_SUCCESS);
}
else if( scoreP2 >= winning)
{ //P2 wins
sendWin(fd2, scoreP2, scoreP1);
sendLose(fd1, scoreP2, scoreP1);
exit(EXIT_SUCCESS);
}
else
{ //continue with round
// receive user moves
int moveP1 = 0;
int moveP2 = 0;
printf("Waiting for moves..\n");
if(isStandingP1) moveP1 = requestMove(fd1);
printf("moveP1 = %d\n", moveP1);
if(isStandingP2) moveP2 = requestMove(fd2);
printf("moveP2 = %d\n", moveP2);
//if player disconnected, give win to the other one
if(moveP1 == -1)
{
printf("P1 failed to make move, P2 wins\n");
sendWin(fd2, scoreP2, scoreP1);
exit(EXIT_SUCCESS);
}
if(moveP2 == -1)
{
printf("P2 failed to make move, P1 wins\n");
sendWin(fd1, scoreP1, scoreP2);
exit(EXIT_SUCCESS);
}
//if player sat.. they aren't standing anymore
if(moveP1 == 2) isStandingP1 = 0;
if(moveP2 == 2) isStandingP2 = 0;
sendMoveToOpponent(fd1, moveP2);
sendMoveToOpponent(fd2, moveP1);
printf("\nMoves -> user1: %d, user2: %d\n", moveP1, moveP2);
if( currentRoll == 1 || (!isStandingP1 && !isStandingP2 ||
scoreP1+scoreRoundP1 > winning || scoreP2+scoreRoundP2 > winning) )
{ //send end of round stuff
//add score if players already sat down
if(!isStandingP1) scoreP1 += scoreRoundP1;
if(!isStandingP2) scoreP2 += scoreRoundP2;
if(isStandingP1 || isStandingP2)
{ //don't show roll if round ended from two people sitting
//send roll
if(sendRoll(fd1, currentRoll) == -1)
{ // error/disconnect
sendWin(fd2, scoreP2, scoreP1);
}
if(sendRoll(fd2, currentRoll) == -1)
{ // error/disconnect
sendWin(fd1, scoreP1, scoreP2);
}
}
//send scores
sendScore(fd1, scoreP1, scoreP2);
sendScore(fd2, scoreP2, scoreP1);
//reset round scores
scoreRoundP1 = 0;
scoreRoundP2 = 0;
//reset players to standing
isStandingP1 = 1;
isStandingP2 = 1;
}
else
{
//add roll to round scores
if(isStandingP1) scoreRoundP1 += currentRoll;
if(isStandingP2) scoreRoundP2 += currentRoll;
//send roll
if(sendRoll(fd1, currentRoll) == -1)
{ // error/disconnect
sendWin(fd2, scoreP2, scoreP1);
}
if(sendRoll(fd2, currentRoll) == -1)
{ // error/disconnect
sendWin(fd1, scoreP1, scoreP2);
}
}
}
}
}
/*****************************************************************************/
/* Function: forkGame */
/* Purpose: fork the process so multiple games can happen */
/* Parameters: */
/* Returns: void */
/*****************************************************************************/
void forkGame(int fd1, int fd2)
{
if( fork() == 0) runGame(fd1,fd2);
//otherwise go back to receiving connections
}
/*****************************************************************************/
/* Function: main */
/* Purpose: accept client connections and start games */
/* Parameters: */
/* Returns: int */
/*****************************************************************************/
int main()
{
int sock_id; // server socket id
if ((sock_id = make_server_socket(PORTNUM)) == -1)
{
perror("Error opening socket");
exit(1);
}
while(1)
{
int fd1, fd2; // client sockets
// accept connections
fd1 = accept(sock_id, NULL, NULL);
if ( fd1 == -1)
{
exit(EXIT_FAILURE);
}
printf("One client has connected.. waiting for another.\n");
fd2 = accept(sock_id, NULL, NULL);
if (fd2 == -1)
{
exit(EXIT_FAILURE);
}
// let the games begin!
printf("Starting new game!\n\n");
forkGame(fd1, fd2);
}
return 0;
}