1 import java.io.*;
2 import java.net.*;
3 import javafx.application.Application;
4 import javafx.geometry.Insets;
5 import javafx.geometry.Pos;
6 import javafx.scene.Scene;
7 import javafx.scene.control.Label;
8 import javafx.scene.control.ScrollPane;
9 import javafx.scene.control.TextArea;
10 import javafx.scene.control.TextField;
11 import javafx.scene.layout.BorderPane;
12 import javafx.stage.Stage;
13
14 public class Client extends Application {
15
16 DataOutputStream toServer = null;
17 DataInputStream fromServer = null;
18
19 @Override
20 public void start(Stage primaryStage) {
21
22 BorderPane paneForTextField = new BorderPane();
23 paneForTextField.setPadding(new Insets(5, 5, 5, 5));
24 paneForTextField.setStyle("-fx-border-color: green");
25 paneForTextField.setLeft(new Label("Enter a radius: "));
26
27 TextField tf = new TextField();
28 tf.setAlignment(Pos.BOTTOM_RIGHT);
29 paneForTextField.setCenter(tf);
30
31 BorderPane mainPane = new BorderPane();
32
33 TextArea ta = new TextArea();
34 mainPane.setCenter(new ScrollPane(ta));
35 mainPane.setTop(paneForTextField);
36
37
38 Scene scene = new Scene(mainPane, 450, 200);
39 primaryStage.setTitle("Client");
40 primaryStage.setScene(scene);
41 primaryStage.show();
42
43 tf.setOnAction(e -> {
44 try {
45
46 double radius = Double.parseDouble(tf.getText().trim());
47
48
49 toServer.writeDouble(radius);
50 toServer.flush();
51
52
53 double area = fromServer.readDouble();
54
55
56 ta.appendText("Radius is " + radius + "\n");
57 ta.appendText("Area received from the server is "
58 + area + '\n');
59 }
60 catch (IOException ex) {
61 System.err.println(ex);
62 }
63 });
64
65 try {
66
67 Socket socket = new Socket("localhost", 8000);
68
69
70
71
72 fromServer = new DataInputStream(socket.getInputStream());
73
74
75 toServer = new DataOutputStream(socket.getOutputStream());
76 }
77 catch (IOException ex) {
78 ta.appendText(ex.toString() + '\n');
79 }
80 }
81
82
86 public static void main(String[] args) {
87 launch(args);
88 }
89 }