title: write(socket, buff, length) makes crash
body: <p>I have a program that sends data to a mobile phone with android. Let's iamgine</p>
  
  <p>//EDIT</p>
  
  <pre><code>while(enviado &lt; length) {
      printf("SEND\n");
      r = write(con, buff_enviar+enviado, length-enviado);
      printf(" OK\n");
      if (r &gt; 0) enviado += r;
      else if (r == 0) { printf(" * &gt;&gt; Socket cerrado [datos] (%d)\n",r); return r; }
      else if (r == -1) { printf(" * &gt;&gt; Error [datos] (%d)\n",r); return r; }
  }
  </code></pre>
  
  <p>When I'm out of this while, I change <code>buff_enviar</code> values. When I cancel the transmission in my phone, by doing a successful socket.close(), the <code>write</code> makes my program end for no reason. So the output of my program is:</p>
  
  <p><code>SEND
  OK
  SEND
  OK
  SEND</code></p>
  
  <p>I guess it's <code>write</code> fault because it does not printf OK. ERRNO value is 61 (No data available)</p>
  
  <p>I hope someone has encountered this problem before and solved it.</p>
  
  <p>EDIT: Sometimes when I cancel, r = -1 so I can handle the error, but most of the time it just terminates the program. It's like randomly chosen. I'm confused :(</p>
  
  <p>Kind regards,
  Raúl</p>