-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientHandler.java
127 lines (98 loc) · 4.34 KB
/
ClientHandler.java
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
import java.net.Socket;
import java.util.*;
import java.io.*;
//The class of Client handler runs inside a thread declared in Server
//Separate thread for separate client?
public class ClientHandler implements Runnable{
public static ArrayList<ClientHandler> clientHandlers = new ArrayList<>();
//initialising the array list
public Socket socket;
//initialising an inbuilt socket class
private BufferedReader bufferedReader;
//initialising a reader
private BufferedWriter bufferedWriter;
//initialising a writer
private String clientUsername;
//initialising the client's Username
public ClientHandler(Socket socket){
//initialising the socket once for 1 server (does not depend on the client)
//below process executes exactly once - instantiates the necessities this class requires and
//adds these details to an array list
//collects username once and broadcasts a message saying that a client has entered the chat
try{
this.socket = socket;
//assigning the input socket variable to the one inside the class
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//assigning a new bufferwriter
this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//assigning a new bufferreader
this.clientUsername = bufferedReader.readLine();
//collecting the new client's username from the first line of the input
clientHandlers.add(this);
//add all "this" to an arraylist. "this" refers to the full class
broadcastMessage("Server: " + clientUsername + " has entered the chat");
//sends message for everyone
}
catch(IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
//closes everything in case of an exception
}
}
//overrides the runnable class
@Override
public void run(){
String messageFromClient; //collects the message from the client
while(socket.isConnected()){ //while(1) 1 is whenever the socket is connected
try{
messageFromClient = bufferedReader.readLine(); //reads the message from the client
broadcastMessage(messageFromClient); //sends the message
}
catch(IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
break;
}
}
}
//this function inputs the message to be sent
//it broadcasts the message to all other users except itself
public void broadcastMessage(String messageToSend){
for(ClientHandler clientHandler : clientHandlers){ //for all the clienthandlers in the arraylist
try{
if(!clientHandler.clientUsername.equals(clientUsername)){
//if statement means that whenever the client username doesn't match a clientHandler's username,
// it will broadcast the message
clientHandler.bufferedWriter.write(messageToSend); //that particular clienthandler's bufferwriter gets return
clientHandler.bufferedWriter.newLine(); //a new line is initialised
clientHandler.bufferedWriter.flush(); //the data is flushed. when it is flushed, data is transmitted
//even when u dont close it
}
}
catch (IOException e) {
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
}
public void removeClientHandler(){
clientHandlers.remove(this); //remove "this" particular class
broadcastMessage("Server: " + clientUsername + "user has left the chat");
}
public void closeEverything(Socket socket, BufferedReader bufferedReader, BufferedWriter bufferedWriter){
removeClientHandler(); //removes the clienthandler
try{
//these codes are to avoid null pointer exception, which means that even when the values of these aren't null,
//they get closed
if(bufferedReader!=null){
bufferedReader.close();
}
if(bufferedWriter!=null){
bufferedReader.close();
}
if (socket != null){
socket.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}