-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
170 lines (160 loc) · 3.65 KB
/
Copy pathserver.cpp
File metadata and controls
170 lines (160 loc) · 3.65 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
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include "piece.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main(int argc, char* argv[])
{
struct sockaddr_in local;
int s;
int s1;
int rc;
if(argc != 2)
{
printf("usage: process <port>\n");
return 1;
}
local.sin_family = AF_INET;
local.sin_port = htons( atoi(argv[1]) );
local.sin_addr.s_addr = htonl( INADDR_ANY );
s = socket( AF_INET, SOCK_STREAM, 0 );
if ( s < 0 )
{
perror( "socket call failed" );
return 1;
}
rc = bind( s, ( struct sockaddr * )&local, sizeof( local ) );
if ( rc < 0 )
{
perror( "bind call failure" );
return 1;
}
rc = listen( s, 5 );
if ( rc )
{
perror( "listen call failed" );
return 1;
}
// set board
Board board;
#ifdef CUR_MODE
curSetBoard(&board);
#else
//
int board_size = 10;
int goal_size = 5;
printf("set (board size, goal size):");
scanf("%d %d", &board_size, &goal_size);
setbuf(stdin, NULL); /*À¶¶õ?ÑÕ¶è*/
board.board_size = board_size;
board.goal_size = goal_size;
#endif
s1 = accept( s, NULL, NULL );
if ( s1 < 0 )
{
perror( "accept call failed" );
return 1;
}
STATUS term = op_term_now;
//int x = -1;
//int y = -1;
CCP ccp;
memset(&ccp, 0, sizeof(ccp));
rc = send( s1, &board, sizeof(Board), 0 );
if ( rc <= 0 )
{
perror( "send call failed" );
goto END;
}
Init(&board);
while(1)
{
while(1)
{
print(op_term_now);
rc = recv( s1, &ccp, sizeof(ccp), 0 );
if ( rc <= 0 )
{
//perror( "recv call failed" );
goto END;
}
// set piece
term = op_term(ccp.x, ccp.y);
/* draw piece */
print(term);
if(my_term_now == term)
{
//x = ccp.x;
//y = ccp.y;
break;
}
else if(op_term_now == term)
{
printf("error!\n");
continue;
}
else if(i_win_now == term || op_win_now == term)
{
goto END;
}
else
{
printf("error!\n");
goto END;
}
}
while(1)
{
print(my_term_now);
#ifdef CUR_MODE
curSetCCP(&ccp);
#else
printf("Please enter your x,y:");
setbuf(stdin, NULL); /*À¶¶õ?ÑÕ¶è*/
scanf("%d %d", &ccp.x, &ccp.y);
#endif
// set piece
term = my_term(ccp.x, ccp.y);
print(term);
/* draw piece */
if(my_term_now == term)
{
continue;
}
else if(op_term_now == term)
{
/* send piece */
rc = send( s1, &ccp, sizeof(ccp), 0 );
if ( rc <= 0 )
{
perror( "send call failed" );
goto END;
}
break;
}
else if(i_win_now == term)
{
rc = send( s1, &ccp, sizeof(ccp), 0 );
if ( rc <= 0 )
{
perror( "send call failed" );
goto END;
}
goto END;
}
else if(op_win_now == term)
{
goto END;
}
}
}
END:
close(s1);
return 0;
}