回答編集履歴
3
バグっぽい"+1"を削除
test
CHANGED
@@ -158,7 +158,7 @@
|
|
158
158
|
|
159
159
|
}
|
160
160
|
|
161
|
-
return new Point((int)(Math.random()*8)
|
161
|
+
return new Point((int)(Math.random()*8), (int)(Math.random()*8));
|
162
162
|
|
163
163
|
}
|
164
164
|
|
@@ -228,7 +228,7 @@
|
|
228
228
|
|
229
229
|
}
|
230
230
|
|
231
|
-
return new Point((int)(Math.random()*8)
|
231
|
+
return new Point((int)(Math.random()*8), (int)(Math.random()*8));
|
232
232
|
|
233
233
|
}
|
234
234
|
|
2
run の中をスッキリ
test
CHANGED
@@ -4,6 +4,10 @@
|
|
4
4
|
|
5
5
|
```java
|
6
6
|
|
7
|
+
package teratail.q246663;
|
8
|
+
|
9
|
+
|
10
|
+
|
7
11
|
import java.io.*;
|
8
12
|
|
9
13
|
import java.net.*;
|
@@ -12,156 +16,270 @@
|
|
12
16
|
|
13
17
|
public class ReversiCommunicationTest {
|
14
18
|
|
19
|
+
public static final int PORT = 12345;
|
20
|
+
|
15
21
|
public static void main(String[] args) {
|
16
22
|
|
17
|
-
new Thread(new Server()).start();
|
18
|
-
|
19
|
-
new Client().run();
|
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
|
-
|
23
|
+
new Thread(new Server(PORT)).start();
|
24
|
+
|
25
|
+
new Client("127.0.0.1", PORT).run();
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
class Point {
|
32
|
+
|
33
|
+
final int col, row;
|
34
|
+
|
35
|
+
Point(int col, int row) { this.col = col; this.row = row; }
|
36
|
+
|
37
|
+
@Override
|
38
|
+
|
39
|
+
public String toString() {
|
40
|
+
|
41
|
+
return super.toString()+"[col="+col+",row="+row+"]";
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
class Communicator implements AutoCloseable {
|
48
|
+
|
49
|
+
private BufferedReader reader;
|
50
|
+
|
51
|
+
private PrintWriter out;
|
52
|
+
|
53
|
+
Communicator(Socket s) throws IOException {
|
54
|
+
|
55
|
+
try {
|
56
|
+
|
57
|
+
reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
58
|
+
|
59
|
+
out = new PrintWriter(s.getOutputStream(), true);
|
60
|
+
|
61
|
+
} catch(IOException e) {
|
62
|
+
|
63
|
+
close();
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
void send(Point point) throws IOException {
|
70
|
+
|
71
|
+
out.println(point.col+","+point.row);
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
Point receive() throws IOException {
|
76
|
+
|
77
|
+
String line = reader.readLine();
|
78
|
+
|
79
|
+
if(line == null) return null;
|
80
|
+
|
81
|
+
String[] tokens = line.split(",");
|
82
|
+
|
83
|
+
return new Point(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
@Override
|
88
|
+
|
89
|
+
public void close() throws IOException {
|
90
|
+
|
91
|
+
if(out == null) { out.close(); out = null; }
|
92
|
+
|
93
|
+
if(reader == null) { reader.close(); reader = null; }
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
class Server implements Runnable {
|
100
|
+
|
101
|
+
private int port;
|
102
|
+
|
103
|
+
Server(int port) {
|
104
|
+
|
105
|
+
this.port = port;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
@Override
|
110
|
+
|
111
|
+
public void run() {
|
112
|
+
|
113
|
+
try (ServerSocket ss = new ServerSocket(port);
|
114
|
+
|
115
|
+
Communicator comm = new Communicator(ss.accept());) {
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
Point point = null;
|
120
|
+
|
121
|
+
while(true) {
|
122
|
+
|
123
|
+
System.out.println("Server: 考え中");
|
124
|
+
|
125
|
+
point = thinking();
|
126
|
+
|
127
|
+
System.out.println("Server: 送信="+point);
|
128
|
+
|
129
|
+
comm.send(point);
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
System.out.println("Server: 受信待ち");
|
134
|
+
|
135
|
+
point = comm.receive();
|
136
|
+
|
137
|
+
System.out.println("Server: 受信="+point);
|
90
138
|
|
91
139
|
}
|
92
140
|
|
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
|
-
e.print
|
141
|
+
} catch(IOException e) {
|
142
|
+
|
143
|
+
e.printStackTrace();
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
private Point thinking() {
|
150
|
+
|
151
|
+
try {
|
152
|
+
|
153
|
+
Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
|
154
|
+
|
155
|
+
} catch(InterruptedException e) {
|
156
|
+
|
157
|
+
e.printStackTrace();
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
class Client implements Runnable {
|
168
|
+
|
169
|
+
private String host;
|
170
|
+
|
171
|
+
private int port;
|
172
|
+
|
173
|
+
Client(String host, int port) {
|
174
|
+
|
175
|
+
this.host = host;
|
176
|
+
|
177
|
+
this.port = port;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
@Override
|
182
|
+
|
183
|
+
public void run() {
|
184
|
+
|
185
|
+
try (Communicator comm = new Communicator(new Socket(host, port));) {
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
Point point = null;
|
190
|
+
|
191
|
+
while(true) {
|
192
|
+
|
193
|
+
System.out.println("Client: 受信待ち");
|
194
|
+
|
195
|
+
point = comm.receive();
|
196
|
+
|
197
|
+
System.out.println("Client: 受信="+point);
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
System.out.println("Client: 考え中");
|
202
|
+
|
203
|
+
point = thinking();
|
204
|
+
|
205
|
+
System.out.println("Client: 送信="+point);
|
206
|
+
|
207
|
+
comm.send(point);
|
158
208
|
|
159
209
|
}
|
160
210
|
|
211
|
+
} catch(IOException e) {
|
212
|
+
|
213
|
+
e.printStackTrace();
|
214
|
+
|
161
|
-
}
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
private Point thinking() {
|
220
|
+
|
221
|
+
try {
|
222
|
+
|
223
|
+
Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
|
224
|
+
|
225
|
+
} catch(InterruptedException e) {
|
226
|
+
|
227
|
+
e.printStackTrace();
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
|
162
232
|
|
163
233
|
}
|
164
234
|
|
165
235
|
}
|
166
236
|
|
167
237
|
```
|
238
|
+
|
239
|
+
```plain text
|
240
|
+
|
241
|
+
Client: 受信待ち
|
242
|
+
|
243
|
+
Server: 考え中
|
244
|
+
|
245
|
+
Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@334495b1[col=3,row=2]
|
246
|
+
|
247
|
+
Server: 受信待ち
|
248
|
+
|
249
|
+
Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@7852e922[col=3,row=2]
|
250
|
+
|
251
|
+
Client: 考え中
|
252
|
+
|
253
|
+
Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@4e25154f[col=5,row=8]
|
254
|
+
|
255
|
+
Client: 受信待ち
|
256
|
+
|
257
|
+
Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@76460dbd[col=5,row=8]
|
258
|
+
|
259
|
+
Server: 考え中
|
260
|
+
|
261
|
+
Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@28d2af42[col=1,row=7]
|
262
|
+
|
263
|
+
Server: 受信待ち
|
264
|
+
|
265
|
+
Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@70dea4e[col=1,row=7]
|
266
|
+
|
267
|
+
Client: 考え中
|
268
|
+
|
269
|
+
Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@5c647e05[col=4,row=3]
|
270
|
+
|
271
|
+
Client: 受信待ち
|
272
|
+
|
273
|
+
Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@47fadae9[col=4,row=3]
|
274
|
+
|
275
|
+
Server: 考え中
|
276
|
+
|
277
|
+
Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@508426ba[col=3,row=8]
|
278
|
+
|
279
|
+
Server: 受信待ち
|
280
|
+
|
281
|
+
:
|
282
|
+
|
283
|
+
:
|
284
|
+
|
285
|
+
```
|
1
try-catchまとめと追記
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
このようなコードで試せます.
|
2
|
+
|
3
|
+
(無限ループなので, テキトウに止めてください.)
|
2
4
|
|
3
5
|
```java
|
4
6
|
|
@@ -24,61 +26,59 @@
|
|
24
26
|
|
25
27
|
public void run() {
|
26
28
|
|
27
|
-
try (ServerSocket ss = new ServerSocket(12345);
|
29
|
+
try (ServerSocket ss = new ServerSocket(12345);
|
28
30
|
|
29
|
-
Socket s = ss.accept();
|
31
|
+
Socket s = ss.accept();
|
30
32
|
|
31
|
-
|
33
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
32
34
|
|
33
|
-
|
35
|
+
PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
|
34
36
|
|
35
37
|
|
36
38
|
|
37
|
-
|
39
|
+
while(true) {
|
38
40
|
|
39
|
-
|
41
|
+
{
|
40
42
|
|
41
|
-
|
43
|
+
try {
|
42
44
|
|
43
|
-
|
45
|
+
System.out.println("Server: 考え中");
|
44
46
|
|
45
|
-
|
47
|
+
Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
|
46
48
|
|
47
|
-
|
49
|
+
} catch(InterruptedException e) {
|
48
50
|
|
49
|
-
|
51
|
+
e.printStackTrace();
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
int col = (int)(Math.random()*8)+1;
|
54
|
-
|
55
|
-
int row = (int)(Math.random()*8)+1;
|
56
|
-
|
57
|
-
out.println(col+","+row);
|
58
|
-
|
59
|
-
System.out.println("Server: 送信="+col+","+row);
|
60
52
|
|
61
53
|
}
|
62
54
|
|
55
|
+
int col = (int)(Math.random()*8)+1;
|
56
|
+
|
57
|
+
int row = (int)(Math.random()*8)+1;
|
58
|
+
|
59
|
+
out.println(col+","+row);
|
60
|
+
|
61
|
+
System.out.println("Server: 送信="+col+","+row);
|
62
|
+
|
63
|
+
}
|
63
64
|
|
64
65
|
|
65
|
-
{
|
66
66
|
|
67
|
-
|
67
|
+
{
|
68
68
|
|
69
|
-
|
69
|
+
System.out.println("Server: 受信待ち");
|
70
70
|
|
71
|
-
|
71
|
+
String line = reader.readLine();
|
72
72
|
|
73
|
-
|
73
|
+
if(line == null) break;
|
74
74
|
|
75
|
-
|
75
|
+
String[] tokens = line.split(",");
|
76
76
|
|
77
|
-
|
77
|
+
int col = Integer.parseInt(tokens[0]);
|
78
78
|
|
79
|
-
|
79
|
+
int row = Integer.parseInt(tokens[1]);
|
80
80
|
|
81
|
-
|
81
|
+
System.out.println("Server: 受信="+col+","+row);
|
82
82
|
|
83
83
|
}
|
84
84
|
|
@@ -98,57 +98,55 @@
|
|
98
98
|
|
99
99
|
public void run() {
|
100
100
|
|
101
|
-
try (Socket s = new Socket("127.0.0.1", 12345);
|
101
|
+
try (Socket s = new Socket("127.0.0.1", 12345);
|
102
102
|
|
103
|
-
|
103
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
104
104
|
|
105
|
-
|
105
|
+
PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
|
106
106
|
|
107
|
-
|
107
|
+
while(true) {
|
108
108
|
|
109
|
-
|
109
|
+
{
|
110
110
|
|
111
|
-
|
111
|
+
System.out.println("Client: 受信待ち");
|
112
112
|
|
113
|
-
|
113
|
+
String line = reader.readLine();
|
114
114
|
|
115
|
-
|
115
|
+
if(line == null) break;
|
116
116
|
|
117
|
-
|
117
|
+
String[] tokens = line.split(",");
|
118
118
|
|
119
|
-
|
119
|
+
int col = Integer.parseInt(tokens[0]);
|
120
120
|
|
121
|
-
|
121
|
+
int row = Integer.parseInt(tokens[1]);
|
122
122
|
|
123
|
-
|
123
|
+
System.out.println("Client: 受信="+col+","+row);
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
try {
|
132
|
+
|
133
|
+
System.out.println("Client: 考え中");
|
134
|
+
|
135
|
+
Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
|
136
|
+
|
137
|
+
} catch(InterruptedException e) {
|
138
|
+
|
139
|
+
e.printStackTrace();
|
124
140
|
|
125
141
|
}
|
126
142
|
|
143
|
+
int col = (int)(Math.random()*8)+1;
|
127
144
|
|
145
|
+
int row = (int)(Math.random()*8)+1;
|
128
146
|
|
129
|
-
|
147
|
+
out.println(col+","+row);
|
130
148
|
|
131
|
-
try {
|
132
|
-
|
133
|
-
System.out.println("Client: 考え中");
|
134
|
-
|
135
|
-
Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
|
136
|
-
|
137
|
-
} catch(InterruptedException e) {
|
138
|
-
|
139
|
-
e.printStackTrace();
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
int col = (int)(Math.random()*8)+1;
|
144
|
-
|
145
|
-
int row = (int)(Math.random()*8)+1;
|
146
|
-
|
147
|
-
out.println(col+","+row);
|
148
|
-
|
149
|
-
|
149
|
+
System.out.println("Client: 送信="+col+","+row);
|
150
|
-
|
151
|
-
}
|
152
150
|
|
153
151
|
}
|
154
152
|
|