title: howto force socket.close to release write block
body: <p>When i have a file transfer and the device loses internet connection<br>
  the <code>DataOutputStream.write</code> is sometimes blocking almost indefinably. </p>
  
  <p>i know this is kind of normal since the underlying socket layers are doing it's job as fast as it can.   </p>
  
  <p>I was thinking to use the  </p>
  
  <pre><code>registerReceiver(mConnRec,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
  </code></pre>
  
  <p>and catch when this happen and do a <code>socket.close()</code> or <code>DataOutputStream.close()</code><br>
  to force the <code>DataOutputStream.write</code> to stop block and normal code flow would occur<br>
  faster.</p>
  
  <p>Will this work or what can i do to control the blocking?</p>
  
  <p>Another ide i had was to start a thread which job is to every 5 sec<br>
  test internet exist like this</p>
  
  <pre><code>public boolean haveNetworkConn(Context ctx)
  {
      boolean HaveConnectedWifi = false;
      boolean HaveConnectedMobile = false;
  
      ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo[] netInfo = cm.getAllNetworkInfo();
  
      for (NetworkInfo ni : netInfo)
      {
          if (ni.getTypeName().equalsIgnoreCase("WIFI"))
              if (ni.isConnected())
                  HaveConnectedWifi = true;
          if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
              if (ni.isConnected())
                  HaveConnectedMobile = true;
      }
      return HaveConnectedWifi || HaveConnectedMobile;
  } 
  </code></pre>
  
  <p>If that return false i could force a socket.close maybe.</p>
  
  <p>Would really like to have some feedback on this ideas</p>
  
  <p><em>UPDATE</em>
  Running some tests using the registerReceiver BroadcastReceiver.<br>
  Nothing happens when i close the data in and OutputStream and the socket itself.
  It keeps on blocking at <code>out.write(buffer, 0, val);</code>. This was most unexpected.<br>
  using Eclipse and placing breakpoints and stepping..</p>
  
  <pre><code>private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
       String action = intent.getAction();
  
       if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
           return;
       }
  
       boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
       NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);                     
  
       if (!noConnectivity)
       {
           if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
           {
               //Handle connected case
           }
       }
       else
       {
          if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
          {
              //Handle disconnected case
              if(socket != null)
                  try {
                      out.close();
                      in.close();
                      socket.close();
  
                  } catch (IOException e) {
  
                  }
          }
       }
  
  }
  };
  </code></pre>