1 import java.io.*;
2 import java.net.*;
3 import java.util.Date;
4 import javafx.application.Application;
5 import javafx.application.Platform;
6 import javafx.scene.Scene;
7 import javafx.scene.control.ScrollPane;
8 import javafx.scene.control.TextArea;
9 import javafx.stage.Stage;
10
11 public class Server extends Application {
12 @Override
13 public void start(Stage primaryStage) {
14
15 TextArea ta = new TextArea();
16
17
18 Scene scene = new Scene(new ScrollPane(ta), 450, 200);
19 primaryStage.setTitle("Server");
20 primaryStage.setScene(scene);
21 primaryStage.show();
22
23 new Thread( () -> {
24 try {
25
26 ServerSocket serverSocket = new ServerSocket(8000);
27 Platform.runLater(() ->
28 ta.appendText("Server started at " + new Date() + '\n'));
29
30
31 Socket socket = serverSocket.accept();
32
33
34 DataInputStream inputFromClient = new DataInputStream(
35 socket.getInputStream());
36 DataOutputStream outputToClient = new DataOutputStream(
37 socket.getOutputStream());
38
39 while (true) {
40
41 double radius = inputFromClient.readDouble();
42
43
44 double area = radius * radius * Math.PI;
45
46
47 outputToClient.writeDouble(area);
48
49 Platform.runLater(() -> {
50 ta.appendText("Radius received from client: "
51 + radius + '\n');
52 ta.appendText("Area is: " + area + '\n');
53 });
54 }
55 }
56 catch(IOException ex) {
57 ex.printStackTrace();
58 }
59 }).start();
60 }
61
62
66 public static void main(String[] args) {
67 launch(args);
68 }
69 }