-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
320 lines (277 loc) · 12.6 KB
/
Copy pathserver.cpp
File metadata and controls
320 lines (277 loc) · 12.6 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
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include "utils.h"
using namespace std;
const int PORT = 24175;
const int MAX_CLIENTS = 2;
const int MAX_GAMES = 10;
// • Extra credit: Once the server accepted both players, it should create a new child process/thread that handles the rest of the game
// Function to handle a single game between two clients
void handleGame(int client1Socket, int client2Socket) {
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
// • Once the server accepts a connection from the client, it should inform the client which player (player one or player two) that he/she is.
string player1Msg = "1";
send(client1Socket, player1Msg.c_str(), player1Msg.length(), 0);
string player2Msg = "2";
send(client2Socket, player2Msg.c_str(), player2Msg.length(), 0);
int bytesReceived = recv(client1Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 1 ID" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
string player1ID(buffer);
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(client2Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 2 ID" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
string player2ID(buffer);
// • Once the server collects the names, it should print the statement "<player one ID> vs. <player two ID>: Game start".
cout << player1ID << " vs. " << player2ID << ": Game start" << endl;
int player1Score = 0;
int player2Score = 0;
int round1Player1Num = 0, round1Player2Num = 0;
int round2Player1Num = 0, round2Player2Num = 0;
int round1Player1Score = 0, round1Player2Score = 0;
int round2Player1Score = 0, round2Player2Score = 0;
// • Then the server should send a request to ask the two players to send in the first number.
string requestMsg = "REQUEST_NUMBER";
send(client1Socket, requestMsg.c_str(), requestMsg.length(), 0);
send(client2Socket, requestMsg.c_str(), requestMsg.length(), 0);
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(client1Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 1 number for round 1" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
round1Player1Num = stoi(string(buffer));
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(client2Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 2 number for round 1" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
round1Player2Num = stoi(string(buffer));
bool player1Valid = isValidPlayer1Number(round1Player1Num);
bool player2Valid = isValidPlayer2Number(round1Player2Num);
if (!player1Valid && !player2Valid) {
round1Player1Score = 0;
round1Player2Score = 0;
} else if (!player1Valid) {
round1Player1Score = 0;
round1Player2Score = 100;
} else if (!player2Valid) {
round1Player1Score = 100;
round1Player2Score = 0;
} else {
round1Player1Score = hcf(round1Player1Num, round1Player2Num);
int lcmValue = lcm(round1Player1Num, round1Player2Num);
round1Player2Score = getLastDigit(lcmValue);
}
player1Score += round1Player1Score;
player2Score += round1Player2Score;
// • Once the server receives the numbers, it should calculate the score for this round. And then sent the score of both players, together with the numbers both player picked, back to the client.
string round1Result = "ROUND1:" + to_string(round1Player1Num) + ":" +
to_string(round1Player2Num) + ":" +
to_string(round1Player1Score) + ":" +
to_string(round1Player2Score);
send(client1Socket, round1Result.c_str(), round1Result.length(), 0);
send(client2Socket, round1Result.c_str(), round1Result.length(), 0);
// • The server should then print the result of round 1: including the number selected, and the score of each player.
cout << "Round 1: Player 1 selected " << round1Player1Num
<< ", Player 2 selected " << round1Player2Num << endl;
cout << "Round 1: Player 1 score = " << round1Player1Score
<< ", Player 2 score = " << round1Player2Score << endl;
send(client1Socket, requestMsg.c_str(), requestMsg.length(), 0);
send(client2Socket, requestMsg.c_str(), requestMsg.length(), 0);
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(client2Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 2 number for round 2" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
round2Player2Num = stoi(string(buffer));
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(client1Socket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving Player 1 number for round 2" << endl;
close(client1Socket);
close(client2Socket);
return;
}
buffer[bytesReceived] = '\0';
round2Player1Num = stoi(string(buffer));
bool round2Player2Valid = isValidPlayer1Number(round2Player2Num);
bool round2Player1Valid = isValidPlayer2Number(round2Player1Num);
bool player1Duplicate = (round1Player1Num == round2Player1Num);
bool player2Duplicate = (round1Player2Num == round2Player2Num);
if ((!round2Player2Valid || player2Duplicate) && (!round2Player1Valid || player1Duplicate)) {
round2Player2Score = 0;
round2Player1Score = 0;
} else if (!round2Player2Valid || player2Duplicate) {
round2Player2Score = 0;
round2Player1Score = 100;
} else if (!round2Player1Valid || player1Duplicate) {
round2Player2Score = 100;
round2Player1Score = 0;
} else {
round2Player2Score = hcf(round2Player1Num, round2Player2Num);
int lcmValue = lcm(round2Player1Num, round2Player2Num);
round2Player1Score = getLastDigit(lcmValue);
}
player1Score += round2Player1Score;
player2Score += round2Player2Score;
// • Then the server should calculate the score for round 2, and send the following tp the players: The score of round 2 for each player, The total score for each player, Whether the player win/lose/draw the match (sending 1/-1/0 respectively)
int player1WinStatus = 0;
int player2WinStatus = 0;
if (player1Score > player2Score) {
player1WinStatus = 1;
player2WinStatus = -1;
} else if (player2Score > player1Score) {
player1WinStatus = -1;
player2WinStatus = 1;
} else {
player1WinStatus = 0;
player2WinStatus = 0;
}
string round2Result = "ROUND2:" + to_string(round2Player1Num) + ":" +
to_string(round2Player2Num) + ":" +
to_string(round2Player1Score) + ":" +
to_string(round2Player2Score) + ":" +
to_string(player1Score) + ":" +
to_string(player2Score) + ":" +
to_string(player1WinStatus);
send(client1Socket, round2Result.c_str(), round2Result.length(), 0);
string round2Result2 = "ROUND2:" + to_string(round2Player1Num) + ":" +
to_string(round2Player2Num) + ":" +
to_string(round2Player1Score) + ":" +
to_string(round2Player2Score) + ":" +
to_string(player1Score) + ":" +
to_string(player2Score) + ":" +
to_string(player2WinStatus);
send(client2Socket, round2Result2.c_str(), round2Result2.length(), 0);
// • The server should then print the result of round 2: including the number selected, and the score of each player, and then print out who is the winner.
cout << "Round 2: Player 1 selected " << round2Player1Num
<< ", Player 2 selected " << round2Player2Num << endl;
cout << "Round 2: Player 1 score = " << round2Player1Score
<< ", Player 2 score = " << round2Player2Score << endl;
cout << "Total: Player 1 = " << player1Score
<< ", Player 2 = " << player2Score << endl;
if (player1Score > player2Score) {
cout << "Winner: " << player1ID << endl;
} else if (player2Score > player1Score) {
cout << "Winner: " << player2ID << endl;
} else {
cout << "Result: Draw" << endl;
}
// • The server should also quit.
close(client1Socket);
close(client2Socket);
}
int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
cerr << "Error creating socket" << endl;
return 1;
}
int opt = 1;
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
cerr << "Error setting socket options" << endl;
close(serverSocket);
return 1;
}
struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (::bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
cerr << "Error binding socket to port " << PORT << endl;
close(serverSocket);
return 1;
}
// • The server should be started at the background, and it will listen for client connecting. (Use port 24175 for this program)
if (listen(serverSocket, MAX_CLIENTS) < 0) {
cerr << "Error listening on socket" << endl;
close(serverSocket);
return 1;
}
cout << "Server listening on port " << PORT << "..." << endl;
// • Extra credit: The server will keep running until 10 games have finished
int gamesCompleted = 0;
struct sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
while (gamesCompleted < MAX_GAMES) {
// Accept first client
int client1Socket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (client1Socket < 0) {
cerr << "Error accepting first client connection" << endl;
continue;
}
cout << "First client connected (Player 1) for game " << (gamesCompleted + 1) << endl;
// Accept second client
int client2Socket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (client2Socket < 0) {
cerr << "Error accepting second client connection" << endl;
close(client1Socket);
continue;
}
cout << "Second client connected (Player 2) for game " << (gamesCompleted + 1) << endl;
// • Extra credit: Once the server accepted both players, it should create a new child process/thread that handles the rest of the game
// Fork a child process to handle this game
pid_t pid = fork();
if (pid < 0) {
cerr << "Error forking process" << endl;
close(client1Socket);
close(client2Socket);
continue;
} else if (pid == 0) {
// Child process: handle the game
close(serverSocket); // Child doesn't need the listening socket
handleGame(client1Socket, client2Socket);
exit(0); // Child process exits after handling the game
} else {
// Parent process: close client sockets (child has copies) and continue listening
close(client1Socket);
close(client2Socket);
gamesCompleted++;
// • Extra credit: After that, the server process/thread should return and listen to the next player
// Clean up any finished child processes (non-blocking)
while (waitpid(-1, NULL, WNOHANG) > 0) {
// Reap any finished child processes
}
}
}
// Wait for all child processes to finish before exiting
cout << "Maximum games (" << MAX_GAMES << ") reached. Waiting for all games to finish..." << endl;
while (wait(NULL) > 0) {
// Wait for all child processes to complete
}
close(serverSocket);
cout << "Server shutting down." << endl;
return 0;
}