-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathClient.java
More file actions
45 lines (36 loc) · 1.61 KB
/
MathClient.java
File metadata and controls
45 lines (36 loc) · 1.61 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
import java.io.*;
import java.net.*;
import java.util.Scanner;
class MathClient {
public static void main(String argv[]) {
try (Socket clientSocket = new Socket("localhost", 6789)) {
System.out.println("Connected to Math Server.");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Scanner scanner = new Scanner(System.in);
while (true) {
// Read menu from server
System.out.println(inFromServer.readLine());
// Get user choice
System.out.print("Enter choice: ");
String choice = scanner.nextLine();
outToServer.writeBytes(choice + "\n");
if (choice.equals("4")) {
System.out.println("Exiting...");
break;
}
// Read next instruction
System.out.println(inFromServer.readLine());
// Get operands from user
// System.out.print("Enter operands (separated by space): ");
String operands = scanner.nextLine();
outToServer.writeBytes(operands + "\n");
// Read result from server
System.out.println(inFromServer.readLine());
}
scanner.close();
} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
}
}
}