-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoClient.java
More file actions
49 lines (45 loc) · 1.87 KB
/
EchoClient.java
File metadata and controls
49 lines (45 loc) · 1.87 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
/* Assignment 3 - Question 2 - EchoClient.java
Authors: John Hebb & Matthew Souter
Date: Monday, November 4th, 2018
Description: This program takes in 7 values and creates 3 different threads, one to find the average of the numbers, one to find the max value and one to find the min value
Github Link- https://github.com/John-Hebb/OS_Assignment3/blob/master/EchoClient.java
*/
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
// Error if too many or too few arguments
if (args.length != 1) {
System.err.println(
"Usage: EchoClient <host name>");
System.exit(1);
}
// Initialize host and port
String hostName = args[0];
int portNum = 9999;
// Open sockets, buffers, and streams
try (
Socket echoSocket = new Socket(hostName, portNum);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
) {
// Output the input to the server and grab echo and print to console
String input;
while ((input = stdIn.readLine()) != null) {
if (input.equals(".")){
System.exit(1);
} else {
out.println(input);
System.out.println("Server: " + in.readLine());
}
}
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("I/O for host " + hostName);
System.exit(1);
}
}
}