-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaChatServer.java
More file actions
327 lines (302 loc) ยท 10.5 KB
/
JavaChatServer.java
File metadata and controls
327 lines (302 loc) ยท 10.5 KB
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
// JavaChatServer.java
// Java Chatting Server
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class JavaChatServer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextArea textArea;
private JTextField txtPortNumber;
private ServerSocket socket; // ์๋ฒ์์ผ
private Socket client_socket; // accept() ์์ ์์ฑ๋ client ์์ผ
private Vector UserVec = new Vector(); // ์ฐ๊ฒฐ๋ ์ฌ์ฉ์๋ฅผ ์ ์ฅํ ๋ฒกํฐ
private static final int BUF_LEN = 128; // Windows ์ฒ๋ผ BUF_LEN ์ ์ ์
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaChatServer frame = new JavaChatServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JavaChatServer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 338, 386);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 10, 300, 244);
contentPane.add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
JLabel lblNewLabel = new JLabel("Port Number");
lblNewLabel.setBounds(12, 264, 87, 26);
contentPane.add(lblNewLabel);
txtPortNumber = new JTextField();
txtPortNumber.setHorizontalAlignment(SwingConstants.CENTER);
txtPortNumber.setText("30000");
txtPortNumber.setBounds(111, 264, 199, 26);
contentPane.add(txtPortNumber);
txtPortNumber.setColumns(10);
JButton btnServerStart = new JButton("Server Start");
btnServerStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
socket = new ServerSocket(Integer.parseInt(txtPortNumber.getText()));
} catch (NumberFormatException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AppendText("Chat Server Running..");
btnServerStart.setText("Chat Server Running..");
btnServerStart.setEnabled(false); // ์๋ฒ๋ฅผ ๋์ด์ ์คํ์ํค์ง ๋ชป ํ๊ฒ ๋ง๋๋ค
txtPortNumber.setEnabled(false); // ๋์ด์ ํฌํธ๋ฒํธ ์์ ๋ชป ํ๊ฒ ๋ง๋๋ค
AcceptServer accept_server = new AcceptServer();
accept_server.start();
}
});
btnServerStart.setBounds(12, 300, 300, 35);
contentPane.add(btnServerStart);
}
// ์๋ก์ด ์ฐธ๊ฐ์ accept() ํ๊ณ user thread๋ฅผ ์๋ก ์์ฑํ๋ค.
class AcceptServer extends Thread {
@SuppressWarnings("unchecked")
public void run() {
while (true) { // ์ฌ์ฉ์ ์ ์์ ๊ณ์ํด์ ๋ฐ๊ธฐ ์ํด while๋ฌธ
try {
AppendText("Waiting clients ...");
client_socket = socket.accept(); // accept๊ฐ ์ผ์ด๋๊ธฐ ์ ๊น์ง๋ ๋ฌดํ ๋๊ธฐ์ค
AppendText("์๋ก์ด ์ฐธ๊ฐ์ from " + client_socket);
// User ๋น ํ๋์ฉ Thread ์์ฑ
UserService new_user = new UserService(client_socket);
UserVec.add(new_user); // ์๋ก์ด ์ฐธ๊ฐ์ ๋ฐฐ์ด์ ์ถ๊ฐ
AppendText("์ฌ์ฉ์ ์
์ฅ. ํ์ฌ ์ฐธ๊ฐ์ ์ " + UserVec.size());
new_user.start(); // ๋ง๋ ๊ฐ์ฒด์ ์ค๋ ๋ ์คํ
} catch (IOException e) {
AppendText("!!!! accept ์๋ฌ ๋ฐ์... !!!!");
}
}
}
}
public void AppendText(String str) {
// textArea.append("์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ด์จ ๋ฉ์ธ์ง : " + str+"\n");
textArea.append(str + "\n");
textArea.setCaretPosition(textArea.getText().length());
}
// User ๋น ์์ฑ๋๋ Thread
// Read One ์์ ๋๊ธฐ -> Write All
class UserService extends Thread {
private InputStream is;
private OutputStream os;
private DataInputStream dis;
private DataOutputStream dos;
private Socket client_socket;
private Vector user_vc;
public String UserName = "";
public String UserStatus = "O";
public UserService(Socket client_socket) {
// TODO Auto-generated constructor stub
// ๋งค๊ฐ๋ณ์๋ก ๋์ด์จ ์๋ฃ ์ ์ฅ
this.client_socket = client_socket;
this.user_vc = UserVec;
try {
is = client_socket.getInputStream();
dis = new DataInputStream(is);
os = client_socket.getOutputStream();
dos = new DataOutputStream(os);
// line1 = dis.readUTF();
// /login user1 ==> msg[0] msg[1]
byte[] b = new byte[BUF_LEN];
dis.read(b);
String line1 = new String(b);
String[] msg = line1.split(" ");
UserName = msg[1].trim();
AppendText("์๋ก์ด ์ฐธ๊ฐ์ " + UserName + " ์
์ฅ.");
WriteOne("Welcome to Java chat server\n");
WriteOne(UserName + "๋ ํ์ํฉ๋๋ค.\n"); // ์ฐ๊ฒฐ๋ ์ฌ์ฉ์์๊ฒ ์ ์์ ์์ ์๋ฆผ
WriteAll("[" + UserName + "]๋์ด ์
์ฅํ์์ต๋๋ค.\n"); // ๋ชจ๋ ์ฐธ๊ฐ์์๊ฒ ์
์ฅ ์๋ฆผ
} catch (Exception e) {
AppendText("userService error");
}
}
// ๋ชจ๋ User๋ค์๊ฒ ๋ฐฉ์ก. ๊ฐ๊ฐ์ UserService Thread์ WriteOne() ์ ํธ์ถํ๋ค.
public void WriteAll(String str) {
for (int i = 0; i < user_vc.size(); i++) {
UserService user = (UserService) user_vc.elementAt(i);
if (user.UserStatus.equals("O")) {
user.WriteOne(str);
}
}
}
// Windows ์ฒ๋ผ message ์ ์ธํ ๋๋จธ์ง ๋ถ๋ถ์ NULL ๋ก ๋ง๋ค๊ธฐ ์ํ ํจ์
public byte[] MakePacket(String msg) {
byte[] packet = new byte[BUF_LEN];
byte[] bb = null;
int i;
for (i = 0; i < BUF_LEN; i++)
packet[i] = 0;
try {
bb = msg.getBytes("euc-kr");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (i = 0; i < bb.length; i++)
packet[i] = bb[i];
return packet;
}
// UserService Thread๊ฐ ๋ด๋นํ๋ Client ์๊ฒ 1:1 ์ ์ก
public void WriteOne(String msg) {
try {
// dos.writeUTF(msg);
byte[] bb;
bb = MakePacket(msg);
dos.write(bb, 0, bb.length);
} catch (IOException e) {
AppendText("dos.write() error");
try {
dos.close();
dis.close();
client_socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
UserVec.removeElement(this); // ์๋ฌ๊ฐ ๋ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฒกํฐ์์ ์ง์ด๋ค
AppendText("์ฌ์ฉ์ ํด์ฅ. ํ์ฌ ์ฐธ๊ฐ์ ์ " + UserVec.size());
}
}
public void run() {
while (true) { // ์ฌ์ฉ์ ์ ์์ ๊ณ์ํด์ ๋ฐ๊ธฐ ์ํด while๋ฌธ
try {
// String msg = dis.readUTF();
byte[] b = new byte[BUF_LEN];
int ret;
ret = dis.read(b);
if (ret < 0) {
AppendText("dis.read() < 0 error");
try {
dos.close();
dis.close();
client_socket.close();
UserVec.removeElement(this); // ์๋ฌ๊ฐ ๋ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฒกํฐ์์ ์ง์ด๋ค
WriteAll("[" + UserName + "]๋์ด ํด์ฅํ์์ต๋๋ค.\n");
AppendText("์ฌ์ฉ์ [" + UserName + "] ํด์ฅ. ๋จ์ ์ฐธ๊ฐ์ ์ " + UserVec.size());
break;
} catch (Exception ee) {
break;
} // catch๋ฌธ ๋
}
String msg = new String(b, "euc-kr");
msg = msg.trim(); // ์๋ค blank NULL, \n ๋ชจ๋ ์ ๊ฑฐ
try {
String[] args = msg.split(" ");
if (args[1].equals("/exit")) {
try {
dos.close();
dis.close();
client_socket.close();
AppendText(msg);
UserVec.removeElement(this); // ์๋ฌ๊ฐ ๋ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฒกํฐ์์ ์ง์ด๋ค
WriteAll("[" + UserName + "]๋์ด ํด์ฅํ์์ต๋๋ค.\n");
AppendText("์ฌ์ฉ์ [" + UserName + "] ํด์ฅ. ํ์ฌ ์ฐธ๊ฐ์ ์ " + UserVec.size());
break;
} catch (Exception ee) {
break;
} // catch๋ฌธ ๋
}
if (args[1].equals("/list")) {
WriteOne("User List\nname\tstatus\n---------------------------------\n");
for (int j = 0; j < UserVec.size(); j++) {
UserService user = (UserService) user_vc.elementAt(j);
String buf = String.format("%s\t%s\n", user.UserName, user.UserStatus);
WriteOne(buf);
}
AppendText(msg);
continue;
}
if (args[1].equals("/to")) { // ๊ท์๋ง
for (int j = 0; j < UserVec.size(); j++) {
String whisper = "";
String buf = "";
UserService user = (UserService) user_vc.elementAt(j);
if (args[2].equals(user.UserName) && user.UserStatus.equals("O")) {
whisper = msg;
whisper = whisper.substring(args[0].length() + args[1].length() + args[2].length() + 3);
buf = String.format("[๊ท์๋ง] [%s] %s", UserName, whisper);
user.WriteOne(buf);
}
}
AppendText(msg);
continue;
}
if (args[1].equals("/sleep")) {
UserStatus = "S";
AppendText(msg);
continue;
}
if (args[1].equals("/wakeup")) {
UserStatus = "O";
AppendText(msg);
continue;
}
AppendText(msg); // server ํ๋ฉด์ ์ถ๋ ฅ
WriteAll(msg + "\n"); // Write All
} catch (ArrayIndexOutOfBoundsException e) { // sleep ์ํ์์ enter key์น๋ฉด wakeup
UserStatus = "O";
AppendText(msg);
continue;
}
} catch (IOException e) {
AppendText("dis.read() error");
try {
dos.close();
dis.close();
client_socket.close();
UserVec.removeElement(this); // ์๋ฌ๊ฐ ๋ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฒกํฐ์์ ์ง์ด๋ค
WriteAll("[" + UserName + "]๋์ด ํด์ฅํ์์ต๋๋ค.\n");
AppendText("์ฌ์ฉ์ [" + UserName + "] ํด์ฅ. ๋จ์ ์ฐธ๊ฐ์ ์ " + UserVec.size());
break;
} catch (Exception ee) {
break;
} // catch๋ฌธ ๋
} // ๋ฐ๊นฅ catch๋ฌธ๋
} // while
} // run
}
}