forked from jerry10004/SimpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinChatClient.java
More file actions
126 lines (120 loc) · 3.25 KB
/
WinChatClient.java
File metadata and controls
126 lines (120 loc) · 3.25 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
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class WinChatClient extends Frame implements ActionListener{
private TextField idTF = null;
private TextField input = null;
private TextArea display = null;
private CardLayout cardLayout = null;
private BufferedReader br = null;
private PrintWriter pw = null;
private Socket sock = null;
public WinChatClient(String ip){
super("채팅 클라이언트");
cardLayout = new CardLayout();
setLayout(cardLayout);
Panel loginPanel = new Panel();
loginPanel.setLayout(new BorderLayout());
loginPanel.add("North", new Label("아이디를 입력하여 주신후 엔터를 입력하여 주세요."));
idTF = new TextField(20);
idTF.addActionListener(this);
Panel c = new Panel();
c.add(idTF);
loginPanel.add("Center", c);
add("login", loginPanel);
Panel main = new Panel();
main.setLayout(new BorderLayout());
input = new TextField();
input.addActionListener(this);
display = new TextArea();
display.setEditable(false);
main.add("Center", display);
main.add("South", input);
add("main", main);
try{
sock = new Socket(ip, 10001);
pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}catch(Exception ex){
System.out.println("서버와 접속시 오류가 발생하였습니다.");
System.out.println(ex);
System.exit(1);
}
setSize(500, 500);
cardLayout.show(this, "login");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
pw.println("/quit");
pw.flush();
try{
sock.close();
}catch(Exception ex){}
System.out.println("종료합니다.");
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
if(args.length != 1){
System.out.println("사용법 : java WinChatClient ip");
System.exit(1);
}
new WinChatClient(args[0]);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == idTF){
String id = idTF.getText();
if(id == null || id.trim().equals("")){
System.out.println("아이디를 다시 입력하여 주세요.");
return;
}
pw.println(id.trim());
pw.flush();
WinInputThread wit = new WinInputThread(sock, br);
wit.start();
cardLayout.show(this, "main");
input.requestFocus();
}else if(e.getSource() == input){
String msg = input.getText();
pw.println(msg);
pw.flush();
if(msg.equals("/quit")){
try{
sock.close();
}catch(Exception ex){}
System.out.println("종료합니다.");
System.exit(1);
}
input.setText("");
input.requestFocus();
}
} // actionPerformed
class WinInputThread extends Thread{
private Socket sock = null;
private BufferedReader br = null;
public WinInputThread(Socket sock, BufferedReader br){
this.sock = sock;
this.br = br;
}
public void run(){
try{
String line = null;
while((line = br.readLine()) != null){
display.append(line + "\n");
}
}catch(Exception ex){
}finally{
try{
if(br != null)
br.close();
}catch(Exception ex){}
try{
if(sock != null)
sock.close();
}catch(Exception ex){}
}
} // InputThread
} // WinInputThread end
} // WinChat Client class