-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatroomClient.java
More file actions
208 lines (179 loc) · 7.13 KB
/
Copy pathChatroomClient.java
File metadata and controls
208 lines (179 loc) · 7.13 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
199
200
201
202
203
204
205
206
207
208
/* Java Programming
* @author Abby Tse
* Use Sockets (including ServerSocket) to implement a Network Server and a Network Client such that
* when both the Server and the Client run, the messages type in at the client site will show on the server
* site and also the messages type in at the server site will also show at the Client site.
* ChatbotClient.java
* Client GUI: To connect, first run server GUI and get the IP address from the top.
* Then, set the Server IP on the client. Afterwards, you should be connected
*/
import java.io.*;
import java.net.*;
import java.util.Optional;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class ChatroomClient extends Application {
Socket socket = new Socket();
DataOutputStream os;
DataInputStream reader;
TextArea text;
public static void main(final String[] args) throws UnknownHostException, IOException {
launch(args);
}
@Override
public void start(final Stage primaryStage) throws Exception {
Button btn = new Button();
TextArea chat = new TextArea();
Text title = new Text();
title.setText("Chatbot Client");
Font font = Font.font("Times New Roman", 25);
title.setFont(font);
title.setFill(Color.GREEN);
chat.setPrefWidth(470);
chat.setPrefRowCount(1);
text = new TextArea();
text.setEditable(false);
// ensure that it scrolls to the bottom to see chat
text.textProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
text.setScrollTop(Double.MAX_VALUE); // this will scroll to the bottom
}
});
ScrollPane scrollPane = new ScrollPane(); // pane to display text messages
scrollPane.setContent(text);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
btn.setText("Send");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String message = chat.getText();
chat.clear();
sendMessage(message);
updateText("Client: " + message);
}
});
// create a text input dialog
TextInputDialog td = new TextInputDialog("localhost");
// setHeaderText
td.setHeaderText("Enter Server IP Address");
// create a button
Button d = new Button("Connect to Server IP");
// create a event handler
//handles connection to the server and exceptions
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
Optional<String> result = td.showAndWait();
if (result.isPresent()) {
try {
socket.connect(new InetSocketAddress(InetAddress.getByName(td.getEditor().getText()), 55286),80);
} catch (SocketTimeoutException exception) {
updateText("Invalid IP Address! " + exception.getMessage());
} catch (ConnectException cException) {
updateText("Server not started yet! " + cException.getMessage());
} catch (IOException e1) {
updateText("Error! " + e1.getMessage());
} finally{
if(!socket.isConnected()){
socket = new Socket();
}
}
} else {
updateText("You must input the server ip!");
}
//open input and output datastreams
if (socket.isConnected()) {
try {
reader = new DataInputStream(socket.getInputStream());
os = new DataOutputStream(socket.getOutputStream());
new ReceiveMessage(socket); // send socket receieve class
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
};
// set on action of event
d.setOnAction(event);
BorderPane root = new BorderPane();
GridPane grid = new GridPane();
grid.add(title,0,0);
title.setTextAlignment(TextAlignment.CENTER);
grid.add(d,0,1);
d.setTextAlignment(TextAlignment.CENTER);
grid.setPadding(new Insets(0,0,10,200));
grid.setVgap(5);
grid.setHgap(5);
HBox centerScene = new HBox();
centerScene.getChildren().add(text);
centerScene.setAlignment(Pos.CENTER);
HBox bottomScene = new HBox();
bottomScene.getChildren().add(chat);
bottomScene.getChildren().add(btn);
bottomScene.setAlignment(Pos.CENTER);
root.setTop(grid);
root.setCenter(centerScene);
root.setBottom(bottomScene);
root.setPadding(new Insets(20));
Scene scene = new Scene(root, 600, 350);
primaryStage.setTitle("Chatbot Client");
primaryStage.setScene(scene);
primaryStage.show();
}
//update text in display chat
private void updateText(String t) {
text.appendText(t + "\n");
}
// send message back to server
public void sendMessage(String message) {
try {
os.writeUTF(message);
os.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//intake messages
class ReceiveMessage implements Runnable { // class receive message
private final Socket socket;
public ReceiveMessage(Socket socket) { // constructor
this.socket = socket; // socket intializes
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.start();
}
public void run() {
try {
while (true) { // to continously receieve messages
String textmessage = reader.readUTF(); // read message from server
os.flush(); // flush
updateText("Server: " + textmessage);
}
} catch (EOFException eof) {
updateText("Server is down!");
updateText("Please exit application and reconnect.");
} catch (IOException e) {
updateText("Error " + e);
}
}
}
}