forked from ArshiyaFathimaAK/networks-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathClient.java
More file actions
53 lines (41 loc) · 1.59 KB
/
MathClient.java
File metadata and controls
53 lines (41 loc) · 1.59 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
package multithreading;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MathClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
// Send CONNECT
System.out.print("Enter your name: ");
String name = scanner.nextLine();
out.println("CONNECT|" + name);
String response = in.readLine();
if (!response.startsWith("ACK|SUCCESS")) {
System.out.println("Connection failed.");
socket.close();
return;
}
System.out.println("Connected to server.");
while (true) {
System.out.print("Enter a math calculation (or 'exit'): ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("exit")) {
out.println("DISCONNECT");
System.out.println(in.readLine()); // BYE
socket.close();
break;
}
out.println("REQ|" + input);
String res = in.readLine();
System.out.println("Server response: " + res);
}
} catch (Exception e) {
System.out.println("Connection error.");
e.printStackTrace();
}
}
}