/
server.c
215 lines (152 loc) · 3.97 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int newsockfd;
int sockfd;
char matrix[3][3]; /* the tic tac toe matrix */
int counter=0;
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_client_move(void);
void disp_matrix(void);
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr =inet_addr("127.0.0.1"); //INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
puts("This is the game of Tic Tac Toe.\n");
puts("waiting connection to established....\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
char done;
done = ' ';
init_matrix();
do {
disp_matrix();
puts("waiting for client move\n");
get_client_move();
disp_matrix();
done = check(); /* see if winner */
if(done!= ' ') break; /* winner!*/
get_player_move();
disp_matrix();
done = check(); /* see if winner */
} while(done== ' ');
if(done=='X') printf("Player X won!\n");
else printf("Player O won!!!!\n");
disp_matrix(); /* show final positions */
close(newsockfd);
close(sockfd);
return 0;
}
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
void get_player_move(void)
{
int x, y,i,j,n;
printf("Enter X,Y coordinates for Player O: ");
scanf("%d%*c%d", &x, &y);
int temp1=htonl(x);
int temp2=htonl(y);
n=write(newsockfd,&temp1,sizeof(temp1));
if(n<0)
error("write failed");
n=write(newsockfd,&temp2,sizeof(temp2));
if(n<0)
error("write failed");
if(n=0)
error("client closed");
x--; y--;
if(matrix[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
else matrix[x][y] = 'O';
counter++;
}
/* Display the matrix on the screen. */
void disp_matrix(void)
{
int t;
for(t=0; t<3; t++) {
printf(" %c | %c | %c ",matrix[t][0],
matrix[t][1], matrix [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}
/* See if there is a winner. */
char check(void)
{
int i,j;
for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
return matrix[0][2];
if(counter==9)
{
printf("draw\n");
exit(0);
}
return ' ';
}
void get_client_move(void)
{
int x, y,i,j;
int n;
n=read(newsockfd,&i,sizeof(i));
n=read(newsockfd,&j,sizeof(j));
if(n<0)
error("error on reading");
if(n=0)
error("client closed");
x=ntohl(i);
y=ntohl(j);
x--; y--;
if(matrix[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_client_move();
}
else matrix[x][y] = 'X';
counter++;
}