-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
307 lines (274 loc) · 12.4 KB
/
Copy pathclient.cpp
File metadata and controls
307 lines (274 loc) · 12.4 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
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
const char* SERVER_IP = "127.0.0.1";
const int PORT = 24175;
int main() {
// Create socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket < 0) {
cerr << "Error creating socket" << endl;
return 1;
}
// • The client will send in a request to the server requesting a game (for this program, you always assume the client and server is on the same machine (so you should use "127.0.0.1" as the IP address)
// Set up server address
struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr) <= 0) {
cerr << "Invalid address/Address not supported" << endl;
close(clientSocket);
return 1;
}
// Connect to server
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
cerr << "Connection failed" << endl;
close(clientSocket);
return 1;
}
cout << "Connected to server" << endl;
// Receive player assignment (1 or 2)
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
int bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving player assignment" << endl;
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
int playerNumber = stoi(string(buffer));
cout << "You are Player " << playerNumber << endl;
// • The client should then send an ID (for this program, a single string) to the server for the record.
// Send player ID to server
string playerID;
cout << "Enter your ID: ";
cin >> playerID;
send(clientSocket, playerID.c_str(), playerID.length(), 0);
// ROUND 1
// Receive request for number
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving round 1 request" << endl;
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
// Get number input from user and send to server
int round1Number;
if (playerNumber == 1) {
cout << "Round 1: Enter a number (50-99, non-prime): ";
} else {
cout << "Round 1: Enter a number (60-99, non-prime): ";
}
cin >> round1Number;
string round1NumStr = to_string(round1Number);
send(clientSocket, round1NumStr.c_str(), round1NumStr.length(), 0);
// Receive round 1 results
// Keep reading until we get a message starting with "ROUND1"
string round1Result = "";
while (round1Result.find("ROUND1") != 0) {
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving round 1 results" << endl;
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
round1Result += string(buffer);
// If we've received too much data without finding ROUND1, something is wrong
if (round1Result.length() > 1000) {
cerr << "Error: Received too much data without finding ROUND1" << endl;
cerr << "Received so far: [" << round1Result << "]" << endl;
close(clientSocket);
return 1;
}
}
// Extract just the ROUND1 message (in case there's more data after it)
// Find the end of the ROUND1 message by counting colons (should have 4 colons total)
size_t round1End = round1Result.length();
int colonCount = 0;
for (size_t i = 0; i < round1Result.length(); i++) {
if (round1Result[i] == ':') {
colonCount++;
if (colonCount == 4) {
// Found the 4th colon, the number after it is the last field
// Find the end of that number (next non-digit or end of string)
size_t j = i + 1;
while (j < round1Result.length() && isdigit(round1Result[j])) {
j++;
}
round1End = j;
break;
}
}
}
round1Result = round1Result.substr(0, round1End);
// Debug: Print what we received
// cerr << "DEBUG: Received round1: [" << round1Result << "] (length: " << round1Result.length() << ")" << endl;
// Parse round 1 results: "ROUND1:num1:num2:score1:score2"
// Check if message starts with "ROUND1"
if (round1Result.find("ROUND1") != 0) {
cerr << "Error: Invalid round 1 result format - doesn't start with ROUND1" << endl;
cerr << "Received: [" << round1Result << "]" << endl;
close(clientSocket);
return 1;
}
// Parse format: "ROUND1:num1:num2:score1:score2"
// Find all colon positions
size_t pos = round1Result.find(':'); // First colon after "ROUND1"
if (pos == string::npos) {
cerr << "Error: Invalid round 1 result format - no colon found" << endl;
cerr << "Received: [" << round1Result << "]" << endl;
close(clientSocket);
return 1;
}
size_t pos2 = round1Result.find(':', pos + 1); // Second colon (after num1)
size_t pos3 = round1Result.find(':', pos2 + 1); // Third colon (after num2)
size_t pos4 = round1Result.find(':', pos3 + 1); // Fourth colon (after score1)
// Check that we found all required colons (4 colons total in the message)
if (pos2 == string::npos || pos3 == string::npos || pos4 == string::npos) {
cerr << "Error: Invalid round 1 result format - missing colons" << endl;
cerr << "Received: [" << round1Result << "]" << endl;
close(clientSocket);
return 1;
}
int round1Num1, round1Num2, round1Score1, round1Score2;
try {
round1Num1 = stoi(round1Result.substr(pos + 1, pos2 - pos - 1));
round1Num2 = stoi(round1Result.substr(pos2 + 1, pos3 - pos2 - 1));
round1Score1 = stoi(round1Result.substr(pos3 + 1, pos4 - pos3 - 1));
round1Score2 = stoi(round1Result.substr(pos4 + 1)); // score2 is everything after the 4th colon
cout << "Round 1: Player 1 selected " << round1Num1
<< ", Player 2 selected " << round1Num2 << endl;
cout << "Round 1: Player 1 score = " << round1Score1
<< ", Player 2 score = " << round1Score2 << endl;
} catch (const exception& e) {
cerr << "Error parsing round 1 results: " << e.what() << endl;
cerr << "Received: " << round1Result << endl;
close(clientSocket);
return 1;
}
// • Once the client receives the information, it should send in a number of round 2
// ROUND 2
// Receive request for number
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving round 2 request" << endl;
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
// Get number input from user and send to server
int round2Number;
if (playerNumber == 1) {
cout << "Round 2: Enter a number (60-99, non-prime): ";
} else {
cout << "Round 2: Enter a number (50-99, non-prime): ";
}
cin >> round2Number;
string round2NumStr = to_string(round2Number);
send(clientSocket, round2NumStr.c_str(), round2NumStr.length(), 0);
// Receive round 2 results with final scores and win status
// Keep reading until we get a message starting with "ROUND2"
string round2Result = "";
while (round2Result.find("ROUND2") != 0) {
memset(buffer, 0, sizeof(buffer));
bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesReceived <= 0) {
cerr << "Error receiving round 2 results" << endl;
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
round2Result += string(buffer);
// If we've received too much data without finding ROUND2, something is wrong
if (round2Result.length() > 1000) {
cerr << "Error: Received too much data without finding ROUND2" << endl;
cerr << "Received so far: [" << round2Result << "]" << endl;
close(clientSocket);
return 1;
}
}
// Extract just the ROUND2 message (in case there's more data after it)
size_t round2End = round2Result.find('\0');
if (round2End != string::npos && round2End < round2Result.length()) {
round2Result = round2Result.substr(0, round2End);
}
// Debug: Print what we received
// cerr << "DEBUG: Received round2: [" << round2Result << "] (length: " << round2Result.length() << ")" << endl;
// Parse round 2 results: "ROUND2:num1:num2:score1:score2:TOTAL1:TOTAL2:WIN_STATUS"
// Check if message starts with "ROUND2"
if (round2Result.find("ROUND2") != 0) {
cerr << "Error: Invalid round 2 result format - doesn't start with ROUND2" << endl;
cerr << "Received: [" << round2Result << "]" << endl;
close(clientSocket);
return 1;
}
// Parse format: "ROUND2:num1:num2:score1:score2:TOTAL1:TOTAL2:WIN_STATUS"
// Find all colon positions
pos = round2Result.find(':'); // First colon after "ROUND2"
if (pos == string::npos) {
cerr << "Error: Invalid round 2 result format - no colon found" << endl;
cerr << "Received: [" << round2Result << "]" << endl;
close(clientSocket);
return 1;
}
pos2 = round2Result.find(':', pos + 1); // Second colon (after num1)
pos3 = round2Result.find(':', pos2 + 1); // Third colon (after num2)
pos4 = round2Result.find(':', pos3 + 1); // Fourth colon (after score1)
size_t pos5 = round2Result.find(':', pos4 + 1); // Fifth colon (after score2)
size_t pos6 = round2Result.find(':', pos5 + 1); // Sixth colon (after TOTAL1)
size_t pos7 = round2Result.find(':', pos6 + 1); // Seventh colon (after TOTAL2)
// Check that we found all required colons (7 colons total in the message)
if (pos2 == string::npos || pos3 == string::npos || pos4 == string::npos ||
pos5 == string::npos || pos6 == string::npos || pos7 == string::npos) {
cerr << "Error: Invalid round 2 result format - missing colons" << endl;
cerr << "Received: [" << round2Result << "]" << endl;
close(clientSocket);
return 1;
}
int round2Num1, round2Num2, round2Score1, round2Score2, total1, total2, winStatus;
try {
round2Num1 = stoi(round2Result.substr(pos + 1, pos2 - pos - 1));
round2Num2 = stoi(round2Result.substr(pos2 + 1, pos3 - pos2 - 1));
round2Score1 = stoi(round2Result.substr(pos3 + 1, pos4 - pos3 - 1));
round2Score2 = stoi(round2Result.substr(pos4 + 1, pos5 - pos4 - 1));
total1 = stoi(round2Result.substr(pos5 + 1, pos6 - pos5 - 1));
total2 = stoi(round2Result.substr(pos6 + 1, pos7 - pos6 - 1));
winStatus = stoi(round2Result.substr(pos7 + 1)); // WIN_STATUS is everything after the 7th colon
cout << "Round 2: Player 1 selected " << round2Num1
<< ", Player 2 selected " << round2Num2 << endl;
cout << "Round 2: Player 1 score = " << round2Score1
<< ", Player 2 score = " << round2Score2 << endl;
cout << "Total: Player 1 = " << total1
<< ", Player 2 = " << total2 << endl;
// • The client, after receiving the information about who wins, should print a statement (you can select what statement to print, it must be different for the 3 cases, and please, no inappropriate language).
// Print win/lose/draw message based on win status
if (winStatus == 1) {
cout << "Congratulations! You won!" << endl;
} else if (winStatus == -1) {
cout << "Sorry, you lost. Better luck next time!" << endl;
} else {
cout << "It's a draw! Well played!" << endl;
}
} catch (const exception& e) {
cerr << "Error parsing round 2 results: " << e.what() << endl;
cerr << "Received: " << round2Result << endl;
close(clientSocket);
return 1;
}
// • After that, the client should disconnect from the server, and quit
close(clientSocket);
return 0;
}