-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
104 lines (95 loc) · 3.78 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
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public Socket socket;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private String clientUsername;
public Client(Socket socket, String clientUsername){
try{
this.socket = socket;
this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.clientUsername = clientUsername;
}catch(Exception e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
public void sendMessage(){
try{
bufferedWriter.write(clientUsername);
bufferedWriter.newLine();
bufferedWriter.flush();
Scanner scanner = new Scanner(System.in);
while(socket.isConnected()){
String message = scanner.nextLine();
bufferedWriter.write(clientUsername + ':' + message);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}catch (Exception e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
public void listenForMessages(){
new Thread(new Runnable() {
@Override
public void run() {
String message;
while (socket.isConnected()){
try{
message = bufferedReader.readLine();
System.out.println(message);
} catch (IOException e) {
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
}
}).start();
}
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 username: ");
String username = scanner.nextLine();
Socket socket = new Socket("localhost", 6013);
Client client = new Client(socket, username);
client.listenForMessages();
client.sendMessage();
try {
Socket sock = new Socket("127.0.0.1", 6013);
OutputStream out = sock.getOutputStream();
PrintWriter pout = new PrintWriter(out, true);
// Read the input from the user
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter input: ");
String input = userInput.readLine();
// Send the input to the server
pout.println(input);
// Read the response from the server
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String response = bin.readLine();
String result = response.replaceAll(".%", ".\n");
System.out.println("Server response: \n" + result);
sock.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}