-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
148 lines (122 loc) · 4.66 KB
/
Client.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.net.Socket;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
// Define our client class
public class Client
{
// Constants
private final static int PORT_NUMBER = 6666;
private final static String ADDRESS = "localhost";
private Socket socket;
private BufferedReader bReader;
private BufferedWriter bWriter;
private String username;
// Constructor for the class
public Client(Socket socket, String username)
{
// Try to set the resources
try {
this.socket = socket;
this.bReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.bWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.username = username;
} catch (IOException e) {
e.printStackTrace();
// Disconnect the client if something went wrong
disconnect(socket, bReader, bWriter);
}
}
// Sets up core loop for sending new messages to server
public void sendInput()
{
// Try to start sending messages
try {
// Send the username over first
bWrite(this, username);
// Establish a new scanner object to read from stdin
Scanner scanner = new Scanner(System.in);
// While the socket is alive, wait for new input and send
while (socket.isConnected()) {
// Get new message from input
String message = scanner.nextLine();
// Write the message to the buffered writer and send
bWrite(this, username + ": " + message);
}
// Close the scanner
scanner.close();
} catch (IOException e) {
e.printStackTrace();
// Disconnect the client if something went wrong
disconnect(socket, bReader, bWriter);
}
}
// Handles disconnecting the client and closing all resources
public void disconnect(Socket socket, BufferedReader bReader, BufferedWriter bWriter)
{
// Try to close all resources
try {
if (bReader != null) bReader.close();
if (bWriter != null) bWriter.close();
if (socket != null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Handles receiving messages from the server
public void listenIncoming()
{
// Create a new Thread object with a new Runnable class
new Thread(new Runnable() {
// Define run method for runnable class
@Override
public void run()
{
String message;
// While the socket is still alive, try get new messages from server
while (socket.isConnected()) {
// Try get messages from server
try {
// Get the message from the buffered reader and output
message = bReader.readLine();
System.out.println(message);
} catch (IOException e) {
e.printStackTrace();
// Disconnect the socket if an error occurred
disconnect(socket, bReader, bWriter);
// Break out of the loop
break;
}
}
}
}).start(); // Start the thread
}
// Helper function to send message using buffered reader
public static void bWrite(Client client, String message) throws IOException
{
client.bWriter.write(message);
client.bWriter.newLine();
client.bWriter.flush();
}
// Main entry point for program
public static void main(String[] args) throws IOException
{
// Create a new scanner object to read from stdin
Scanner scanner = new Scanner(System.in);
// Output message to collect username
System.out.println("Enter a username: ");
// Get username from input
String username = scanner.nextLine();
// Create a new socket and client
Socket socket = new Socket(ADDRESS, PORT_NUMBER);
Client client = new Client(socket, username);
// Listen for incoming messages on a new thread, and send any posted messages to server
client.listenIncoming();
client.sendInput();
// Close the scanner
scanner.close();
}
}