基于服务器的三角形面积计算
效果展示:
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');
}
}
}