Simulate Server/Client process communication

1. Server side

ServerSocket serverSocket = new ServerSocket(8080);//Create a server socket,
Socket socket = serverSocket.accept();//And wait for connection

2. Client

Socket socket = new Socket("localhost",8080);//Create a socket and send a connection request to the server through the socket

Client code:

public class Client extends Application{
    //Initialize the input and output streams
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        pane.setPadding(new Insets(10,10,10,10));
        pane.setLeft(new Label("Input radius: "));

        TextField tf = new TextField();
        tf.setAlignment(Pos.BOTTOM_LEFT);
        pane.setCenter(tf);

        BorderPane mainPane = new BorderPane();
        TextArea ta = new TextArea();
        mainPane.setCenter(new ScrollPane(ta));
        mainPane.setTop(pane);

        Scene scene = new Scene(mainPane,450,200);
        primaryStage.setTitle("Client");
        primaryStage.setScene(scene);
        primaryStage.show();

        try {
            Socket socket = new Socket("localhost",8080);
            inputStream = new DataInputStream(socket.getInputStream());
            outputStream = new DataOutputStream(socket.getOutputStream());

        }catch (IOException e){
            System.err.println(e);
        }

        tf.setOnAction (e-> {
            try{
                double radius = Double.parseDouble(tf.getText().trim());
                outputStream.writeDouble(radius);
                outputStream.flush();
                double area = inputStream.readDouble();

                ta.appendText("半径: "+radius+"\n");
                ta.appendText("Area obtained from the server: "+area+"\n");
            }catch (IOException ex){
                ta.appendText(ex.toString()+"\n");
            }
        });
    }
}

Server side code:

public class MultiThreadServer extends Application{
    private TextArea ta = new TextArea();
    private int clientNo = 0;
    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(new ScrollPane(ta),450,200);
        primaryStage.setTitle("Server");
        primaryStage.setScene(scene);
        primaryStage.show();

        The new Thread(()->{//ServerSocket accept() method takes time to execute and should not be run in the JavaFX program thread, so it is placed in a separate thread
            try{
                //server socket
                ServerSocket serverSocket = new ServerSocket(8080);
                ta.appendText("The server starts working: "+new Date().toString()+"\n");
                while (true){
                    //waiting for connection
                    Socket socket = serverSocket.accept();
                    clientNo++;

                    Platform.runLater(()->{
                        ta.appendText("The "+clientNo+" client successfully connected: "+new Date().toString()+"\n");

                        InetAddress inetAddress = socket.getInetAddress();
                        ta.appendText("Client "+clientNo+" hostname: "+inetAddress.getHostName()+"\n");
                        ta.appendText("client"+clientNo+"address is:"+inetAddress.getHostAddress()+"\n");
                    });
                    //Create a new thread to handle communication
                    new Thread(new HandleAClient(socket)).start();
                }
            }catch (IOException e){
                e.printStackTrace ();
            }
        }).start();

    }
    class HandleAClient implements Runnable{
        private Socket socket;

        public HandleAClient(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                //Create an input and output stream to accept data from the client
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                while (true){
                    double radius = inputStream.readDouble();
                    double area = radius*radius*Math.PI;
                    outputStream.writeDouble(area);

                    Platform.runLater(()->{
                        ta.appendText("The radius sent by the client: "+radius+"\n");
                        ta.appendText("The area sent by the server: "+area+"\n");
                    });
                }

            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641078&siteId=291194637