-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientPart2.java
More file actions
42 lines (34 loc) · 1.55 KB
/
Copy pathClientPart2.java
File metadata and controls
42 lines (34 loc) · 1.55 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
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ClientPart2 {
public static void main(String[] args) {
ClientPart2 client = new ClientPart2();
client.bmi_client();
}
public void bmi_client() {
int port = 50001; // Server's port number
String hostname = "localhost"; // Server's hostname or IP address
try (Socket socket = new Socket(hostname, port);
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
Scanner scanner = new Scanner(System.in)) {
System.out.println("Connected to Scalable BMI Server");
// Get weight and height from the user
System.out.print("Enter your weight in kilograms: ");
double weight = scanner.nextDouble();
System.out.print("Enter your height in meters: ");
double height = scanner.nextDouble();
// Send weight and height to the server
writer.println(weight);
writer.println(height);
// Receive and display the BMI result from the server
String response = reader.readLine();
System.out.println(response);
} catch (IOException ex) {
System.out.println("Error: Unable to connect to the server (" + ex.getMessage() + ")");
}
}
}