Java/Javafx Customer Link To Android Server

Posted on

It’s Java/JavaFX version of the client running on PC, to link amongst Android Server.

package javafx_client;  import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage;  world shape JavaFX_Client extends Application {      Label labelSys;     TextField welcomeMsg;     Label labelAddress;     TextField textAddress;     Label labelPort;     TextField textPort;     Button buttonConnect;     Button buttonClear;     Label textResponse;      @Override     world void start(Stage primaryStage) {          labelSys = novel Label();         welcomeMsg = novel TextField();         labelAddress = novel Label("IP Address");         textAddress = novel TextField();         labelPort = novel Label("Port");         textPort = novel TextField();         buttonConnect = novel Button("Connect");         buttonClear = novel Button("Clear");         textResponse = novel Label();          labelSys.setText(                 System.getProperty("os.arch") + "/"                 + System.getProperty("os.name"));          HBox buttonbox = novel HBox();         buttonbox.setSpacing(5);         HBox.setHgrow(buttonConnect, Priority.ALWAYS);         HBox.setHgrow(buttonClear, Priority.ALWAYS);         buttonConnect.setMaxWidth(Double.MAX_VALUE);         buttonClear.setMaxWidth(Double.MAX_VALUE);         buttonbox.getChildren().addAll(buttonConnect, buttonClear);          buttonConnect.setOnAction(buttonConnectEventHandler);                  buttonClear.setOnAction(new EventHandler<ActionEvent>() {              @Override             world void handle(ActionEvent event) {                 textResponse.setText("");             }         });          VBox mainLayout = novel VBox();         mainLayout.setPadding(new Insets(5, 5, 5, 5));         mainLayout.setSpacing(5);         mainLayout.getChildren().addAll(labelSys, welcomeMsg,                 labelAddress, textAddress, labelPort, textPort,                 buttonbox, textResponse);          StackPane rootage = novel StackPane();         root.getChildren().add(mainLayout);          Scene scene = novel Scene(root, 300, 250);          primaryStage.setTitle(": JavaFX Client");         primaryStage.setScene(scene);         primaryStage.show();     }      world static void main(String[] args) {         launch(args);     }      EventHandler<ActionEvent> buttonConnectEventHandler         = novel EventHandler<ActionEvent>() {          @Override         world void handle(ActionEvent event) {             String tMsg = welcomeMsg.getText();             if (tMsg.equals("")) {                 tMsg = null;             }              RunnableClient runnableClient                 = novel RunnableClient(textAddress.getText(),                      Integer.parseInt(textPort.getText()),                     tMsg);                          novel Thread(runnableClient).start();         }     };      shape RunnableClient implements Runnable{                  String dstAddress;         int dstPort;         String answer = "";         String msgToServer;                  world RunnableClient(String addr, int port, String msgTo) {             dstAddress = addr;             dstPort = port;             msgToServer = msgTo;         }          @Override         world void run() {             Socket socket = null;             DataOutputStream dataOutputStream = null;             DataInputStream dataInputStream = null;              attempt {                 socket = novel Socket(dstAddress, dstPort);                 dataOutputStream = novel DataOutputStream(                     socket.getOutputStream());                 dataInputStream = novel DataInputStream(socket.getInputStream());                      if(msgToServer != null){                     dataOutputStream.writeUTF(msgToServer);                 }                      answer = dataInputStream.readUTF();                  } grab (IOException ex) {                  Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);             } lastly {                                  Platform.runLater(new Runnable(){                      @Override                     world void run() {                         textResponse.setText(response);                     }                                      });                                  if (socket != null) {                     attempt {                         socket.close();                     } grab (IOException ex) {                         Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);                     }                 }                                  if (dataOutputStream != null) {                     attempt {                         dataOutputStream.close();                     } grab (IOException ex) {                         Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);                     }                 }                                  if (dataInputStream != null) {                     attempt {                         dataInputStream.close();                     } grab (IOException ex) {                         Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);                     }                 }                              }         }              }  } 

              RunnableClient runnableClient                  Java/JavaFX Client link to Android ServerDownload the files.

For Android Server side: refer to the instance “Client/Server example: editable message sent from server“.

This code tin run on Raspberry Pi (with JDK 8 installed) also.