質問編集履歴
3
コードの変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
### 前提・実現したいこと
|
2
2
|
|
3
3
|
Eclipseを使い,Javaでソケット通信を用いたプログラムを作りました。
|
4
4
|
|
@@ -8,6 +8,10 @@
|
|
8
8
|
|
9
9
|
コードは上がサーバ側のもので,下がクライアント側です。そして両方ともObjectInputStreamの宣言のところで止まります。ただ,プログラムは終了せずにずっと実行中のままです。
|
10
10
|
|
11
|
+
|
12
|
+
|
13
|
+
なぜ,ObjectInputStreamの宣言のところで止まるのか知りたいです。
|
14
|
+
|
11
15
|
### 発生している問題・エラーメッセージ
|
12
16
|
|
13
17
|
|
@@ -22,155 +26,245 @@
|
|
22
26
|
|
23
27
|
```Java
|
24
28
|
|
25
|
-
p
|
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
|
-
System.out.println("o
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
S
|
74
|
-
|
75
|
-
list
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
29
|
+
package test;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
import java.io.BufferedReader;
|
34
|
+
|
35
|
+
import java.io.IOException;
|
36
|
+
|
37
|
+
import java.io.InputStreamReader;
|
38
|
+
|
39
|
+
import java.io.ObjectInputStream;
|
40
|
+
|
41
|
+
import java.io.ObjectOutputStream;
|
42
|
+
|
43
|
+
import java.io.PrintWriter;
|
44
|
+
|
45
|
+
import java.net.ServerSocket;
|
46
|
+
|
47
|
+
import java.net.Socket;
|
48
|
+
|
49
|
+
import java.util.ArrayList;
|
50
|
+
|
51
|
+
import java.util.List;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public class SV {
|
56
|
+
|
57
|
+
int port = 9999;
|
58
|
+
|
59
|
+
public SV() {
|
60
|
+
|
61
|
+
System.out.println("hello");
|
62
|
+
|
63
|
+
try {
|
64
|
+
|
65
|
+
int i = 0;
|
66
|
+
|
67
|
+
ServerSocket ssock = new ServerSocket(port);
|
68
|
+
|
69
|
+
Socket sock = ssock.accept();
|
70
|
+
|
71
|
+
System.out.println("connect");
|
72
|
+
|
73
|
+
String str;
|
74
|
+
|
75
|
+
List<String> list1 = new ArrayList<String>();
|
76
|
+
|
77
|
+
List<String> list2 = new ArrayList<String>();
|
78
|
+
|
79
|
+
list2.add("123");
|
80
|
+
|
81
|
+
list2.add("456");
|
82
|
+
|
83
|
+
list2.add("789");
|
84
|
+
|
85
|
+
System.out.println(list2);
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
|
90
|
+
|
91
|
+
//BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
92
|
+
|
93
|
+
System.out.println("ok1");
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
|
98
|
+
|
99
|
+
//PrintWriter bw = new PrintWriter(sock.getOutputStream(),true);
|
100
|
+
|
101
|
+
System.out.println("ok2");
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
while(i<3) {
|
106
|
+
|
107
|
+
System.out.println(i);
|
108
|
+
|
109
|
+
list1 = (List<String>) ois.readObject();
|
110
|
+
|
111
|
+
//str = br.readLine();
|
112
|
+
|
113
|
+
System.out.println(list1);
|
114
|
+
|
115
|
+
//System.out.println(str);
|
116
|
+
|
117
|
+
oos.writeObject(list2.get(i));
|
118
|
+
|
119
|
+
//bw.write(list2.get(i));
|
120
|
+
|
121
|
+
i++;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
ssock.close();
|
128
|
+
|
129
|
+
} catch (IOException | ClassNotFoundException e) {
|
130
|
+
|
131
|
+
e.printStackTrace();
|
88
132
|
|
89
133
|
}
|
90
134
|
|
91
|
-
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
92
|
-
|
139
|
+
public static void main(String args[]) {
|
140
|
+
|
141
|
+
System.out.println("server");
|
142
|
+
|
143
|
+
new SV();
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
```Java
|
154
|
+
|
93
|
-
|
155
|
+
package test;
|
156
|
+
|
157
|
+
|
158
|
+
|
94
|
-
|
159
|
+
import java.io.BufferedReader;
|
160
|
+
|
161
|
+
import java.io.IOException;
|
162
|
+
|
163
|
+
import java.io.InputStreamReader;
|
164
|
+
|
165
|
+
import java.io.ObjectInputStream;
|
166
|
+
|
167
|
+
import java.io.ObjectOutputStream;
|
168
|
+
|
169
|
+
import java.io.PrintWriter;
|
170
|
+
|
171
|
+
import java.net.Socket;
|
172
|
+
|
173
|
+
import java.util.ArrayList;
|
174
|
+
|
175
|
+
import java.util.List;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
public class CL {
|
180
|
+
|
181
|
+
int port = 9999;
|
182
|
+
|
183
|
+
public CL() {
|
184
|
+
|
185
|
+
System.out.println("hello");
|
186
|
+
|
187
|
+
try {
|
188
|
+
|
189
|
+
int i = 0;
|
190
|
+
|
191
|
+
Socket sock = new Socket("localhost",port);
|
192
|
+
|
193
|
+
String str;
|
194
|
+
|
195
|
+
List<String> list1 = new ArrayList<String>();
|
196
|
+
|
197
|
+
List<String> list2 = new ArrayList<String>();
|
198
|
+
|
199
|
+
list2.add("ABC");
|
200
|
+
|
201
|
+
list2.add("DEF");
|
202
|
+
|
203
|
+
list2.add("GHI");
|
204
|
+
|
205
|
+
System.out.println(list2);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
|
210
|
+
|
211
|
+
//BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
212
|
+
|
213
|
+
System.out.println("ok1");
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
|
218
|
+
|
219
|
+
//PrintWriter bw = new PrintWriter(sock.getOutputStream(),true);
|
220
|
+
|
221
|
+
System.out.println("ok2");
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
while(i<3) {
|
226
|
+
|
227
|
+
Thread.sleep(1000);
|
228
|
+
|
229
|
+
System.out.println(i);
|
230
|
+
|
231
|
+
oos.writeObject(list2.get(i));
|
232
|
+
|
233
|
+
//bw.println(list2.get(i));
|
234
|
+
|
235
|
+
list1 = (List<String>) ois.readObject();
|
236
|
+
|
237
|
+
//str = br.readLine();
|
238
|
+
|
239
|
+
//System.out.println(str);
|
240
|
+
|
241
|
+
System.out.println(list1);
|
242
|
+
|
243
|
+
i++;
|
244
|
+
|
245
|
+
}
|
246
|
+
|
95
|
-
} catch (IOException | ClassNotFoundException e) {
|
247
|
+
} catch (IOException | InterruptedException | ClassNotFoundException e) {
|
96
248
|
|
97
249
|
e.printStackTrace();
|
98
250
|
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
public static void main(String args[]) {
|
258
|
+
|
259
|
+
System.out.println("client");
|
260
|
+
|
261
|
+
new CL();
|
262
|
+
|
99
263
|
}
|
100
264
|
|
101
265
|
}
|
102
266
|
|
103
|
-
|
267
|
+
|
104
|
-
|
105
|
-
public CL() {
|
106
|
-
|
107
|
-
System.out.println("hello");
|
108
|
-
|
109
|
-
try {
|
110
|
-
|
111
|
-
int i = 0;
|
112
|
-
|
113
|
-
Socket sock = new Socket("localhost",port);
|
114
|
-
|
115
|
-
String str;
|
116
|
-
|
117
|
-
List<String> list1 = new ArrayList<String>();
|
118
|
-
|
119
|
-
List<String> list2 = new ArrayList<String>();
|
120
|
-
|
121
|
-
list2.add("ABC");
|
122
|
-
|
123
|
-
list2.add("DEF");
|
124
|
-
|
125
|
-
list2.add("GHI");
|
126
|
-
|
127
|
-
System.out.println(list2);
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
|
132
|
-
|
133
|
-
//BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
134
|
-
|
135
|
-
System.out.println("ok1");
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
|
140
|
-
|
141
|
-
//PrintWriter bw = new PrintWriter(sock.getOutputStream());
|
142
|
-
|
143
|
-
System.out.println("ok2");
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
while(i<3) {
|
148
|
-
|
149
|
-
System.out.println(i);
|
150
|
-
|
151
|
-
oos.writeObject(list2.get(i));
|
152
|
-
|
153
|
-
//bw.write(list2.get(i));
|
154
|
-
|
155
|
-
list1 = (List<String>) ois.readObject();
|
156
|
-
|
157
|
-
//str = br.readLine();
|
158
|
-
|
159
|
-
//System.out.println(str);
|
160
|
-
|
161
|
-
System.out.println(list1);
|
162
|
-
|
163
|
-
i++;
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
} catch (IOException | ClassNotFoundException e) {
|
168
|
-
|
169
|
-
e.printStackTrace();
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
}
|
174
268
|
|
175
269
|
```
|
176
270
|
|
@@ -178,6 +272,8 @@
|
|
178
272
|
|
179
273
|
ObjectInputStreamとObjectOutputStreamを消して,コメントアウトしてあるBufferedReaderとPrintWriterで同じようなことをしようとした結果,今度はstr = br.readLine()のところで止まってしまいました。
|
180
274
|
|
275
|
+
※追記:bw.writeをbw.printlnに変更したら正常に動きました。
|
276
|
+
|
181
277
|
|
182
278
|
|
183
279
|
|
2
クライアント側のコードを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
コードはサーバ側のもので,クライアント側
|
9
|
+
コードは上がサーバ側のもので,下がクライアント側です。そして両方ともObjectInputStreamの宣言のところで止まります。ただ,プログラムは終了せずにずっと実行中のままです。
|
10
10
|
|
11
11
|
### 発生している問題・エラーメッセージ
|
12
12
|
|
@@ -100,6 +100,78 @@
|
|
100
100
|
|
101
101
|
}
|
102
102
|
|
103
|
+
---------------------------------------------------------------------------------
|
104
|
+
|
105
|
+
public CL() {
|
106
|
+
|
107
|
+
System.out.println("hello");
|
108
|
+
|
109
|
+
try {
|
110
|
+
|
111
|
+
int i = 0;
|
112
|
+
|
113
|
+
Socket sock = new Socket("localhost",port);
|
114
|
+
|
115
|
+
String str;
|
116
|
+
|
117
|
+
List<String> list1 = new ArrayList<String>();
|
118
|
+
|
119
|
+
List<String> list2 = new ArrayList<String>();
|
120
|
+
|
121
|
+
list2.add("ABC");
|
122
|
+
|
123
|
+
list2.add("DEF");
|
124
|
+
|
125
|
+
list2.add("GHI");
|
126
|
+
|
127
|
+
System.out.println(list2);
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
|
132
|
+
|
133
|
+
//BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
134
|
+
|
135
|
+
System.out.println("ok1");
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
|
140
|
+
|
141
|
+
//PrintWriter bw = new PrintWriter(sock.getOutputStream());
|
142
|
+
|
143
|
+
System.out.println("ok2");
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
while(i<3) {
|
148
|
+
|
149
|
+
System.out.println(i);
|
150
|
+
|
151
|
+
oos.writeObject(list2.get(i));
|
152
|
+
|
153
|
+
//bw.write(list2.get(i));
|
154
|
+
|
155
|
+
list1 = (List<String>) ois.readObject();
|
156
|
+
|
157
|
+
//str = br.readLine();
|
158
|
+
|
159
|
+
//System.out.println(str);
|
160
|
+
|
161
|
+
System.out.println(list1);
|
162
|
+
|
163
|
+
i++;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
} catch (IOException | ClassNotFoundException e) {
|
168
|
+
|
169
|
+
e.printStackTrace();
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
103
175
|
```
|
104
176
|
|
105
177
|
### 試したこと
|
1
初心者マーク
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
### 前提・実現したいこと
|
1
|
+
---### 前提・実現したいこと
|
2
2
|
|
3
3
|
Eclipseを使い,Javaでソケット通信を用いたプログラムを作りました。
|
4
4
|
|