-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaChatClientView.java
More file actions
198 lines (183 loc) · 5.53 KB
/
JavaChatClientView.java
File metadata and controls
198 lines (183 loc) · 5.53 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
// JavaChatClientView.java
// 실질적인 채팅 창
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
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.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class JavaChatClientView extends JFrame {
private JPanel contentPane;
private JTextField txtInput;
private String UserName;
private JButton btnSend;
private JTextArea textArea;
private static final int BUF_LEN = 128; // Windows 처럼 BUF_LEN 을 정의
private Socket socket; // 연결소켓
private InputStream is;
private OutputStream os;
private DataInputStream dis;
private DataOutputStream dos;
private JLabel lblUserName;
/**
* Create the frame.
*/
public JavaChatClientView(String username, String ip_addr, String port_no) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 392, 462);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 10, 352, 340);
contentPane.add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
txtInput = new JTextField();
txtInput.setBounds(91, 365, 185, 40);
contentPane.add(txtInput);
txtInput.setColumns(10);
btnSend = new JButton("Send");
btnSend.setBounds(288, 364, 76, 40);
contentPane.add(btnSend);
lblUserName = new JLabel("Name");
lblUserName.setHorizontalAlignment(SwingConstants.CENTER);
lblUserName.setBounds(12, 364, 67, 40);
contentPane.add(lblUserName);
setVisible(true);
AppendText("User " + username + " connecting " + ip_addr + " " + port_no);
UserName = username;
lblUserName.setText(username + ">");
try {
socket = new Socket(ip_addr, Integer.parseInt(port_no));
is = socket.getInputStream();
dis = new DataInputStream(is);
os = socket.getOutputStream();
dos = new DataOutputStream(os);
SendMessage("/login " + UserName);
ListenNetwork net = new ListenNetwork();
net.start();
Myaction action = new Myaction();
btnSend.addActionListener(action); // 내부클래스로 액션 리스너를 상속받은 클래스로
txtInput.addActionListener(action);
txtInput.requestFocus();
} catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
AppendText("connect error");
}
}
// Server Message를 수신해서 화면에 표시
class ListenNetwork extends Thread {
public void run() {
while (true) {
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();
socket.close();
break;
} catch (Exception ee) {
break;
}// catch문 끝
}
String msg = new String(b, "euc-kr");
msg = msg.trim(); // 앞뒤 blank NULL, \n 모두 제거
AppendText(msg); // server 화면에 출력
} catch (IOException e) {
AppendText("dis.read() error");
try {
dos.close();
dis.close();
socket.close();
break;
} catch (Exception ee) {
break;
} // catch문 끝
} // 바깥 catch문끝
}
}
}
// keyboard enter key 치면 서버로 전송
class Myaction implements ActionListener // 내부클래스로 액션 이벤트 처리 클래스
{
@Override
public void actionPerformed(ActionEvent e) {
// Send button을 누르거나 메시지 입력하고 Enter key 치면
if (e.getSource() == btnSend || e.getSource() == txtInput) {
String msg = null;
msg = String.format("[%s] %s\n", UserName, txtInput.getText());
SendMessage(msg);
txtInput.setText(""); // 메세지를 보내고 나면 메세지 쓰는창을 비운다.
txtInput.requestFocus(); // 메세지를 보내고 커서를 다시 텍스트 필드로 위치시킨다
if (msg.contains("/exit")) // 종료 처리
System.exit(0);
}
}
}
// 화면에 출력
public void AppendText(String msg) {
textArea.append(msg + "\n");
textArea.setCaretPosition(textArea.getText().length());
}
// 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();
System.exit(0);
}
for (i = 0; i < bb.length; i++)
packet[i] = bb[i];
return packet;
}
// Server에게 network으로 전송
public void SendMessage(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();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.exit(0);
}
}
}
}