-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
52 lines (44 loc) · 1.87 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
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Client {
private static final String HOST = "localhost";
private static final int PORT = 6666;
private static String nick;
public static void main (String [] args){
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("[CLIENT] Please introduce your nick");
System.out.print("> ");
try {
nick = input.readLine();
System.out.println("[CLIENT] Hello "+nick+"! Now you are talking with everyone :)\n" +
"↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓");
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
System.out.println("Connected at "+dateFormat.format(new Date()));
} catch (IOException e) {
System.out.println("[CLIENT ERROR] Couldn't read from Keyboard");
}
MySocket s = new MySocket(nick, HOST, PORT);
Thread inputThread = new Thread(() -> {
try{
String line;
while ((line = input.readLine()) != null){
s.writeString(line);
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
System.out.println(dateFormat.format(new Date())+" << [ME]: "+line);
}
}catch (IOException e){
System.out.println("[CLIENT ERROR] Couldn't read from Keyboard");
}
});
inputThread.start();
Thread outputThread = new Thread(() -> {
String line;
while ((line = s.readString()) != null){
System.out.println(line);
}
});
outputThread.start();
}
}