title: Javafx server socket - sending string message
body: <p>Im doing the server/client messaging via server socket with javafx.</p>
  
  <p>I have been doing this for a long time now and thought it would be simple enough but I cannot figure this out. I have tried a bunch of different ways but I am just not good enough. Please help me figure it out if you can. </p>
  
  <p>Here is the code for <strong>client</strong></p>
  
  <pre><code>    // IO streams
          DataOutputStream toServer = null;
          DataInputStream fromServer = null;
          String serverMessage = "";
  
    @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();
      Button btnSend = new Button("|&gt;");
      paneForTextField.setPadding(new Insets(5, 5, 5, 5)); 
      paneForTextField.setStyle("-fx-border-color: green");
      paneForTextField.setRight( btnSend );
  
      TextField tf = new TextField();
      tf.setAlignment(Pos.BOTTOM_RIGHT);
      paneForTextField.setCenter(tf);
  
  BorderPane mainPane = new BorderPane();
  // Text area to display contents
  TextArea ta = new TextArea();
  mainPane.setTop(new ScrollPane(ta));
  mainPane.setCenter(paneForTextField);
  
  // Create a scene and place it in the stage
  Scene scene = new Scene(mainPane, 450, 270);
  primaryStage.setTitle("Client"); // Set the stage title
  primaryStage.setScene(scene); // Place the scene in the stage
  primaryStage.show(); // Display the stage
  
  tf.setOnAction(e -&gt; {
    try {
      // Get the message from the text field
      String message = tf.getText().trim();
  
      // Send the message to the server
      toServer.writeBytes(message);
      toServer.flush();
      System.out.println("message sent");
      tf.setText("");
  
      // Display to the text area
      ta.appendText("client: " + message + "\n");
  
  
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
  });
  
  try {
    // Create a socket to connect to the server
    Socket socket = new Socket("localhost", 8000);
    // 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() );
  
  
    new Thread(() -&gt; {
      try{
          while(true){
              serverMessage = fromServer.readUTF();
  
              System.out.println("message received");
  
              Platform.runLater( () -&gt; {
  
                  tf.appendText(serverMessage);
  
              });
  
  
          }
      }
      catch(IOException e){
          ta.appendText(e.toString() + "\n");
      }
  
    }).start();
  }
  catch (IOException ex) {
    ta.appendText(ex.toString() + '\n');
  }
    }
  </code></pre>
  
  <p>and here is the code for <strong>server</strong></p>
  
  <pre><code>private TextField tf = new TextField();
      private ServerSocket serverSocket;
      private Socket socket;
      private DataInputStream input ;
      private BufferedWriter output;
      // Text area for displaying contents
      TextArea ta = new TextArea();
  
      @Override // Override the start method in the Application class
      public void start(Stage primaryStage) {
  
  
           // Create a server socket
          BorderPane borderPaneForText = new BorderPane();
          Button btnSend = new Button("|&gt;");
          btnSend.setOnAction( e-&gt; {
  
              Platform.runLater( () -&gt; {
                  try{
  
                      output.write(tf.getText());
                      showMessage("server: " + tf.getText() + "\n");
  
                  }
                  catch(IOException ex){
                      ex.printStackTrace();
                  }
              });
          });
  
          tf.setAlignment(Pos.BOTTOM_RIGHT);
  
          borderPaneForText.setCenter(tf);
          borderPaneForText.setRight(btnSend);
          borderPaneForText.setPadding( new Insets( 5, 5, 5, 5) );
  
          BorderPane mainPane = new BorderPane();
          mainPane.setTop(new ScrollPane(ta));
          mainPane.setCenter(borderPaneForText);
  
          // Create a scene and place it in the stage
          Scene scene = new Scene(mainPane, 450, 270);
          primaryStage.setTitle("Server"); // Set the stage title
          primaryStage.setScene(scene); // Place the scene in the stage
          primaryStage.show(); // Display the stage
  
          ta.setEditable(false);
          new Thread( () -&gt; {
              try {
                // Create a server socket
                serverSocket = new ServerSocket(8000);
                Platform.runLater(() -&gt;
                  ta.appendText("Server started at " + new Date() + '\n'));
  
                // Listen for a connection request
                Socket socket = serverSocket.accept();
  
                // Create data input and output streams
                input = new DataInputStream( socket.getInputStream() );
                output = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream() ) );
  
                while (true) {
                  // Receive message from the client
                  String message = input.readUTF();
  
                  output.write(message);
  
                  Platform.runLater(() -&gt; {
                    ta.appendText("Client: " + message + "\n"); 
                  });
                }
              }
              catch(IOException ex) {
                ex.printStackTrace();
              }
          }).start();
      }
  
          /**
           * The main method is only needed for the IDE with limited
           * JavaFX support. Not needed for running from the command line.
           */
          public static void main(String[] args) {
            launch(args);
          }
  
  
  
  
    public void showMessage(String message){
  
        Platform.runLater( () -&gt; {
                ta.appendText(message);
  
        });
    }
  </code></pre>
  
  <p>I dont know if it is the problem with the datainputstream because It does not have a readline or readString method. A similar program with double worked when I tried it out.</p>
  
  <p>I am trying to make like a basic chatting window on both applications so that both can exchange messages. when I press send in either of the applications, I expect the string to be sent to the other application. Then I want to display that text in the text area in both the server and client, like how real chat behaves. Currently, the strings are shown in their own respective text areas but not the other application.</p>