-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdb82ce
commit 9eab2b5
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |