title: Comunication tcp server c/ client java
body: <p>i have some trouble with comunication between a c server and a java client.</p>
  <p><em><strong>How i recive in c</strong></em></p>
  <pre><code>void *connection_handler(void* args){
      int sock = *(int*)args;
      char op_code [sizeof(uint8_t)];
      char len [sizeof(uint8_t)];
  
      while(true){
          if(read(sock,&amp;op_code,(sizeof(uint8_t)))==0 || atoi(op_code)==0)
            {printf(&quot;Error reading op_code\n&quot;);break;}
  
          if(read(sock,&amp;len,(sizeof(uint8_t)))==0 || atoi(len)==0)
            {printf(&quot;Error reading len\n&quot;);break;}
  
          printf(&quot;op code-&gt;%d\n&quot;,atoi(op_code));
          printf(&quot;len -&gt;%d\n&quot;,atoi(len));
      }
  
      printf(&quot;Client is now disconnected\n&quot;);
      close(sock);
      free((int*)args);
      pthread_exit(0);
  }
  </code></pre>
  <p><em><strong>How i connect to server and send data in java</strong></em></p>
  <pre><code>
         private void connectToServer(){
          try {
              SocketAddress sockaddr =
                      new InetSocketAddress(MainActivity.SERVER_IP, MainActivity.SERVER_PORT);
              socket = new Socket();
              socket.connect(sockaddr, 3000);
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              Log.d(TAG, &quot;doInBackground: connesso&quot;);
  
          }
          catch (SocketTimeoutException te) {
              Log.d(TAG, &quot;doInBackground: non connesso, time out exeption&quot; + te);
              sendDataToActivity(MainActivity.ERROR, MainActivity.TIME_OUT_EXEPTION);
              te.printStackTrace();
          }
          catch (IOException e) {
              Log.d(TAG, &quot;doInBackground: non connesso&quot; + e);
              e.printStackTrace();
          }
      }
  
      /**
       * Send message to server.
       * @param username
       * @param psw
       */
      public void sendSignIn (String username,String psw){
          new Thread(){
              public void run(){
                  //Invio il command_code e la lunghezza del messaggio al server
  
                  try {
                      dataOutputStream.writeBytes(&quot;1&quot;);
                     // dataOutputStream.close();
                      dataOutputStream.writeBytes(&quot;2&quot;);
                      //dataOutputStream.writeBytes(username);
  dataOutputStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }.start();
      }
  
  </code></pre>
  <p><em><strong>Output of this code</strong></em></p>
  <pre><code>op_code -&gt; 12 (Should be 1 but can't understand why my read read both)
  len -&gt; 2
  </code></pre>
  <p>Probably the problem it's caused by the size of op_code and len but honestly i can't understand how fix it.
  Can some one illuminate me pls?</p>