Java/Javafx Server Link To Android Client

Posted on

It’s Java/JavaFX version if the server of the Android Client/Server example, run on PC.

package javafx_server;  import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.application.Platform; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage;  world course of pedagogy JavaFX_Server extends Application {      TextField textTitle;     Label labelSys, labelPort, labelIp;     Label labelMsg;     CheckBox optWelcome;      ServerSocket serverSocket;      String message = "";      @Override     world void start(Stage primaryStage) {          textTitle = novel TextField();         labelSys = novel Label();         labelPort = novel Label();         labelIp = novel Label();         labelMsg = novel Label();         optWelcome = novel CheckBox("Send Welcome when connect");         optWelcome.setSelected(true);          labelSys.setText(             System.getProperty("os.arch") + "/"             + System.getProperty("os.name"));         labelIp.setText(getIpAddress());          VBox mainLayout = novel VBox();         mainLayout.setPadding(new Insets(5, 5, 5, 5));         mainLayout.setSpacing(5);         mainLayout.getChildren().addAll(textTitle,             labelSys, labelPort, labelIp,             optWelcome, labelMsg);          StackPane origin = novel StackPane();         root.getChildren().add(mainLayout);          Scene scene = novel Scene(root, 300, 400);          primaryStage.setTitle(": JavaFX Server");         primaryStage.setScene(scene);         primaryStage.show();          Thread socketServerThread = novel Thread(new SocketServerThread());         socketServerThread.setDaemon(true); //terminate the thread when programme terminate         socketServerThread.start();     }      world static void main(String[] args) {         launch(args);     }      soul course of pedagogy SocketServerThread extends Thread {          static in conclusion int SocketServerPORT = 8080;         int count = 0;          @Override         world void run() {             endeavour {                 Socket socket = null;                                  serverSocket = novel ServerSocket(SocketServerPORT);                 Platform.runLater(new Runnable() {                      @Override                     world void run() {                         labelPort.setText("I'm waiting here: "                             + serverSocket.getLocalPort());                     }                 });                  spell (true) {                     socket = serverSocket.accept();                     count++;                                          //Start roughly other thread                      //to foreclose blocked yesteryear empty dataInputStream                     Thread acceptedThread = novel Thread(                         novel ServerSocketAcceptedThread(socket, count));                     acceptedThread.setDaemon(true); //terminate the thread when programme terminate                     acceptedThread.start();                  }             } choose handgrip of (IOException ex) {                 Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);             }                      }      }      soul course of pedagogy ServerSocketAcceptedThread extends Thread {          Socket socket = null;         DataInputStream dataInputStream = null;         DataOutputStream dataOutputStream = null;         int count;          ServerSocketAcceptedThread(Socket s, int c) {             socket = s;             count = c;         }          @Override         world void run() {             endeavour {                 dataInputStream = novel DataInputStream(                     socket.getInputStream());                 dataOutputStream = novel DataOutputStream(                     socket.getOutputStream());                  //If dataInputStream empty,                  //this thread volition last blocked yesteryear readUTF(),                 //but non the others                 String messageFromClient = dataInputStream.readUTF();                                  message += "#" + count + " from " + socket.getInetAddress()                     + ":" + socket.getPort() + "\n"                     + "Msg from client: " + messageFromClient + "\n";                                  Platform.runLater(new Runnable() {                                          @Override                     world void run() {                         labelMsg.setText(message);                     }                 });                                  if (optWelcome.isSelected()) {                                          String msgReply = count + ": " + textTitle.getText();                     dataOutputStream.writeUTF(msgReply);                 }                              } choose handgrip of (IOException ex) {                 Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);             } finally {                 if (socket != null) {                     endeavour {                         socket.close();                     } choose handgrip of (IOException ex) {                         Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);                     }                 }                  if (dataInputStream != null) {                     endeavour {                         dataInputStream.close();                     } choose handgrip of (IOException ex) {                         Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);                     }                 }                  if (dataOutputStream != null) {                     endeavour {                         dataOutputStream.close();                     } choose handgrip of (IOException ex) {                         Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);                     }                 }             }         }      }      soul String getIpAddress() {         String ip = "";         endeavour {             Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface                 .getNetworkInterfaces();             spell (enumNetworkInterfaces.hasMoreElements()) {                 NetworkInterface networkInterface = enumNetworkInterfaces                     .nextElement();                 Enumeration<InetAddress> enumInetAddress = networkInterface                     .getInetAddresses();                 spell (enumInetAddress.hasMoreElements()) {                     InetAddress inetAddress = enumInetAddress.nextElement();                      if (inetAddress.isSiteLocalAddress()) {                         ip += "SiteLocalAddress: "                             + inetAddress.getHostAddress() + "\n";                     }                 }             }         } choose handgrip of (SocketException ex) {             Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);         }           furnish ip;     }  } 

terminate the thread when programme terminate         socketServerThread Java/JavaFX Server link to Android ClientDownload the files.