Why is extra data being written to my output stream and how can I stop this?
<p>I am trying to build a simple connection between my Java server (it sends information) and my C client (it receives info).</p>

<p>I want to send one single integer representing the size of an array and then a byte array itself. My java code looks like this</p>

<pre><code>Socket sc = new Socket("52.187.54.73", 19000);

while(true)
{

System.out.println("Socket open");
DataOutputStream outToClient = new DataOutputStream(sc.getOutputStream());

Random rand = new Random(); 
int rlen = rand.nextInt(10)%10 + 1;

 byte[] a = new byte[rlen];
 System.out.println("Size of the array is " + rlen);    


 for(int i = 0; i &lt; rlen; i++)
     a[i] = (byte)rand.nextInt(100);


outToClient.write(rlen);

outToClient.write(a,0,rlen);
//close socket
sc.close(); 
System.out.println("Done!");
//Break once done
break;
}
</code></pre>

<p>and my C code looks like this</p>

<pre><code> ptr1 = (void *)calloc(1, sizeof(int));
 while(1){ 
      sin_size = sizeof(client_addr);
      connected = accept(sock, (struct sockaddr *)&amp;client_addr,&amp;sin_size);
      printf("\n Got a connection from (%s ,%d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

   bytes_received = recv(connected,ptr1,10,0);
   if(bytes_received&lt;0){   
  printf("Something went wrong %s\n", strerror(errno));
   } 

   printf("Bytes received: %d \n", bytes_received);
   for(int i =0; i&lt;bytes_received; i++){

    unsigned char a = *(unsigned char *)(ptr1 + i*sizeof(unsigned char));
    printf("Inc: %d", a);

   }
</code></pre>

<p>Assuming that the above block of code works and I am able to get the value of <code>rlen</code> the code for processing the array is as follows:</p>

<pre><code>   ptr2 = calloc(rlen, sizeof(unsigned char)); 
   bytes_received = recv(connected, ptr2, 1,0);// incoming, 0);

   printf("bytes received: %d\n", bytes_received);

   for(int i = 0; i &lt; rlen; i++){ 
       printf(" i = %d, val = %d\n ", i,*(unsigned char *)(ptr2 + i*sizeof(unsigned char)));
    }
</code></pre>

<p>Now the Java server sends a number between 1 and 10. I was earlier trying to read the number sent using <code>int incoming = *(int *)ptr1;</code> but for some reason, occasionally I would get huge values like <code>51020210</code>.</p>

<p>To check why this was happening and debug I wrote the code block:</p>

<pre><code>for(int i =0; i&lt;bytes_received; i++){

    unsigned char a = *(unsigned char *)(ptr1 + i*sizeof(unsigned char));
    printf("Inc: %d", a);

   }
</code></pre>

<p>It turns out that sometimes, the integer being sent becomes very large.
Why is this happening? On the server side printing out <code>rlen</code> shows me the right value, but non-deterministically I get random really large values at the client side.</p>

<p>Note that whatever number I receive on the client side has the first number as the value of <code>rlen</code>. </p>

<p>So for example if <code>rlen</code> is 6, then the random really large value is something like <code>689932423</code></p>

<p>Later the values of <code>rlen</code> will become very large in the order of 1000s so I need to solve this problem and cannot simply take the first byte of whatever I get on the client side. </p>