teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

バグっぽい"+1"を削除

2020/03/14 08:16

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -78,7 +78,7 @@
78
78
  } catch(InterruptedException e) {
79
79
  e.printStackTrace();
80
80
  }
81
- return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
81
+ return new Point((int)(Math.random()*8), (int)(Math.random()*8));
82
82
  }
83
83
  }
84
84
  class Client implements Runnable {
@@ -113,7 +113,7 @@
113
113
  } catch(InterruptedException e) {
114
114
  e.printStackTrace();
115
115
  }
116
- return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
116
+ return new Point((int)(Math.random()*8), (int)(Math.random()*8));
117
117
  }
118
118
  }
119
119
  ```

2

run の中をスッキリ

2020/03/14 08:16

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -1,84 +1,143 @@
1
1
  このようなコードで試せます.
2
2
  (無限ループなので, テキトウに止めてください.)
3
3
  ```java
4
+ package teratail.q246663;
5
+
4
6
  import java.io.*;
5
7
  import java.net.*;
6
8
 
7
9
  public class ReversiCommunicationTest {
10
+ public static final int PORT = 12345;
8
11
  public static void main(String[] args) {
9
- new Thread(new Server()).start();
12
+ new Thread(new Server(PORT)).start();
10
- new Client().run();
13
+ new Client("127.0.0.1", PORT).run();
11
14
  }
15
+ }
16
+ class Point {
17
+ final int col, row;
18
+ Point(int col, int row) { this.col = col; this.row = row; }
19
+ @Override
20
+ public String toString() {
21
+ return super.toString()+"[col="+col+",row="+row+"]";
22
+ }
23
+ }
24
+ class Communicator implements AutoCloseable {
25
+ private BufferedReader reader;
26
+ private PrintWriter out;
27
+ Communicator(Socket s) throws IOException {
28
+ try {
29
+ reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
30
+ out = new PrintWriter(s.getOutputStream(), true);
31
+ } catch(IOException e) {
32
+ close();
33
+ }
34
+ }
35
+ void send(Point point) throws IOException {
36
+ out.println(point.col+","+point.row);
37
+ }
38
+ Point receive() throws IOException {
39
+ String line = reader.readLine();
40
+ if(line == null) return null;
41
+ String[] tokens = line.split(",");
42
+ return new Point(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
43
+ }
44
+ @Override
45
+ public void close() throws IOException {
46
+ if(out == null) { out.close(); out = null; }
47
+ if(reader == null) { reader.close(); reader = null; }
48
+ }
49
+ }
50
+ class Server implements Runnable {
51
+ private int port;
52
+ Server(int port) {
53
+ this.port = port;
54
+ }
55
+ @Override
56
+ public void run() {
57
+ try (ServerSocket ss = new ServerSocket(port);
58
+ Communicator comm = new Communicator(ss.accept());) {
12
59
 
13
- static class Server implements Runnable {
14
- public void run() {
60
+ Point point = null;
61
+ while(true) {
15
- try (ServerSocket ss = new ServerSocket(12345);
62
+ System.out.println("Server: 考え中");
16
- Socket s = ss.accept();
63
+ point = thinking();
17
- BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
18
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
64
+ System.out.println("Server: 送信="+point);
65
+ comm.send(point);
19
66
 
20
- while(true) {
21
- {
22
- try {
23
- System.out.println("Server: 考え中");
24
- Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
25
- } catch(InterruptedException e) {
26
- e.printStackTrace();
27
- }
28
- int col = (int)(Math.random()*8)+1;
29
- int row = (int)(Math.random()*8)+1;
30
- out.println(col+","+row);
31
- System.out.println("Server: 送信="+col+","+row);
32
- }
33
-
34
- {
35
- System.out.println("Server: 受信待ち");
67
+ System.out.println("Server: 受信待ち");
36
- String line = reader.readLine();
68
+ point = comm.receive();
37
- if(line == null) break;
38
- String[] tokens = line.split(",");
39
- int col = Integer.parseInt(tokens[0]);
40
- int row = Integer.parseInt(tokens[1]);
41
- System.out.println("Server: 受信="+col+","+row);
69
+ System.out.println("Server: 受信="+point);
42
- }
43
- }
44
- } catch(IOException e) {
45
- e.printStackTrace();
46
70
  }
71
+ } catch(IOException e) {
72
+ e.printStackTrace();
47
73
  }
48
74
  }
75
+ private Point thinking() {
76
+ try {
77
+ Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
78
+ } catch(InterruptedException e) {
79
+ e.printStackTrace();
80
+ }
81
+ return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
82
+ }
83
+ }
49
- static class Client implements Runnable {
84
+ class Client implements Runnable {
85
+ private String host;
86
+ private int port;
87
+ Client(String host, int port) {
88
+ this.host = host;
89
+ this.port = port;
90
+ }
91
+ @Override
50
- public void run() {
92
+ public void run() {
51
- try (Socket s = new Socket("127.0.0.1", 12345);
52
- BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
53
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
93
+ try (Communicator comm = new Communicator(new Socket(host, port));) {
54
- while(true) {
55
- {
56
- System.out.println("Client: 受信待ち");
57
- String line = reader.readLine();
58
- if(line == null) break;
59
- String[] tokens = line.split(",");
60
- int col = Integer.parseInt(tokens[0]);
61
- int row = Integer.parseInt(tokens[1]);
62
- System.out.println("Client: 受信="+col+","+row);
63
- }
64
94
 
65
- {
95
+ Point point = null;
66
- try {
96
+ while(true) {
97
+ System.out.println("Client: 受信待ち");
98
+ point = comm.receive();
99
+ System.out.println("Client: 受信="+point);
100
+
67
- System.out.println("Client: 考え中");
101
+ System.out.println("Client: 考え中");
68
- Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
69
- } catch(InterruptedException e) {
70
- e.printStackTrace();
102
+ point = thinking();
71
- }
72
- int col = (int)(Math.random()*8)+1;
73
- int row = (int)(Math.random()*8)+1;
74
- out.println(col+","+row);
75
- System.out.println("Client: 送信="+col+","+row);
103
+ System.out.println("Client: 送信="+point);
76
- }
77
- }
78
- } catch(IOException e) {
104
+ comm.send(point);
79
- e.printStackTrace();
80
105
  }
106
+ } catch(IOException e) {
107
+ e.printStackTrace();
81
108
  }
82
109
  }
110
+ private Point thinking() {
111
+ try {
112
+ Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
113
+ } catch(InterruptedException e) {
114
+ e.printStackTrace();
115
+ }
116
+ return new Point((int)(Math.random()*8)+1, (int)(Math.random()*8)+1);
117
+ }
83
118
  }
119
+ ```
120
+ ```plain text
121
+ Client: 受信待ち
122
+ Server: 考え中
123
+ Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@334495b1[col=3,row=2]
124
+ Server: 受信待ち
125
+ Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@7852e922[col=3,row=2]
126
+ Client: 考え中
127
+ Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@4e25154f[col=5,row=8]
128
+ Client: 受信待ち
129
+ Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@76460dbd[col=5,row=8]
130
+ Server: 考え中
131
+ Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@28d2af42[col=1,row=7]
132
+ Server: 受信待ち
133
+ Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@70dea4e[col=1,row=7]
134
+ Client: 考え中
135
+ Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@5c647e05[col=4,row=3]
136
+ Client: 受信待ち
137
+ Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@47fadae9[col=4,row=3]
138
+ Server: 考え中
139
+ Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@508426ba[col=3,row=8]
140
+ Server: 受信待ち
141
+ :
142
+ :
84
143
  ```

1

try-catchまとめと追記

2020/03/14 06:03

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -1,4 +1,5 @@
1
1
  このようなコードで試せます.
2
+ (無限ループなので, テキトウに止めてください.)
2
3
  ```java
3
4
  import java.io.*;
4
5
  import java.net.*;
@@ -11,34 +12,33 @@
11
12
 
12
13
  static class Server implements Runnable {
13
14
  public void run() {
14
- try (ServerSocket ss = new ServerSocket(12345);) {
15
+ try (ServerSocket ss = new ServerSocket(12345);
15
- Socket s = ss.accept();
16
+ Socket s = ss.accept();
16
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
17
+ BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
17
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
18
+ PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
18
19
 
19
- while(true) {
20
+ while(true) {
20
- {
21
+ {
21
- try {
22
+ try {
22
- System.out.println("Server: 考え中");
23
+ System.out.println("Server: 考え中");
23
- Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
24
+ Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
24
- } catch(InterruptedException e) {
25
+ } catch(InterruptedException e) {
25
- e.printStackTrace();
26
+ e.printStackTrace();
26
- }
27
- int col = (int)(Math.random()*8)+1;
28
- int row = (int)(Math.random()*8)+1;
29
- out.println(col+","+row);
30
- System.out.println("Server: 送信="+col+","+row);
31
27
  }
28
+ int col = (int)(Math.random()*8)+1;
29
+ int row = (int)(Math.random()*8)+1;
30
+ out.println(col+","+row);
31
+ System.out.println("Server: 送信="+col+","+row);
32
+ }
32
33
 
33
- {
34
+ {
34
- System.out.println("Server: 受信待ち");
35
+ System.out.println("Server: 受信待ち");
35
- String line = reader.readLine();
36
+ String line = reader.readLine();
36
- if(line == null) break;
37
+ if(line == null) break;
37
- String[] tokens = line.split(",");
38
+ String[] tokens = line.split(",");
38
- int col = Integer.parseInt(tokens[0]);
39
+ int col = Integer.parseInt(tokens[0]);
39
- int row = Integer.parseInt(tokens[1]);
40
+ int row = Integer.parseInt(tokens[1]);
40
- System.out.println("Server: 受信="+col+","+row);
41
+ System.out.println("Server: 受信="+col+","+row);
41
- }
42
42
  }
43
43
  }
44
44
  } catch(IOException e) {
@@ -48,32 +48,31 @@
48
48
  }
49
49
  static class Client implements Runnable {
50
50
  public void run() {
51
- try (Socket s = new Socket("127.0.0.1", 12345);) {
51
+ try (Socket s = new Socket("127.0.0.1", 12345);
52
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
52
+ BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
53
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
53
+ PrintWriter out = new PrintWriter(s.getOutputStream(), true);) {
54
- while(true) {
54
+ while(true) {
55
- {
55
+ {
56
- System.out.println("Client: 受信待ち");
56
+ System.out.println("Client: 受信待ち");
57
- String line = reader.readLine();
57
+ String line = reader.readLine();
58
- if(line == null) break;
58
+ if(line == null) break;
59
- String[] tokens = line.split(",");
59
+ String[] tokens = line.split(",");
60
- int col = Integer.parseInt(tokens[0]);
60
+ int col = Integer.parseInt(tokens[0]);
61
- int row = Integer.parseInt(tokens[1]);
61
+ int row = Integer.parseInt(tokens[1]);
62
- System.out.println("Client: 受信="+col+","+row);
62
+ System.out.println("Client: 受信="+col+","+row);
63
- }
63
+ }
64
64
 
65
- {
65
+ {
66
- try {
66
+ try {
67
- System.out.println("Client: 考え中");
67
+ System.out.println("Client: 考え中");
68
- Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
68
+ Thread.sleep((long)(Math.random()*3000)+2000); //[ms]
69
- } catch(InterruptedException e) {
69
+ } catch(InterruptedException e) {
70
- e.printStackTrace();
70
+ e.printStackTrace();
71
- }
72
- int col = (int)(Math.random()*8)+1;
73
- int row = (int)(Math.random()*8)+1;
74
- out.println(col+","+row);
75
- System.out.println("Client: 送信="+col+","+row);
76
71
  }
72
+ int col = (int)(Math.random()*8)+1;
73
+ int row = (int)(Math.random()*8)+1;
74
+ out.println(col+","+row);
75
+ System.out.println("Client: 送信="+col+","+row);
77
76
  }
78
77
  }
79
78
  } catch(IOException e) {