服务器程序接收数据,客户端程序发送数据。
客户端将圆半径发送给服务器,服务器将圆面积返回给客户端。
服务器程序
1 package java语言程序设计进阶篇; 2 3 import javafx.application.Application; 4 import javafx.application.Platform; 5 import javafx.scene.Scene; 6 import javafx.scene.control.ScrollPane; 7 import javafx.scene.control.TextArea; 8 import javafx.stage.Stage; 9 import java.io.DataInputStream; 10 import java.io.DataOutputStream; 11 import java.io.IOException; 12 import java.net.ServerSocket; 13 import java.net.Socket; 14 import java.util.Date; 15 16 public class Server extends Application{ 17 @Override 18 public void start(Stage primaryStage){ 19 //界面 20 TextArea ta = new TextArea(); 21 22 Scene scene = new Scene(new ScrollPane(ta),450,200); 23 primaryStage.setTitle("Server"); 24 primaryStage.setScene(scene); 25 primaryStage.show(); 26 //新线程 27 new Thread(() -> { 28 try { 29 ServerSocket serverSocket = new ServerSocket(8000);//新建服务器套接字 30 Platform.runLater(() -> 31 ta.appendText("Server started at " + new Date() + "\n")); 32 33 Socket socket = serverSocket.accept();//对客服机监听连接 34 35 DataInputStream inputFromClient = new DataInputStream( 36 socket.getInputStream()); 37 DataOutputStream outputStream = new DataOutputStream( 38 socket.getOutputStream()); 39 while (true) { 40 double radius = inputFromClient.readDouble(); 41 42 double area = radius * radius * Math.PI; 43 44 outputStream.writeDouble(area); 45 46 Platform.runLater(() -> { 47 ta.appendText("Radius received from client: " + radius + ‘\n‘); 48 ta.appendText("Area is: " + area + ‘\n‘); 49 }); 50 } 51 } 52 catch(IOException ex){ 53 ex.printStackTrace(); 54 } 55 }).start(); 56
客户机程序:
1 package java语言程序设计进阶篇; 2 3 import javafx.application.Application; 4 import javafx.geometry.Insets; 5 import javafx.geometry.Pos; 6 import javafx.scene.Scene; 7 import javafx.scene.control.Label; 8 import javafx.scene.control.ScrollPane; 9 import javafx.scene.control.TextArea; 10 import javafx.scene.control.TextField; 11 import javafx.scene.layout.BorderPane; 12 import javafx.stage.Stage; 13 14 import javax.xml.soap.Text; 15 import java.io.DataInputStream; 16 import java.io.DataOutputStream; 17 import java.io.IOException; 18 import java.net.Socket; 19 20 public class Client extends Application{ 21 //IO streams 22 DataOutputStream toServer = null; 23 DataInputStream fromServer = null; 24 @Override 25 public void start(Stage primaryStage){ 26 BorderPane paneForTextField = new BorderPane(); 27 paneForTextField.setPadding(new Insets(5,5,5,5)); 28 paneForTextField.setStyle("-fx-border-color: green"); 29 paneForTextField.setLeft(new Label("Enter a radius: ")); 30 31 TextField tf = new TextField(); 32 tf.setAlignment(Pos.BOTTOM_RIGHT); 33 paneForTextField.setCenter(tf); 34 35 BorderPane mainPane = new BorderPane(); 36 TextArea ta = new TextArea(); 37 mainPane.setCenter(new ScrollPane(ta)); 38 mainPane.setTop(paneForTextField); 39 40 Scene scene = new Scene(mainPane,450,200); 41 primaryStage.setTitle("Client"); 42 primaryStage.setScene(scene); 43 primaryStage.show(); 44 45 tf.setOnAction(e ->{ 46 try{ 47 double radius = Double.parseDouble(tf.getText().trim()); 48 49 toServer.writeDouble(radius); 50 toServer.flush(); 51 52 double area = fromServer.readDouble(); 53 54 ta.appendText("Radius is " + radius + "\n"); 55 ta.appendText("Area received from the server is " + area + ‘\n‘); 56 } 57 catch(IOException ex){ 58 System.out.println(ex); 59 } 60 }); 61 try{ 62 Socket socket = new Socket("localhost",8000); 63 fromServer = new DataInputStream(socket.getInputStream()); 64 65 toServer = new DataOutputStream(socket.getOutputStream()); 66 } 67 catch (IOException ex){ 68 ta.appendText(ex.toString() + ‘\n‘); 69 } 70 } 71 }