-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientThread.java
383 lines (333 loc) · 12.1 KB
/
ClientThread.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package chattingprogramming;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.SimpleDateFormat;
public class ClientThread extends Thread
{
private ChatClient ct_client;
private Socket ct_sock;
private DataInputStream ct_in;
private DataOutputStream ct_out;
private StringBuffer ct_buffer;
private Thread thisThread;
private DisplayRoom room;
//private DisplayGame game;
private static final String SEPARATOR = "|";
private static final String DELIMETER = "`";
private static final int REQ_LOGON = 1001;
private static final int REQ_ENTERROOM = 1011;
private static final int REQ_SENDWORDS = 1021;
private static final int REQ_LOGOUT = 1031;
private static final int REQ_QUITROOM = 1041;
private static final int REQ_EXIT = 1050;
private static final int REQ_EXITROOM = 1051;
private static final int REC_ROOMLIST = 2050;
private static final int REC_LOGONLIST = 2051;
private static final int REC__WHISPERMESSAGE = 3001;
private static final int REC_GAME = 3333;
private static final int YES_GAME = 3334;
private static final int NO_GAME = 3335;
private static final int YES_LOGON = 2001;
private static final int NO_LOGON = 2002;
private static final int YES_ENTERROOM = 2011;
private static final int NO_ENTERROOM = 2012;
private static final int MDY_USERIDS = 2013;
private static final int YES_SENDWORDS = 2021;
private static final int NO_SENDWORDS = 2022;
private static final int YES_LOGOUT = 2031;
private static final int NO_LOGOUT = 2032;
private static final int YES_QUITROOM = 2041;
private static final int roomstate = 2050;
private static final int YES_EXIT = 2060;
private static final int NO_EXIT = 20601;
private static final int YES_WHISPERMESSAGE = 2070;
private static final int NO_WHISPERMESSAGE = 2071;
private static final int MSG_ALREADYUSER = 3001;
private static final int MSG_SERVERFULL = 3002;
private static final int MSG_CANNOTOPEN = 3011;
private static MessageBox msgBox, logonbox;
public String id;
public ClientThread(ChatClient client) {
try{
ct_sock = new Socket(InetAddress.getLocalHost(), 2777);
ct_in = new DataInputStream(ct_sock.getInputStream());
ct_out = new DataOutputStream(ct_sock.getOutputStream());
ct_buffer = new StringBuffer(4096);
thisThread = this;
ct_client = client;
}catch(IOException e){
MessageBoxLess msgout = new MessageBoxLess(client, "에러", "You cannot enter in Server..");
msgout.show();
}
}
public void run(){
try{
Thread currThread = Thread.currentThread();
while(currThread == thisThread){
String recvData = ct_in.readUTF();
StringTokenizer st = new StringTokenizer(recvData, SEPARATOR);
int command = Integer.parseInt(st.nextToken());
switch(command){
case YES_LOGON:{
logonbox.dispose();
ct_client.cc_tfStatus.setText("Long compelte :D");
ct_client.cc_btLogon.setEnabled(false);
ct_client.cc_tfLogon.setEditable(false);
String date = st.nextToken();
ct_client.cc_tfDate.setText(date);
id = st.nextToken();
break;
}
case NO_LOGON:{
int errcode = Integer.parseInt(st.nextToken());
ct_client.cc_tfLogon.setText("");
if(errcode == MSG_ALREADYUSER){
logonbox.dispose();
msgBox = new MessageBox(ct_client, "로그인 오류", "duplicated ID");
msgBox.show();
}else if(errcode == MSG_SERVERFULL){
logonbox.dispose();
msgBox = new MessageBox(ct_client, "로그인 오류", "Full Room");
msgBox.show();
}
break;
}
case REC_LOGONLIST:{
ct_client.cc_lstMember.clear();
String ids = st.nextToken();
System.out.println(ids);
StringTokenizer users = new StringTokenizer(ids, DELIMETER);
while(users.hasMoreTokens()){
ct_client.cc_lstMember.add(users.nextToken());
}
break;
}
case YES_ENTERROOM:{
ct_client.dispose();
room = new DisplayRoom(ct_client,this, "대화방");
room.pack();
room.show();
break;
}
case NO_ENTERROOM:{
int roomerrcode = Integer.parseInt(st.nextToken());
if(roomerrcode == MSG_CANNOTOPEN){
msgBox = new MessageBox(ct_client, "대화방입장 오류", "Not logon ID");
msgBox.show();
}
break;
}
case roomstate:{
String message = st.nextToken();
room.dr_taContents.append(message);
break;
}
case MDY_USERIDS:{
room.dr_lstMember.clear();
String ids = st.nextToken();
StringTokenizer roomusers = new StringTokenizer(ids, DELIMETER);
while(roomusers.hasMoreTokens()){
room.dr_lstMember.add(roomusers.nextToken());
}
break;
}
case YES_SENDWORDS:{
String id = st.nextToken();
try{
String data = st.nextToken();
room.dr_taContents.append(id+" : "+data+"\n");
}catch(NoSuchElementException e){}
room.dr_tfInput.setText("");
break;
}
case YES_LOGOUT:{
ct_client.cc_lstMember.clear();
ct_client.cc_btLogon.setEnabled(true);
ct_client.cc_tfLogon.setEditable(true);
ct_client.cc_tfLogon.setText("");
ct_client.cc_tfStatus.setText("ID를 입력하세요!");
ct_client.cc_tfDate.setText("...");
id=null;
MessageBox msgBox = new MessageBox(ct_client, "완료", "Logout complete");
msgBox.show();
break;
}
case YES_WHISPERMESSAGE:{
String message = st.nextToken();
room.dr_taContents.append(message);
break;
}
case NO_WHISPERMESSAGE:{
String errormessage = st.nextToken();
room.dr_taContents.append(errormessage);
break;
}
case YES_GAME:{
//ct_client.dispose();
//game = new DisplayGame("게임방");
//game.pack();
//game.show();
break;
}
case NO_GAME:{
}
case YES_QUITROOM:{
room.dr_lstMember.clear();
System.out.println("st.countTokens()"+st.countTokens());
if(st.countTokens()>0) {
String ids = st.nextToken();
StringTokenizer roomusers = new StringTokenizer(ids, DELIMETER);
while(roomusers.hasMoreTokens()){
room.dr_lstMember.add(roomusers.nextToken());
}
}
break;
}
case YES_EXIT:{
try {
id = "";
ct_in.close();
ct_out.close();
ct_sock.close();
System.out.println("RETURN\n");
System.exit(0);
}catch(IOException e) {
e.printStackTrace();
}
}
}
Thread.sleep(200);
}
}catch(InterruptedException e){
System.out.println(e);
release();
}catch(IOException e){
System.out.println(e);
release();
}
}
// 네트워크 자원을 해제한다.
public void release(){
if(id!=null) {//로그인상태 일때
MessageBox msgBox = new MessageBox(ct_client, "경고", "Logon state yes");
msgBox.show();
}
else {
try {
ct_buffer.setLength(0);
ct_buffer.append(REQ_EXIT);
ct_buffer.append(SEPARATOR);
send(ct_buffer.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
};
public void requestLogon(String id) {
try{
logonbox = new MessageBox(ct_client, "로그온", "Loging...");
logonbox.show();
ct_buffer.setLength(0);
ct_buffer.append(REQ_LOGON);
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
send(ct_buffer.toString());
}catch(IOException e){
System.out.println(e);
}
}
public void requestEnterRoom(String id) {
try{
ct_buffer.setLength(0); // EnterRoom 패킷을 생성한다.
ct_buffer.append(REQ_ENTERROOM);
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
send(ct_buffer.toString()); // EnterRoom 패킷을 전송한다.
}catch(IOException e){
System.out.println(e);
}
}
public void requestQuitRoom(String id) {
try{
ct_buffer.setLength(0);
ct_buffer.append(REQ_QUITROOM);
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
send(ct_buffer.toString());
}catch(IOException e){
System.out.println(e);
}
}
public void requestSendWords(String words) {
try{
ct_buffer.setLength(0);
ct_buffer.append(REQ_SENDWORDS);
ct_buffer.append(SEPARATOR);
ct_buffer.append(ct_client.msg_logon);
ct_buffer.append(SEPARATOR);
ct_buffer.append(words);
send(ct_buffer.toString());
}catch(IOException e){
System.out.println(e);
}
}
public void requestLogout(String id) {
try{
System.out.println("requestLogout id"+ id);
ct_buffer.setLength(0);
ct_buffer.append(REQ_LOGOUT);//로그아웃 타이틀
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
send(ct_buffer.toString());
}catch(IOException e){
System.out.println(e);
}
}
public void roomexit(String id) {
try {
ct_buffer.setLength(0);
ct_buffer.append(REQ_EXITROOM);
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
send(ct_buffer.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
public void requestwhisper(String userid, String whisperid, String meg) {
try {
ct_buffer.setLength(0);
ct_buffer.append(REC__WHISPERMESSAGE);
ct_buffer.append(SEPARATOR);
ct_buffer.append(id);
ct_buffer.append(SEPARATOR);
ct_buffer.append(whisperid);
ct_buffer.append(SEPARATOR);
ct_buffer.append(meg);
send(ct_buffer.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
public void requestgame(String userid) {
try {
ct_buffer.setLength(0);
ct_buffer.append(REC_GAME);
//ct_buffer.append(SEPARATOR);
//ct_buffer.append(id);
//ct_buffer.append(SEPARATOR);
//ct_buffer.append(gameid);
//ct_buffer.append(SEPARATOR);
//ct_buffer.append(meg);
send(ct_buffer.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
// 클라이언트에서 메시지를 전송한다.
private void send(String sendData) throws IOException {
ct_out.writeUTF(sendData);
ct_out.flush();
}
}