title: How to properly make client server talk in java?
body: <p><a href="https://pastebin.com/GMKQHXnQ" rel="nofollow noreferrer">https://pastebin.com/GMKQHXnQ</a></p>
  
  <p><a href="https://pastebin.com/tJ51QHrv" rel="nofollow noreferrer">https://pastebin.com/tJ51QHrv</a></p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  import javafx.application.Application;
  import javafx.application.Platform;
  import javafx.geometry.Insets;
  import javafx.geometry.Pos;
  import javafx.scene.Scene;
  import javafx.scene.control.*;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.layout.GridPane;
  import javafx.stage.Stage;
  
  public class Client extends Application {
  
     static DataOutputStream toServer = null;
     static DataInputStream fromServer = null;
     public TextField field;
     public static TextArea area;
     public BorderPane mainPane;
     static Button topLeft;
     static Button topMid;
     static Button topRight;
     static Button midLeft;
     static Button midMid;
     static Button midRight;
     static Button botLeft;
     static Button botMid;
     static Button botRight;
  
     private BorderPane borderPane;
  
     @Override
     public void start(Stage primaryStage) throws Exception {
         // UI
         BorderPane fieldPane = new BorderPane();
         fieldPane.setPadding(new Insets(5, 5, 5, 5));
         fieldPane.setLeft(new Label("Enter text: "));
  
         field = new TextField();
         field.setAlignment(Pos.BOTTOM_RIGHT);
         fieldPane.setCenter(field);
  
         BorderPane mainPane = new BorderPane();
         area = new TextArea();
         area.setEditable(false);
         mainPane.setCenter(new ScrollPane(area));
         mainPane.setTop(fieldPane);
  
         TextArea output = new TextArea();
         output.setWrapText(true);
         GridPane gamePane = new GridPane();
         gamePane.addRow(2);
         gamePane.addColumn(2);
         gamePane.setHgap(250);
         gamePane.setVgap(250);
  
         topLeft = new Button();
         topLeft.setMinSize(60, 60);
         topMid = new Button();
         topMid.setMinSize(60, 60);
         topRight = new Button();
         topRight.setMinSize(60, 60);
         midLeft = new Button();
         midLeft.setMinSize(60, 60);
         midMid = new Button();
         midMid.setMinSize(60, 60);
         midRight = new Button();
         midRight.setMinSize(60, 60);
         botLeft = new Button();
         botLeft.setMinSize(60, 60);
         botMid = new Button();
         botMid.setMinSize(60, 60);
         botRight = new Button();
         botRight.setMinSize(60, 60);
         gamePane.add(topLeft, 0, 0);
         gamePane.add(topMid, 0, 1);
         gamePane.add(topRight, 0, 2);
         gamePane.add(midLeft, 1, 0);
         gamePane.add(midMid, 1, 1);
         gamePane.add(midRight, 1, 2);
         gamePane.add(botLeft, 2, 0);
         gamePane.add(botMid, 2, 1);
         gamePane.add(botRight, 2, 2);
         mainPane.setCenter(gamePane);
         mainPane.setTop(output);
         // Create a serverScene and place it in the stage
         output.setText("Server Started");
  
         Scene scene = new Scene(mainPane, 450, 200);
         primaryStage.setTitle("Client");
         primaryStage.setScene(scene);
         primaryStage.show();
  // END UI
  
  
         try { // start connect
             Socket socket = new Socket("localhost", 8182);
  
             fromServer = new DataInputStream(socket.getInputStream());
             toServer = new DataOutputStream(socket.getOutputStream());
         } catch (Exception e) {
             area.appendText(e.toString() + '\n');
         }
  
         field.setOnAction(e -&gt; {
             try {/*
                 String normal = this.field.getText();
  
                 toServer.writeUTF(normal);
                 toServer.flush();
  
                 String inverse = fromServer.readUTF();
                 area.appendText("Inverse from the server is: " + inverse + '\n');*/
             } catch (Exception e2) {
                 area.appendText(e2.toString() + '\n');
             }
         });
         topLeft.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(1);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 area.appendText(ioException.toString() + '\n');
             }
         });
         topMid.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(2);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         topRight.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(3);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         midLeft.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(4);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         midMid.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(5);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         midRight.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(6);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         botLeft.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(7);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         botMid.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(8);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
         botRight.setOnAction(e -&gt; {
             try {
                 try {
                     sendMove.send(9);
                 } catch (InterruptedException interruptedException) {
                     interruptedException.printStackTrace();
                 }
             } catch (IOException ioException) {
                 ioException.printStackTrace();
             }
         });
     }
  
  
     public static void main(String[] args) {
         launch(args);
     }
  }
  class sendMove{
  
  public static void send(int i) throws IOException, InterruptedException{
         Client.toServer.write(i);
         Client.toServer.flush();
         Platform.runLater(()-&gt;{
  
         int inverse=0;
         try{
         inverse=Client.fromServer.readInt();
         }catch(IOException e){
         e.printStackTrace();
         }
  
         Client.area.appendText(inverse+" recieved back");
         });
         Thread.sleep(100);
         }
  }
  </code></pre>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  
  import javafx.application.Application;
  import javafx.application.Platform;
  import javafx.scene.Scene;
  import javafx.scene.control.ScrollPane;
  import javafx.scene.control.TextArea;
  import javafx.stage.Stage;
  
  public class Server extends Application {
  
      private int clientNo = 0;
      private TextArea ta = new TextArea();
  
      @Override
      public void start (Stage primaryStage) throws Exception {
          ta.setEditable(false);
  
          Scene scene = new Scene(new ScrollPane(ta), 450, 200);
          primaryStage.setScene(scene);
          primaryStage.setTitle("Server");
          primaryStage.show();
  
          new Thread( () -&gt; {
              try {
                  ServerSocket serverSocket = new ServerSocket(8182);
  
                  Platform.runLater( () -&gt; {
                      ta.appendText("Server started time " + new Date() + '\n');
                  });
  
                  while (true) {
                      Socket socket = serverSocket.accept();
                      clientNo++;
  
                      Platform.runLater( () -&gt; {
                          ta.appendText("Starting client " + clientNo + " at " + new Date() + '\n');
                          ta.appendText("Client " + clientNo + " IP Address is " + socket.getInetAddress().getHostAddress() + '\n');
                      });
  
                      new Thread(new ThreadClient(socket)).start();
                  }
              } catch (Exception e) {
                  ta.appendText(e.toString() + '\n');
              }
          }).start();
      }
  
      class ThreadClient implements Runnable {
  
          private Socket socket;
  
          public ThreadClient(Socket socket) {
              this.socket = socket;
          }
  
          @Override
          public void run () {
              try {
                  DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
                  DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
  
                  while (true) {
                      int normal = inputFromClient.readInt();
                      int inverse = normal;
  
                      outputToClient.write(inverse);
  
                      Platform.runLater( () -&gt; {
                          ta.appendText("Text received from client: " + normal + '\n');
                          ta.appendText("Inverse is " + inverse + '\n');
                      });
                  }
              } catch (Exception e) {
                  ta.appendText(e.toString() + '\n');
              }
          }
      }
  
  
      public static void main (String[] args) {
          Application.launch(args);
      }
  }
  </code></pre>
  
  <p>Here are both bits of code</p>
  
  <p>Problem:</p>
  
  <p>Clicking a button freezes client completely, no errors given</p>
  
  <p>General idea:</p>
  
  <p>was to click a button, sends to the server, sever sends back to both clients. Clients use the number to disable corresponding button, add some text and editing and boom, tic tac toe.</p>