-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
104 lines (92 loc) · 3.52 KB
/
Client.java
File metadata and controls
104 lines (92 loc) · 3.52 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
/**
* Client.java
*
* This class is used to represent the players at the blackjack table. It allows them to enter their username,
* as well as send and receive messages.
*
*/
// Import necessary libraries
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
// Initialize variables needed for clients to identify themselves and interact with others.
private Socket socket;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private String username;
// Client constructor
public Client(Socket socket, String username){
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.username = username;
}catch (IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
// Function used to allow this client to send messages to other clients and the server
// Print out who said what to every client
public void sendMessage(){
try{
bufferedWriter.write(username);
bufferedWriter.newLine();
bufferedWriter.flush();
Scanner scanner = new Scanner(System.in);
while(socket.isConnected()){
String messageToSend = scanner.nextLine();
bufferedWriter.write(username + ": " + messageToSend);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}catch(IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
// Function used to have the client constantly listen for a message so that
// in case a message is sent out from another client or a server, this client
// doesn't miss it and is able to read it and print it out
public void listenForMessage(){
new Thread(new Runnable(){
@Override
public void run(){
String msgFromGroupChat;
while(socket.isConnected()){
try{
msgFromGroupChat = bufferedReader.readLine();
System.out.println(msgFromGroupChat);
}catch(IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
}
}).start();
}
// Function used to close every open attribute of the client when they disconnect
public void closeEverything(Socket socket, BufferedReader bufferedReader, BufferedWriter bufferedWriter){
try{
if(bufferedReader != null){
bufferedReader.close();
}
if(bufferedWriter != null){
bufferedWriter.close();
}
if(socket != null){
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name for the game: ");
String username = scanner.nextLine();
Socket socket = new Socket("localhost", 1234);
Client client = new Client(socket, username);
client.listenForMessage();
client.sendMessage();
}
}