-
Notifications
You must be signed in to change notification settings - Fork 0
/
T3Server.c
315 lines (297 loc) · 12.9 KB
/
T3Server.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
#include "csapp.h"
#include <pthread.h>
#include <unistd.h>
#define DEBUG
/*
TODO: (Icing on top)
Block input (write to client initially, read, strcmp)
Add play again option
Fix Tie game final board state (2nd player doesn't receive last move)
Tell players which symbol they are
*/
int open_clientfd(char *hostname, int port);
int threadCount = 0; //Per process thread count
char board[3][3] = { // The board
{'1', '2', '3'}, // Initial values are reference numbers
{'4', '5', '6'}, // used to select a vacant square for
{'7', '8', '9'} // a turn.
};
int turn = 0; // Global Turn Counter
int currentPlayer = -1;
int hodor = 0; // Signal variable if a player has won
void* handleConnection(void* args);
typedef struct arguments_t { //Used to create a thread to handle each client connection
int connfd;
struct sockaddr_in clientaddr;
int playerID; //Pass the player's ID into the thread
} arguments;
int main(int argc, char **argv)
{
int listenfd; // The proxy's listening descriptor
int port; // The port the proxy is listening on
int clientlen; // Size in bytes of the client socket address
struct sockaddr_in clientaddr;
int connfd; // The file descriptor for each client
arguments* args; // Used to pass arguments to the thread
pthread_t thread;
int pid; //Holds child process id when forking
int error = 0;
// Check arguments
if (argc != 2) {
fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
exit(0);
}
signal(SIGPIPE, SIG_IGN);
// Create a listening descriptor
port = atoi(argv[1]);
listenfd = Open_listenfd(port);
while (1) {
error = 0;
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
args = malloc(sizeof(arguments));
args->connfd = connfd;
args->clientaddr = clientaddr;
args->playerID = threadCount;
if (pthread_create(&thread, NULL, &handleConnection, args)) {
perror("Thread creation failed!");
continue;
}
if (threadCount == 1) { // Once there are two clients,
pid = fork(); // Create a new process to handle the next two
if (pid != 0) {
printf("In Parent process, created child: %d\n", pid);
while(1){
sleep(1000000000000000000); // Sleep while two child threads handle each connection
}
} else {
threadCount = 0; // Reset thread count in new process
//continue while loop
}
}
}
// Control never reaches here
exit(0);
}
void* handleConnection(void* argsVoid) {
threadCount++;
struct sockaddr_in clientaddr; // Client address structure
int connfd; // socket desciptor for talking with client
int n; // General index and counting variables
int playerID; // Player ID to identify whose turn is first
char buf[256];
int line;
int selection;
int error = 0; // Used to detect error in reading requests
int retval=-1;
arguments* args = (arguments*)argsVoid; // Unpack all arguments passed into thread
clientaddr = args->clientaddr;
connfd = args->connfd;
playerID = args->playerID;
printf("Thread Number/Player ID: %d\n", threadCount); // Confirm connection on server side
while(threadCount != 2){ //Wait for next player to connect
sleep(1);
}
if(playerID == 0){ // First player
retval = write(args->connfd, "You are connected to a game! You are Player 1 and play X on the board.", 256); //Tell Client
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}
if(playerID == 1){ // Second Player
retval = write(args->connfd, "You are connected to a game! You are Player 2 and play O on the board. Please wait for Player 1 to make a move.", 256); // Tell Client
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}
while (1) {
if (turn > 8 && hodor == 0){
retval = write(args->connfd, "Tie game!!!\n", strlen("Tie game!!!\n"));
if (retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
pthread_exit(NULL);
}
if (turn % 2 == 0 && playerID == 0){ // Player 1's turn
if(hodor == 1){ // Check if player has lost
retval = write(args->connfd, "You lose!!!\n", strlen("You lose!!!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
pthread_exit(NULL);
}
//Send the board to the player
bzero(buf, 256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
if ((n = read(args->connfd, &selection, MAXLINE)) <= 0){
error = 1; //Used to fix a bug
printf("process_request: client issued a bad request (1).\n");
close(args->connfd);
break;
}
// Parse the row and column of symbol from the user's selection
int pos = selection;
int row = --pos/3;
int column = pos%3;
if(board[row][column] == 'X' || board[row][column] == 'O'){ // Check if move is already in 2d array
retval = write(args->connfd, "Error, move already made!\n", strlen("Error, move already made!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
continue;
}
// Ensure pos is within range
if (pos <0 || pos > 8){
printf("\nPlease enter a proper value.\n");
continue;
}
// Insert the symbol
board[row][column] = 'X';
if (error) {
close(connfd);
pthread_exit(NULL);
}
//check for winning line, diagonal
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0])){
hodor = 1; //Set winner signal value to 1
}
for (line = 0; line <= 2; line ++){
if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) || (board[0][line] == board[1][line] && board[0][line] == board[2][line])){
hodor = 1; //Set winner signal value to 1
}
}
//check for winning line, rows and columns
if(hodor == 0){ // If there isn't a winner yet, print board
bzero(buf,256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n\nWaiting for Player 2 to make a move\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}else{ // If there is a winner, print final board state
bzero(buf,256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}
if (error) {
close(connfd);
pthread_exit(NULL);
}
if (hodor==1){ // If you have won, send to client
retval = write(args->connfd, "You win!!!\n", strlen("You win!!!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
turn++;
pthread_exit(NULL);
}
turn++; //Increment turn
}else if (turn % 2 == 1 && playerID == 1){ //Same code as above, for other player (swap X for O)
if(hodor == 1){
retval = write(args->connfd, "You lose!!!\n", strlen("You lose!!!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
pthread_exit(NULL);
}
bzero(buf, 256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
bzero(buf, 256);
if ((n = read(args->connfd, &selection, MAXLINE)) <= 0){
error = 1; //Used to fix a bug
printf("process_request: client issued a bad request (1).\n");
close(args->connfd);
break;
}
int pos = selection;
int row = --pos/3;
int column = pos%3;
if(board[row][column] == 'X' || board[row][column] == 'O'){
retval = write(args->connfd, "Error, move already made!\n", strlen("Error, move already made!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
continue;
}
if (pos <0 || pos > 8){
printf("\nPlease enter a proper value.\n");
continue;
}
board[row][column] = 'O';
if (error) {
close(connfd);
pthread_exit(NULL);
}
//check for winning line, diagonal
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0])){
hodor = 1;
}
for (line = 0; line <= 2; line ++){
if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) || (board[0][line] == board[1][line] && board[0][line] == board[2][line])){
hodor = 1;
}
}
//check for winning line, rows and columns
if(hodor == 0){
bzero(buf,256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n\nWaiting for Player 1 to make a move\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}
else{
bzero(buf,256);
snprintf(buf, 256, "\n\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n ---+---+---\n %c | %c | %c\n", board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
retval = write(args->connfd, buf, 256);
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
}
if (error) {
close(connfd);
pthread_exit(NULL);
}
if (hodor==1){
retval = write(args->connfd, "You win!!!\n", strlen("You win!!!\n"));
if(retval < 0){
printf("Error writing!\n");
pthread_exit(NULL);
}
turn++;
pthread_exit(NULL);
}
turn++; //Increment turn
}else{
sleep(1);
continue;
}
}
close(args->connfd);
return NULL;
}