質問編集履歴
1
質問内容の変更、添削
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
チャットプログラムの
|
1
|
+
チャットプログラムの警告について
|
test
CHANGED
@@ -1,4 +1,236 @@
|
|
1
|
+
```java
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
import java.util.*;
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
public class chatsev
|
10
|
+
|
11
|
+
{
|
12
|
+
|
13
|
+
static final int Portnum=12345;
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
static ServerSocket svSocket;
|
18
|
+
|
19
|
+
static Vector connections;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
public static void sendAll(String s){
|
24
|
+
|
25
|
+
if(connections !=null){
|
26
|
+
|
27
|
+
for(Enumeration e =connections.elements();
|
28
|
+
|
29
|
+
e.hasMoreElements();){
|
30
|
+
|
31
|
+
try{
|
32
|
+
|
33
|
+
PrintWriter out = new PrintWriter(
|
34
|
+
|
35
|
+
((Socket)e.nextElement()).getOutputStream());
|
36
|
+
|
37
|
+
out.println(s);
|
38
|
+
|
39
|
+
out.flush();
|
40
|
+
|
41
|
+
}catch(IOException ex){}
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
System.out.println(s);
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public static void addConnection(Socket s){
|
56
|
+
|
57
|
+
if(connections == null){
|
58
|
+
|
59
|
+
connections = new Vector();
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
connections.addElement(s);
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
public static void deleteConnection(Socket s){
|
70
|
+
|
71
|
+
if(connections != null){
|
72
|
+
|
73
|
+
connections.removeElement(s);
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
public static void main(String[] arg){
|
82
|
+
|
83
|
+
try{
|
84
|
+
|
85
|
+
svSocket =new ServerSocket(Portnum);
|
86
|
+
|
87
|
+
}catch (IOException e){
|
88
|
+
|
89
|
+
System.err.println(e);
|
90
|
+
|
91
|
+
System.exit(1);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
while(true){
|
98
|
+
|
99
|
+
try{
|
100
|
+
|
101
|
+
Socket cs =svSocket.accept();
|
102
|
+
|
103
|
+
addConnection(cs);
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
Thread ct =new Thread(new clientProc(cs));
|
108
|
+
|
109
|
+
ct.start();
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
}catch (IOException e){
|
114
|
+
|
115
|
+
System.err.println(e);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
class clientProc implements Runnable{
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
Socket s;
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
BufferedReader in;
|
146
|
+
|
147
|
+
PrintWriter out;
|
148
|
+
|
149
|
+
String name =null;
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
public clientProc(Socket s) throws IOException{
|
154
|
+
|
155
|
+
this.s=s;
|
156
|
+
|
157
|
+
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
|
158
|
+
|
159
|
+
out = new PrintWriter(s.getOutputStream());
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
public void run(){
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
try{
|
174
|
+
|
175
|
+
while (name ==null){
|
176
|
+
|
177
|
+
name = in.readLine();
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
chatsev.sendAll("::"+name+"さんが入室しました");
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
String line =in.readLine();
|
190
|
+
|
191
|
+
while(!"logout".equals(line)){
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
chatsev.sendAll(name +">"+line);
|
196
|
+
|
197
|
+
line =in.readLine();
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
chatsev.sendAll("::"+name+"さんが退室しました");
|
204
|
+
|
205
|
+
chatsev.deleteConnection(s);
|
206
|
+
|
207
|
+
s.close();
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
}catch (IOException e){
|
212
|
+
|
213
|
+
try{
|
214
|
+
|
215
|
+
s.close();
|
216
|
+
|
217
|
+
}catch (IOException e2){}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
}
|
232
|
+
|
1
|
-
書籍を参考にチャットのサンプルプログラムを動かそうとしたのですが、
|
233
|
+
```書籍を参考にチャットのサンプルプログラムを動かそうとしたのですが、
|
2
234
|
|
3
235
|
|
4
236
|
|
@@ -68,245 +300,7 @@
|
|
68
300
|
|
69
301
|
|
70
302
|
|
71
|
-
|
303
|
+
|
72
|
-
|
73
|
-
チャットサーバーサンプルコード
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
import java.io.*;
|
78
|
-
|
79
|
-
import java.net.*;
|
80
|
-
|
81
|
-
import java.util.*;
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
public class chatsev
|
86
|
-
|
87
|
-
{
|
88
|
-
|
89
|
-
static final int Portnum=12345;
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
static ServerSocket svSocket;
|
94
|
-
|
95
|
-
static Vector connections;
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
public static void sendAll(String s){
|
100
|
-
|
101
|
-
if(connections !=null){
|
102
|
-
|
103
|
-
for(Enumeration e =connections.elements();
|
104
|
-
|
105
|
-
e.hasMoreElements();){
|
106
|
-
|
107
|
-
try{
|
108
|
-
|
109
|
-
PrintWriter out = new PrintWriter(
|
110
|
-
|
111
|
-
((Socket)e.nextElement()).getOutputStream());
|
112
|
-
|
113
|
-
out.println(s);
|
114
|
-
|
115
|
-
out.flush();
|
116
|
-
|
117
|
-
}catch(IOException ex){}
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
System.out.println(s);
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
public static void addConnection(Socket s){
|
132
|
-
|
133
|
-
if(connections == null){
|
134
|
-
|
135
|
-
connections = new Vector();
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
connections.addElement(s);
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
public static void deleteConnection(Socket s){
|
146
|
-
|
147
|
-
if(connections != null){
|
148
|
-
|
149
|
-
connections.removeElement(s);
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
public static void main(String[] arg){
|
158
|
-
|
159
|
-
try{
|
160
|
-
|
161
|
-
svSocket =new ServerSocket(Portnum);
|
162
|
-
|
163
|
-
}catch (IOException e){
|
164
|
-
|
165
|
-
System.err.println(e);
|
166
|
-
|
167
|
-
System.exit(1);
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
while(true){
|
174
|
-
|
175
|
-
try{
|
176
|
-
|
177
|
-
Socket cs =svSocket.accept();
|
178
|
-
|
179
|
-
addConnection(cs);
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
Thread ct =new Thread(new clientProc(cs));
|
184
|
-
|
185
|
-
ct.start();
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
}catch (IOException e){
|
190
|
-
|
191
|
-
System.err.println(e);
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
class clientProc implements Runnable{
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
Socket s;
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
BufferedReader in;
|
222
|
-
|
223
|
-
PrintWriter out;
|
224
|
-
|
225
|
-
String name =null;
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
public clientProc(Socket s) throws IOException{
|
230
|
-
|
231
|
-
this.s=s;
|
232
|
-
|
233
|
-
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
|
234
|
-
|
235
|
-
out = new PrintWriter(s.getOutputStream());
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
public void run(){
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
try{
|
250
|
-
|
251
|
-
while (name ==null){
|
252
|
-
|
253
|
-
name = in.readLine();
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
chatsev.sendAll("::"+name+"さんが入室しました");
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
String line =in.readLine();
|
266
|
-
|
267
|
-
while(!"logout".equals(line)){
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
chatsev.sendAll(name +">"+line);
|
272
|
-
|
273
|
-
line =in.readLine();
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
chatsev.sendAll("::"+name+"さんが退室しました");
|
280
|
-
|
281
|
-
chatsev.deleteConnection(s);
|
282
|
-
|
283
|
-
s.close();
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
}catch (IOException e){
|
288
|
-
|
289
|
-
try{
|
290
|
-
|
291
|
-
s.close();
|
292
|
-
|
293
|
-
}catch (IOException e2){}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
}
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
304
|
|
311
305
|
|
312
306
|
|