forked from ksh901016/java_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientExample.java
More file actions
157 lines (134 loc) · 4.75 KB
/
Copy pathClientExample.java
File metadata and controls
157 lines (134 loc) · 4.75 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
package chat;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientExample extends Application {
Socket socket;
void startClient(){
// 연결 시작 코드
Thread thread = new Thread(){
@Override
public void run(){
try {
socket = new Socket();
socket.connect(new InetSocketAddress("localhost", 5001));
Platform.runLater(()->{
displayText("[연결 완료: " + socket.getRemoteSocketAddress() + "]");
btnConn.setText("stop");
btnSend.setDisable(false);
});
}catch(Exception e){
Platform.runLater(()->displayText("[서버 통신 안됨]"));
if(!socket.isClosed()) { stopClient(); }
return;
}
receive(); // 서버에서 보낸 데이터 받기
}
};
thread.start();
}
void stopClient(){
// 연결 끊기 코드
try{
Platform.runLater(()->{
displayText("[연결 끊음]");
btnConn.setText("start");
btnSend.setDisable(true);
});
if(socket != null && !socket.isClosed()){
socket.close();
}
}catch(IOException e){ }
}
void receive(){
// 데이터 받기 코드
while(true){
try {
byte[] byteArr = new byte[100];
InputStream inputStream = socket.getInputStream();
int readByteCount = inputStream.read(byteArr);
if(readByteCount == -1) { throw new IOException();}
String data = new String(byteArr, 0, readByteCount, "UTF-8");
Platform.runLater(()->displayText("[받기 완료] " + data));
} catch (IOException e) {
Platform.runLater(()->displayText("[서버 통신 안됨]"));
stopClient();
break;
}
}
}
void send(String data){
// 데이터 전송 코드
Thread thread = new Thread(){
@Override
public void run() {
try {
byte[] byteArr = data.getBytes("UTF-8");
OutputStream outputStream = socket.getOutputStream();
outputStream.write(byteArr);
outputStream.flush();
Platform.runLater(()->displayText("[보내기 완료]"));
} catch (IOException e) {
Platform.runLater(()->displayText("[서버 통신 안됨]"));
stopClient();
}
}
};
thread.start();
}
///////
// UI 생성 코드
TextArea txtDisplay;
TextField txtInput;
Button btnConn, btnSend;
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setPrefSize(500, 300);
txtDisplay = new TextArea();
txtDisplay.setEditable(false);
BorderPane.setMargin(txtDisplay, new Insets(0,0,2,0));
root.setCenter(txtDisplay);
BorderPane bottom = new BorderPane();
txtInput = new TextField();
txtInput.setPrefSize(60, 30);
BorderPane.setMargin(txtInput, new Insets(0,1,1,1));
btnConn = new Button("start");
btnConn.setPrefSize(60, 30);
btnConn.setOnAction(e->{
if(btnConn.getText().equals("start")){
startClient();
}else if(btnConn.getText().equals("stop")){
stopClient();
}
});
btnSend = new Button("send");
btnSend.setPrefSize(60, 30);
btnSend.setDisable(true);
btnSend.setOnAction(e->send(txtInput.getText()));
bottom.setCenter(txtInput);
bottom.setLeft(btnConn);
bottom.setRight(btnSend);
root.setBottom(bottom);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("app.css").toString());
primaryStage.setScene(scene);
primaryStage.setTitle("Client");
primaryStage.setOnCloseRequest(event->stopClient());
primaryStage.show();
}
void displayText(String text){
txtDisplay.appendText(text + "\n");
}
}