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 MultiThreadServer extends Application {
12
13 private TextArea ta = new TextArea();
14
15
16 private int clientNo = 0;
17
18 @Override
19 public void start(Stage primaryStage) {
20
21 Scene scene = new Scene(new ScrollPane(ta), 450, 200);
22 primaryStage.setTitle("MultiThreadServer");
23 primaryStage.setScene(scene);
24 primaryStage.show();
25
26 new Thread( () -> {
27 try {
28
29 ServerSocket serverSocket = new ServerSocket(8000);
30 ta.appendText("MultiThreadServer started at "
31 + new Date() + '\n');
32
33 while (true) {
34
35 Socket socket = serverSocket.accept();
36
37
38 clientNo++;
39
40 Platform.runLater( () -> {
41
42 ta.appendText("Starting thread for client " + clientNo +
43 " at " + new Date() + '\n');
44
45
46 InetAddress inetAddress = socket.getInetAddress();
47 ta.appendText("Client " + clientNo + "'s host name is "
48 + inetAddress.getHostName() + "\n");
49 ta.appendText("Client " + clientNo + "'s IP Address is "
50 + inetAddress.getHostAddress() + "\n");
51 });
52
53
54 new Thread(new HandleAClient(socket)).start();
55 }
56 }
57 catch(IOException ex) {
58 System.err.println(ex);
59 }
60 }).start();
61 }
62
63
64 class HandleAClient implements Runnable {
65 private Socket socket;
66
67
68 public HandleAClient(Socket socket) {
69 this.socket = socket;
70 }
71
72
73 public void run() {
74 try {
75
76 DataInputStream inputFromClient = new DataInputStream(
77 socket.getInputStream());
78 DataOutputStream outputToClient = new DataOutputStream(
79 socket.getOutputStream());
80
81
82 while (true) {
83
84 double radius = inputFromClient.readDouble();
85
86
87 double area = radius * radius * Math.PI;
88
89
90 outputToClient.writeDouble(area);
91
92 Platform.runLater(() -> {
93 ta.appendText("radius received from client: " +
94 radius + '\n');
95 ta.appendText("Area found: " + area + '\n');
96 });
97 }
98 }
99 catch(IOException ex) {
100 ex.printStackTrace();
101 }
102 }
103 }
104
105
109 public static void main(String[] args) {
110 launch(args);
111 }
112 }