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 54 55 56
| package com.company;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.Date;
public class Server extends Application{
public static final int port = 13000;
@Override
public void start(Stage primaryStage) throws IOException {
TextArea ta = new TextArea();
Scene scene = new Scene(new ScrollPane(ta));
primaryStage.setTitle("Serveur");
primaryStage.setScene(scene);
primaryStage.show();
new Thread(()->{
try {
ServerSocket server = new ServerSocket(port);
Platform.runLater(()->{
ta.appendText("server started at "+ new Date()+" \n");
try {
Socket socket = server.accept();
DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
while(true){
double raduis = input.readDouble();
double area = raduis*raduis*Math.PI;
output.writeDouble(area);
Platform.runLater(()-> {
ta.appendText("raduis receive from client : "+raduis+"\n");
ta.appendText("area send to the client "+ area+"\n");
});
}
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
} |