-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
66 lines (62 loc) · 2.08 KB
/
Copy pathClient.java
File metadata and controls
66 lines (62 loc) · 2.08 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
package exam15package;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame{
private JLabel label = new JLabel("단어 입력");
private JTextField textField = new JTextField(10);
private JLabel resultLabel = new JLabel();
private Socket socket = null;
private BufferedReader in = null;
private BufferedWriter out = null;
public Client(){
super("스펠체크 클라이언트");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(label);
c.add(textField);
c.add(resultLabel);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTextField t = (JTextField)e.getSource();
try{
out.write(t.getText()+"\n");
out.flush();
resultLabel.setText(t.getText());
t.setText("");
}catch (Exception x){
x.printStackTrace();
}
}
});
setSize(400, 100);
setVisible(true);
this.play();
}
public void play(){
try{
socket = new Socket("localhost", 9999);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
String inputMessage = in.readLine();
resultLabel.setText(resultLabel.getText()+"는"+inputMessage);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(socket != null) socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
new Client();
}
}