Skip to content

Commit

Permalink
Added clientHandler Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kumar-Harshit223 authored Oct 17, 2022
1 parent bdb82ce commit 9eab2b5
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions KumarHarshit/ChatApp/src/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class ClientHandler implements Runnable {

public static ArrayList<ClientHandler> clientHandlers=new ArrayList<>();
private Socket socket;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private String clientUsername;

public ClientHandler(Socket socket) {
try {
this.socket=socket;
this.bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

this.bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.clientUsername=bufferedReader.readLine();
clientHandlers.add(this);

broadcastMessage("SERVER : "+clientUsername+" has entered the chat! ");
}
catch(IOException e) {
closeEverything(socket, bufferedReader, bufferedWriter);
}
}

public void run() {

String messageFromClient;

while(socket.isConnected()) {
try {
messageFromClient=bufferedReader.readLine();

broadcastMessage(messageFromClient);
}
catch(IOException e) {
closeEverything(socket, bufferedReader, bufferedWriter);
break;
}
}
}

public void broadcastMessage(String messageToSend) {

for(ClientHandler clientHandler : clientHandlers) {
try {
if(!clientHandler.clientUsername.equals(clientUsername)) {
clientHandler.bufferedWriter.write(messageToSend);
clientHandler.bufferedWriter.newLine();
clientHandler.bufferedWriter.flush();
}
}
catch(IOException e) {
closeEverything(socket,bufferedReader, bufferedWriter);
}
}
}

public void removeClientHandler() {
clientHandlers.remove(this);
broadcastMessage("SERVER : "+clientUsername+" has left the chat! ");
}

public void closeEverything(Socket socket, BufferedReader bufferdReader, BufferedWriter bufferedWriter) {
removeClientHandler();
try {
if(bufferedReader !=null) {
bufferedReader.close();
}
if(bufferedWriter !=null) {
bufferedWriter.close();
}
if(socket !=null) {
socket.close();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 9eab2b5

Please sign in to comment.