基于服务器的三角形面积计算

效果展示:



Server

public class Main extends Application {

    @Override // Override the start method in the Application class

    public void start(Stage primaryStage) {

        // Text area for displaying contents

        TextArea ta = new TextArea();

 

        // Create a scene and place it in the stage

        Scene scene = new Scene(new ScrollPane(ta), 450, 200);

        primaryStage.setTitle("Server"); // Set the stage title

        primaryStage.setScene(scene); // Place the scene in the stage

        primaryStage.show(); // Display the stage

 

        new Thread(() -> {

            try {

                // Create a server socket

                ServerSocket serverSocket = new ServerSocket(8001);

                Platform.runLater(() ->

                        ta.appendText("Server started at " + new Date() + '\n'));

 

                // Listen for a connection request

                Socket socket = serverSocket.accept();

 

                // Create data input and output streams

                DataInputStream inputFromClient = new DataInputStream(

                        socket.getInputStream());

                DataOutputStream outputToClient = new DataOutputStream(

                        socket.getOutputStream());

 

                while (true) {

                    // Receive high from the client

                    int high = inputFromClient.readInt();

                    int base = inputFromClient.readInt();

                    // Compute area

                    double area = high * base/2.0;

 

                    // Send area back to the client

                    outputToClient.writeDouble(area);

 

                    Platform.runLater(() -> {

                        ta.appendText("High received from client: " + high + '\n');

                        ta.appendText("Base received from client: "+base+'\n');

                        ta.appendText("Area is: " + area + '\n');

                    });

                }

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }).start();

    }

}

 

Client:

public class Main extends Application {

    DataOutputStream toServer = null;//java基本数据类型写入数据输出流中

    DataInputStream fromServer = null;

 

    @Override // Override the start method in the Application class

    public void start(Stage primaryStage) {

        // Panel p to hold the label and text field

        BorderPane paneForTextField = new BorderPane();

        paneForTextField.setPadding(new Insets(5, 5, 5, 5));

        paneForTextField.setStyle("-fx-border-color: green");

 

        TextField th = new TextField("");

        Label labelH=new Label("High:");

        HBox hb_h=new HBox();

        hb_h.getChildren().addAll(labelH,th);

 

        TextField tb = new TextField("");

        Label labelB=new Label("Base:");

        HBox hb_b=new HBox();

        hb_b.getChildren().addAll(labelB,tb);

 

        Button ok=new Button("OK");

 

        paneForTextField.setTop(hb_h);

        paneForTextField.setCenter(hb_b);

        paneForTextField.setBottom(ok);

 

        BorderPane mainPane = new BorderPane();

        // Text area to display contents

        TextArea ta = new TextArea();

        mainPane.setCenter(new ScrollPane(ta));

        mainPane.setTop(paneForTextField);

 

        // Create a scene and place it in the stage

        Scene scene = new Scene(mainPane, 450, 200);

        primaryStage.setTitle("Client"); // Set the stage title

        primaryStage.setScene(scene); // Place the scene in the stage

        primaryStage.show(); // Display the stage

 

        ok.setOnAction(e->{

            try{

                int high=Integer.parseInt(th.getText().trim());

                int base=Integer.parseInt(tb.getText().trim());

                toServer.writeInt(high);

                toServer.writeInt(base);

                toServer.flush();

                double area = fromServer.readDouble();

                ta.appendText("输入的高是:" + high +"  底是:"+base+"\n");

                ta.appendText("从服务器上得到的三角形的面积是 " + area + '\n');

 

            }catch (IOException ex){

                System.err.println(ex);

            }

        });

 

        try {

            // Create a socket to connect to the server

            Socket socket = new Socket("localhost", 8001);

            // Socket socket = new Socket("130.254.204.36", 8000);

            // Socket socket = new Socket("drake.Armstrong.edu", 8000);

 

            // Create an input stream to receive data from the server

            fromServer = new DataInputStream(socket.getInputStream());

 

            // Create an output stream to send data to the server

            toServer = new DataOutputStream(socket.getOutputStream());

        } catch (IOException ex) {

            ta.appendText(ex.toString() + '\n');

        }

    }

}

 


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值