title: Android NDK - socket closing when using localhost
body: <p>I'm having issues with using some sockets in C++ code with Android NDK. Two sockets are created, and one reads from the other. This works <em>with</em> a network connection on Android, both <em>with</em> and <em>without</em> a network connection on another machine, but <strong>NOT</strong> on Android without a network connection.</p>
  
  <p>Here is a <em>netstat</em> output before the socket closes. The line of interest is</p>
  
  <pre><code>tcp        0      0 127.0.0.1:32993        127.0.0.1:46810        ESTABLISHED
  
  Proto Recv-Q Send-Q Local Address          Foreign Address        State
  tcp        0      0 0.0.0.0:32993          0.0.0.0:*              LISTEN
  tcp        0      0 0.0.0.0:45549          0.0.0.0:*              LISTEN
  tcp        0      0 127.0.0.1:5037         0.0.0.0:*              LISTEN
  tcp        0      0 0.0.0.0:5555           0.0.0.0:*              LISTEN
  tcp        0      0 127.0.0.1:46811        127.0.0.1:32993        ESTABLISHED
  tcp        0      0 127.0.0.1:32993        127.0.0.1:46811        ESTABLISHED
  tcp        0      0 127.0.0.1:46810        127.0.0.1:32993        ESTABLISHED
  tcp        0      0 127.0.0.1:32993        127.0.0.1:46810        ESTABLISHED
  tcp        0      0 10.0.2.15:5555         10.0.2.2:34217         ESTABLISHED
  udp        0      0 10.0.2.15:49667        10.0.2.3:53           
  udp        0      0 0.0.0.0:60262          0.0.0.0:*      
  </code></pre>
  
  <p>I then attempt to read 10 bytes from the socket 127.0.0.1:46810 (header_length - filled = 10):</p>
  
  <pre><code>bytes = read(fd, header, header_length - filled);
  </code></pre>
  
  <p>However, 0 bytes are read. TCP_NODELAY has been set on the socket. This is the <em>netstat</em> output after the read.</p>
  
  <pre><code>Proto Recv-Q Send-Q Local Address          Foreign Address        State
  tcp        0      0 0.0.0.0:32993          0.0.0.0:*              LISTEN
  tcp        0      0 127.0.0.1:5037         0.0.0.0:*              LISTEN
  tcp        0      0 0.0.0.0:5555           0.0.0.0:*              LISTEN
  tcp        0      0 127.0.0.1:46811        127.0.0.1:32993        FIN_WAIT2
  tcp        0      1 127.0.0.1:32993        127.0.0.1:46811        CLOSE_WAIT
  tcp        0      0 127.0.0.1:46810        127.0.0.1:32993        FIN_WAIT2
  tcp        0      0 127.0.0.1:32993        127.0.0.1:46810        CLOSE_WAIT
  tcp        0      0 10.0.2.15:5555         10.0.2.2:34217         ESTABLISHED
  udp        0      0 0.0.0.0:60262          0.0.0.0:*        
  </code></pre>
  
  <p>There are no problems when there is a network connection, and the addresses used are still 127.0.0.1. Why would it fail to read any bytes on localhost when there is no network connection?</p>