-
Notifications
You must be signed in to change notification settings - Fork 36
/
Client.java
94 lines (76 loc) · 2.48 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
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.io.StringReader;
public class Client {
private String host;
private int port;
public static void main(String[] args) throws UnknownHostException, IOException {
new Client("127.0.0.1", 12345).run();
}
public Client(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws UnknownHostException, IOException {
// connect client to server
Socket client = new Socket(host, port);
System.out.println("Client successfully connected to server!");
// Get Socket output stream (where the client send her mesg)
PrintStream output = new PrintStream(client.getOutputStream());
// ask for a nickname
Scanner sc = new Scanner(System.in);
System.out.print("Enter a nickname: ");
String nickname = sc.nextLine();
// send nickname to server
output.println(nickname);
// create a new thread for server messages handling
new Thread(new ReceivedMessagesHandler(client.getInputStream())).start();
// read messages from keyboard and send to server
System.out.println("Messages: \n");
// while new messages
while (sc.hasNextLine()) {
output.println(sc.nextLine());
}
// end ctrl D
output.close();
sc.close();
client.close();
}
}
class ReceivedMessagesHandler implements Runnable {
private InputStream server;
public ReceivedMessagesHandler(InputStream server) {
this.server = server;
}
public void run() {
// receive server messages and print out to screen
Scanner s = new Scanner(server);
String tmp = "";
while (s.hasNextLine()) {
tmp = s.nextLine();
if (tmp.charAt(0) == '[') {
tmp = tmp.substring(1, tmp.length()-1);
System.out.println(
"\nUSERS LIST: " +
new ArrayList<String>(Arrays.asList(tmp.split(","))) + "\n"
);
}else{
try {
System.out.println("\n" + getTagValue(tmp));
// System.out.println(tmp);
} catch(Exception ignore){}
}
}
s.close();
}
// I could use a javax.xml.parsers but the goal of Client.java is to keep everything tight and simple
public static String getTagValue(String xml){
return xml.split(">")[2].split("<")[0] + xml.split("<span>")[1].split("</span>")[0];
}
}