-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChatServer.java
More file actions
310 lines (268 loc) · 11.1 KB
/
ChatServer.java
File metadata and controls
310 lines (268 loc) · 11.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.io.*;
import java.net.*;
import java.util.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/*
Created by Bikram Shrestha
ChatServer class provide a GUI for the user to see
the various connection that is been established as
well as current active user list. This is performed
with the help of multi threading, The server listen
for connection from client continuously and create
a new thread to handle the communication with the
connected socket.
When the user try to connect to the server for the
first time, as client side can only send the userName,
it is checked whether there is user with same name in
the userList maintained in server side and if the
user name is unique, an accepted message is send to
the client and client is then only allow to receive
message and active userList.
*/
public class ChatServer extends Application {
// Label was create to label logList and userList.
Label lbLog = new Label("Log");
Label lbUserList = new Label("Active User");
/*
ArrayList for user and chat message was created
so that it can be used to create observable list.
*/
private ArrayList<String> logList = new ArrayList<>();
private ArrayList<String> userList = new ArrayList<>();
// List view for log and user was declared.
ListView<String> logListView = new ListView<String>();
ListView<String> userListView = new ListView<String>();
/*
ObservableList for ListView was created using
the arrayList of log and user list.
*/
ObservableList<String> logItems =
FXCollections.observableArrayList (logList);
ObservableList<String> userItems =
FXCollections.observableArrayList (userList);
// Mapping of sockets to output streams
private Hashtable outputStreams = new Hashtable();
//ArrayList of all open Socket.
private ArrayList<Socket> socketList = new ArrayList<>();
// Server socket
private ServerSocket serverSocket;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//Setting content to display for the ListVIew
userListView.setItems(userItems);
logListView.setItems(logItems);
logListView.setMinWidth(430);
// Creating GridPane to arrange all the node.
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(10));
//All the nodes are added to the gridPane.
gridPane.add(lbLog,0,0);
gridPane.add(logListView,0,1);
gridPane.add(lbUserList,0,2);
gridPane.add(userListView,0,3);
// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 450, 400);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
/*
Special care is taken to make sure that all the connection
to the client is been closed properly before closing the
application.
*/
primaryStage.setOnCloseRequest(t -> closeSocketExit());
// Start a new thread to listen for connection.
new Thread(() -> listen()).start();
}
/*
When this method is called, it make sure that all the
open socket, or connection to the client is terminated
properly.
*/
private void closeSocketExit() {
try {
for(Socket socket:socketList){
//If socket doesn't exit, no need to close.
if(socket!=null){
socket.close();
}
}
Platform.exit(); // Close UI.
} catch (IOException e) {
e.printStackTrace();
}
}
/*
This thread create a new serverSocket using the port 8000
and wait for user to connect. This is done in a loop so
that this server will be waiting and creating a new
connection as user join the server.
*/
private void listen() {
try {
// Create a server socket
serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
logItems.add("MultiThreadServer started at " + new Date()));
while (true) {// Listen for a new connection request
Socket socket = serverSocket.accept();
//Add accepted socket to the socketList.
socketList.add(socket);
// Display the client socket information and time connected.
Platform.runLater(() ->
logItems.add("Connection from " + socket + " at " + new Date()));
// Create output stream
DataOutputStream dataOutputStream =
new DataOutputStream(socket.getOutputStream());
// Save output stream to hashtable
outputStreams.put(socket, dataOutputStream);
// Create a new thread for the connection
new ServerThread(this, socket);
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
// This method dispatch userList to all user in the server.
private void dispatchUserList() {
this.sendToAll(userList.toString());
}
// Used to get the output streams
Enumeration getOutputStreams(){
return outputStreams.elements();
}
// Used to send message to all clients
void sendToAll(String message){
// Go through hashtable and send message to each output stream
for (Enumeration e = getOutputStreams(); e.hasMoreElements();){
DataOutputStream dout = (DataOutputStream)e.nextElement();
try {
// Write message
dout.writeUTF(message);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
// This method send onlineStatus to all the user excluding self.
void sendOnlineStatus(Socket socket,String message){
for (Enumeration e = getOutputStreams(); e.hasMoreElements();){
DataOutputStream dataOutputStream = (DataOutputStream)e.nextElement();
try {
//If it is same socket then don't send the message.
if(!(outputStreams.get(socket) == dataOutputStream)){
// Write message
dataOutputStream.writeUTF(message);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/*
Declaring a ServerThread class so that it can be
used to create a multi-thread server serving
a each socket in different thread.
*/
class ServerThread extends Thread {
private ChatServer server;
private Socket socket;
String userName; // Default null;
boolean userJoined; // Default false;
/** Construct a thread */
public ServerThread(ChatServer server, Socket socket) {
this.socket = socket;
this.server = server;
start();
}
/** Run a thread */
public void run() {
try {
// Create data input and output streams
DataInputStream dataInputStream =
new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream =
new DataOutputStream(socket.getOutputStream());
// Continuously serve the client
while (true) {
/*
When user connect to the server for first time, as it
can only send userName, the userName is checked against
the userList to make sure only one user with the same
name exist, and approve message is send if approved.
*/
if(!userJoined){
userName = dataInputStream.readUTF();
if(userList.contains(userName)){
dataOutputStream.writeUTF(userName);
System.out.println(userName + " already exist.");
}
else{
userList.add(userName);
dataOutputStream.writeUTF("Accepted");
server.dispatchUserList();
System.out.println(userName +" joined the chat room");
userJoined = true;
String userNotification = userName + " joined the chat room.";
Platform.runLater(() ->
logItems.add(userName + " joined the chat room."));
server.sendOnlineStatus(socket,userNotification);
userItems.clear();
userItems.addAll(userList);
}
}
/*
Once it join it can receive the message from the other
user in broadcast mode.
*/
else if(userJoined){
// User Message
String string = dataInputStream.readUTF();
// Send text back to the clients
server.sendToAll(string);
server.dispatchUserList();
// Add chat to the server jta
Platform.runLater(() ->logItems.add(string));
}
}
}
/*
When ever Exception is thrown due to closed socket, this is
handled properly so that further error does not occurs due
to non existence socket. The user is also removed from the
userList if it was able to register successfully before changing
the default value of null to userName. And relevant message is
broadcast to other user letting them know that the user has
left the chat due to closed socket.
*/
catch(IOException ex) {
System.out.println("Connection Closed for " + userName);
Platform.runLater(() ->
logItems.add("Connection Closed for " + userName));
if(!userName.equals(null)){
userList.remove(userName);
}
outputStreams.remove(socket);
server.dispatchUserList();
if (!userName.equals(null)){
server.sendToAll(userName + " has left the chat room.");
}
Platform.runLater(() ->{
userItems.clear();
userItems.addAll(userList);
});
}
}
}
}