-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuoteClient.java
More file actions
89 lines (66 loc) · 2.54 KB
/
QuoteClient.java
File metadata and controls
89 lines (66 loc) · 2.54 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
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
/** -----------------------------------------------------------------------
QuoteClient.java
@author William Clift
Operating Systems
25 March 2020
Compile and Run Instructions:
-------------------------------------------------------------------
By Assignment
-------------------------------------------------------------------
Compile: javac QuoteClient.java
Run: java Quote Client [numberOfQuotes]
Prints quotes of a random type
-------------------------------------------------------------------
Categories Option -- (Extra Credit Portion)
-------------------------------------------------------------------
Compile: javac QuoteClient.java
Run: java Quote Client [numberOfQuotes_0] [typeOfQuote_0] [number_1] [type_1] ... [number_n] [type_n]
//Type Choices: "Happy", "Reflective", "Nerdy_Computer", "Nerdy_Math", "By_William"
-------------------------------------------------------------------
Close Server (Kill Server Command):
Run: java QuoteClient x
------------------------------------------------------------------- **/
import java.net.*;
import java.io.*;
public class QuoteClient{
public static void main(String[] args){
try{
// Make Connection to Server Socket
Socket sock = new Socket("localhost", 6013);
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
PrintWriter pout = new PrintWriter(out, true);
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
if(args.length == 0){
System.out.println("Invalid Arguments.");
}else if(args[0].equals("x")){ // Indicates the server should shutdown
pout.println(args[0]);
}else{
// Send Request to Server
pout.println("c");
pout.println(args.length); // Sends the Size of args
for(String arg : args)
pout.println(arg);
pout.println("x"); // Signal that the list of arguments is over
// Read from Socket
String line;
boolean done = false;
System.out.println(); // Add space before the quotes
while((line = bin.readLine()) != null && !done){
if(line.equals("x")){ // When the quotes are returned, terminate connection
done = true;
} else{
System.out.println(line);
}
}
}
// Close Socket Connection
pout.close();
sock.close();
in.close();
}
catch(IOException ioe){
System.err.println(ioe);
}
}
}