-
Notifications
You must be signed in to change notification settings - Fork 0
/
BetterClient.java
119 lines (106 loc) · 3.82 KB
/
BetterClient.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.net.InetAddress;
import java.net.Socket;
/* client sends messages to server and receives responses
this version allows the server to send an initial greeting
and send multiples lines in each response */
class BetterClient {
private static InetAddress server;
private static Socket socket;
private static BufferedReader in;
private static PrintWriter out;
private static BufferedReader user;
private static String fromServer, userInput;
private static String eor = "[EOR]"; // a code for end-of-response
private static String exitCommand = "EXIT";
private static void setup(String [] args) {
// check arguments are correct
if (args.length != 2) {
toConsole("Usage: java LineClient host port");
System.exit(0);
}
int port = 0;
String host = null;
try {
host = args[0];
port = Integer.parseInt(args[1]);
}
catch( NumberFormatException nfex ) {
toConsole("Bad port number");
System.exit(0);
}
try {
/* determine the address of the server and connect to it */
server = InetAddress.getByName(host);
socket = new Socket(server, port);
toConsole("Connected: " + socket);
toConsole("\nType " + exitCommand + " to disconnect\n");
// get the input stream and attach to a buffered reader
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// get the output stream and attach to a printwriter
out = new PrintWriter(socket.getOutputStream(), true);
// set up a buffered reader for user input
user = new BufferedReader(new InputStreamReader(System.in));
// get an initial greeting from the server
while (!(fromServer = in.readLine()).equals(eor)) {
toConsole(fromServer);
}
}
catch(UnknownHostException uhex) {
toConsole("bad host name");
System.exit(0);
}
catch(IOException ioex) {
toConsole("io error:" + ioex);
System.exit(0);
}
}
// repeat a cycle of client requests and server responses
private static void talk() {
try {
while (true) {
// get a line of user input and send to server
userInput = user.readLine();
out.println(userInput);
out.flush(); // asks for it to be sent
if (userInput.toUpperCase().equals("EXIT")) {
disconnect();
}
// get all current input lines from the server (response)
while (!(fromServer = in.readLine()).equals(eor)) {
toConsole(fromServer);
}
}
}
catch(IOException ioex) {
toConsole("io error:" + ioex);
System.exit(0);
}
}
private static void disconnect() {
try {
in.close();
out.close();
toConsole("\nDisconnected\n");
}
catch(IOException ioex) {
toConsole("io error:" + ioex);
System.exit(0);
}
System.exit(0);
}
// worth using because it makes so much code shorter!
private static void toConsole(String message) {
System.out.println(message);
}
public static void main(String[] args) {
setup(args);
talk();
}
}