-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
52 lines (41 loc) · 1.47 KB
/
Client.java
File metadata and controls
52 lines (41 loc) · 1.47 KB
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.net.*;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
String tag = args[0];
try {
socket = new Socket("localhost", 4445);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
out.println(tag);
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
if (((fromServer.startsWith("ADD")) || (fromServer.startsWith("REMOVE")))) {
fromUser = stdIn.readLine();
if (fromUser != null) {
//System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
}
out.close();
in.close();
stdIn.close();
socket.close();
}
}