forked from syssoft-ds/UDP-TCP-Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCP_Chat_Client.java
100 lines (86 loc) · 3.68 KB
/
TCP_Chat_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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCP_Chat_Client {
private static String name;
private static String serverIP;
private static int serverPort;
private static void fatal(String input) {
System.err.println(input);
System.exit(-1);
}
public static boolean isIP(String ip) { // Checks if String is valid IPv4 address
return UDP_Chat.isIP(ip);
}
public static boolean isPort(String port) {
return UDP_Chat.isPort(port);
}
public static void main(String[] args) {
// Handling arguments, checking validity
if (args.length != 3) {
fatal("Arguments: \"<server ip address> <server port number> <client name>\"");
}
if (!isIP(args[0])) {
fatal("Invalid IP address");
} else {
serverIP = args[0];
}
if (!isPort(args[1])) {
fatal("Invalid port number");
} else {
serverPort = Integer.parseInt(args[1]);
}
name = args[2];
try (Socket socket = new Socket(serverIP, serverPort);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
// closes automatically
new Thread(() -> {
try {
String fromServer;
while ((fromServer = in.readLine()) != null) {
if (fromServer.equalsIgnoreCase("ping")) {
out.println("pong");
continue;
}
System.out.println(fromServer);
}
} catch (IOException e) {
fatal("Unable to get message from Server.");
}
}).start();
// Register the client with the server
out.println("register " + name);
System.out.println(name + " is connected to Server at IP " + serverIP + " on port " + serverPort
+ ".\nUse \"send <client name> <message>\" to send a message to a client.");
String userInput;
while ((userInput = stdIn.readLine()) != null) {
String[] parts = userInput.split(" ", 3);
if (parts[0].equalsIgnoreCase("send") && parts.length == 3) {
out.println(userInput);
System.out.println("Message sent.");
} else if (parts[0].equalsIgnoreCase("getclientlist")) {
out.println("getclientlist");
System.out.println("Client list requested.");
} else if (parts[0].equalsIgnoreCase("ask") && parts.length == 3) {
out.println(userInput);
System.out.println("Question asked.");
} else if (parts[0].equalsIgnoreCase("set") && parts.length == 3) {
out.println(userInput);
System.out.println("Answer set.");
} else {
System.err.println("Unknown command.");
}
}
} catch (UnknownHostException e) {
fatal("Unknown Server with IP " + serverIP);
} catch (IOException e) {
fatal("Unable to send message.");
}
System.exit(0);
}
}