-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava231_ChatClient.java
More file actions
178 lines (153 loc) · 3.88 KB
/
Copy pathJava231_ChatClient.java
File metadata and controls
178 lines (153 loc) · 3.88 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
package java0920_network;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Java231_ChatClient implements Runnable, ActionListener {
String userName;
String host;
int port;
private JFrame frm;
private JPanel pan;
private JTextArea taOutput;
private JLabel lbName;
private JTextField tfInput;
private DataInputStream dataIn;
private DataOutputStream dataOut;
Thread th;
public Java231_ChatClient() {
}
public Java231_ChatClient(String host, int port) {
System.out.print("사용자 이름을 입력하세요 : ");
Scanner sc = null;
sc = new Scanner(System.in);
userName = sc.nextLine();
if (userName.equals("")) {
userName = "guest";
}
this.host = host;
this.port = port;
initComponent();
}
private void initComponent() {
frm = new JFrame("채팅 프로그램 [" + host + ":" + port + "]");
taOutput = new JTextArea();
taOutput.setEditable(false);
tfInput = new JTextField(10);
pan = new JPanel();
lbName = new JLabel("입력 : ");
JScrollPane scroll = new JScrollPane(taOutput);
frm.add(BorderLayout.CENTER, scroll);
frm.add(BorderLayout.SOUTH, pan);
pan.setLayout(new BorderLayout());
pan.add(lbName, BorderLayout.WEST);
pan.add(tfInput, BorderLayout.CENTER);
tfInput.addActionListener(this);
frm.setSize(400, 400);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frm.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
stop();
}
});
}
private void stop() {
if (th != null) {
th.interrupt();
th = null;
try {
dataOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 메모리에서 정리
frm.setVisible(false);
frm.dispose();
System.exit(0);
}
private void initStart() {
if (th == null) {
Socket socket = null;
try {
socket = new Socket(host, port);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
dataIn = new DataInputStream(new BufferedInputStream(is));
dataOut = new DataOutputStream(new BufferedOutputStream(os));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
th = new Thread(this);
th.start();
}
}
public static void main(String[] args) {
Java231_ChatClient client = new Java231_ChatClient("127.0.0.1", 7777);
client.initStart();
}
@Override
public void run() {
System.out.println("메시지 수신 대기중");
while (!Thread.interrupted()) {
try {
String line = dataIn.readUTF();
taOutput.append(line + "\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
dataOut.writeUTF(userName + ":" + e.getActionCommand());
dataOut.flush();
tfInput.setText("");
tfInput.requestFocus();
} catch (IOException e1) {
handleIOException(e1);
}
}
private void handleIOException(IOException e) {
if (th != null) {
tfInput.setVisible(false);
frm.validate();
if (th != Thread.currentThread()) {
th.interrupt();
th = null;
try {
dataOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}