-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
103 lines (89 loc) · 3.42 KB
/
ClientHandler.java
File metadata and controls
103 lines (89 loc) · 3.42 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
/**
* ClientHandler.java
*
* This class is used to link the Server and Clients so that they can interact with each other.
*
*/
// Import appropriate libraries
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
public class ClientHandler implements Runnable{
// Initialized variables needed for player attributes to run the blackjack game or interact with others/server,
// such as their hand total
public static ArrayList<ClientHandler> clientHandlers = new ArrayList<>();
public Socket socket;
public BufferedReader bufferedReader;
public BufferedWriter bufferedWriter;
public String clientUsername;
public int handTotal;
public String messageFromClient;
public int win;
public int wonGames;
// ClientHandler constructor
public ClientHandler(Socket socket){
try{
// Initialize all the attribute variables to the appropriate values.
this.socket = socket;
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.clientUsername = bufferedReader.readLine();
this.handTotal = 0;
this.win = -1;
this.messageFromClient = " ";
clientHandlers.add(this);
broadcastMessage("Dealer: " + clientUsername + " has joined the table!");
}catch (IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
// Function used that will broadcast the message this client sends to everyone
@Override
public void run() {
while(socket.isConnected()){
try{
messageFromClient = bufferedReader.readLine();
broadcastMessage(messageFromClient);
}catch (IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
break;
}
}
}
// Function used to print out the passed in message for every client
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);
}
}
}
// Function used to remove client from clienthandlers arraylist if they disconnect
public void removeClientHandler(){
clientHandlers.remove(this);
broadcastMessage("Dealer: " + clientUsername + " has left the table!");
}
// Function used to close every open attribute of the client when they disconnect
public void closeEverything(Socket socket, BufferedReader bufferedReader, BufferedWriter bufferedWriter){
removeClientHandler();
try{
if(bufferedReader != null){
bufferedReader.close();
}
if(bufferedWriter != null){
bufferedWriter.close();
}
if(socket != null){
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}