title: Java TCP socket program to send and read [x] bytes array. How to loop, sout and sent correctly
body: <p>I'm new with sockets and writing basic Java Client-to-Server-to-Client program that sends in this case
  length of the message <code>outToServer.writeInt(message.length);</code> and <code>byte[] message = new byte[10];</code> array to Server that reads and prints it to console and then modifies few bytes in the array and then sends it back to the Client. Client receives the array, reads it and and prints it to console.</p>
  <p>My problem is that it prints only:</p>
  <pre><code>byte array received from server class: 
  
  0
  0
  0
  0
  0
  0
  0
  0
  0
  0
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  </code></pre>
  <p>when it should print some modified bytes with different numbers.</p>
  <p>I assume there's some problem with for loop or in the reading part of the received bytes. Should i use readFully or something similar and how to do it correctly to loop the values i want?</p>
  <p>Many thanks.</p>
  <p>Client:</p>
  <pre><code>class TCPClient2 {
  
      public static void main(String argv[])
      {
  
  
          try {
              System.out.println(&quot;Client started&quot;);
              Socket clientSocket = new Socket(&quot;localhost&quot;, 8080);
              DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
              System.out.println(&quot;\nByte array printed before sending to server:\n&quot;);
  
              byte[] message = new byte[10];
              //byte[] message = new byte[] { -1, -128, 1, 127, 23, 23, 23, 23, 23, 10 }; // you can try these too
              message[0] = 1; // changes the first byte of the array
  
  
              for(int i = 0; i &lt; message.length; i++) {
                  System.out.println(message[i]);     //prints byte array BEFORE being sent to server
              }
              System.out.println(Arrays.toString(message)); //prints byte array BEFORE being sent to server as String
              System.out.println(&quot;\n&quot;);
  
  
              outToServer.writeInt(message.length); // write length of the message
              outToServer.write(message);           // write the message
              //
              //
              // Message -array is now sent to Server
              //
              // Waiting for answer
              //
  
  
              // Answer is now received from Server
              System.out.println(&quot;byte array received from server class: \n&quot;);
              DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
  
  
              int length = inFromClient.readInt(); // read length of incoming message
                      /*
                      if(length&gt;0) {
                          byte[] message = new byte[length];
                          dIn.readFully(message, 0, message.length); // read the message
                      }
                          */
  
              byte[] messageFromServer = new byte[length];
              for(int i = 0; i &lt; messageFromServer.length; i++) {
                  System.out.println(messageFromServer[i]);//prints received byte array
              }
              System.out.println(Arrays.toString(messageFromServer));//prints received byte array as a string
  
          }catch (Exception e) {
              e.printStackTrace();
          }
  
  
          }
  
      }
  </code></pre>
  <p>Server:</p>
  <pre><code>class TCPServer2 {
  
      public static void main(String argv[])
      {
  
                  try
                  {
                      System.out.println(&quot;waiting for connections&quot;);
                      ServerSocket welcomeSocket = new ServerSocket(8080);
                      Socket connectionSocket = welcomeSocket.accept();
                      System.out.println(&quot;connection established&quot;);
  
                      DataInputStream inFromClient =
                              new DataInputStream(connectionSocket.getInputStream());         //receive byte array from client
  
                      int length = inFromClient.readInt(); // read length of incoming message
                      /*
                      if(length&gt;0) {
                        byte[] message = new byte[length];
                          inFromClient.readFully(message, 0, message.length); // readFully the message, but how?
                      }
                      */
  
                      byte[]message = new byte[length];
                      for(int i = 0; i &lt; message.length; i++) {
                          System.out.println(message[i]); // print array
                      }
  
                      System.out.println(Arrays.toString(message)); // print array as string
                      System.out.println(&quot;message from client printed&quot;);
  
                      message[0]= 1;      //change bytes of the received array
                      message[9]=1;       //change bytes of the received array
  
                      DataOutputStream outToClient =
                              new DataOutputStream(connectionSocket.getOutputStream()); //send byte array with changes back to the client
  
                      outToClient.writeInt(message.length); // write length of the message
                      outToClient.write(message);           // write the message
  
                      System.out.println(&quot;message to client sent&quot;);
  
  
  
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
  
      }
       
  
           
  </code></pre>