title: android sending files using socket
body: <p>I am using wifi direct to send file between two android phones.
  sender code:</p>
  
  <pre><code>OutputStream outputStream = null;
              InputStream inputStream = null;
              try {
                  outputStream = socket.getOutputStream();
                  inputStream = new FileInputStream(exportTempFile);
                  byte[] buf = new byte[socket.getSendBufferSize()];
              int len = 0;
              while ((len = inputStream.read(buf)) != -1) {
                  outputStream.write(buf, 0, len);
  
              }
                  outputStream.flush();
                  return 0;
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (socket != null) {
                      if (socket.isConnected()) {
                          try {
                              if (outputStream != null) {
                                  outputStream.close();
                              }
                              if (inputStream != null) {
                                  inputStream.close();
                              }
                              socket.close();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      }
                  }
              }
  </code></pre>
  
  <p>receiver code:</p>
  
  <pre><code>OutputStream outputStream = null;
  InputStream inputStream = null;
  try {
      tempDir = new File(context.getCacheDir(), "temp");
      if (!tempDir.exists()) {
          tempDir.mkdir();
      }
      File file = new File(tempDir, "temp.zip");
      file.createNewFile();
      outputStream = new FileOutputStream(file);
      inputStream = socket.getInputStream();
  
      byte[] buf = new byte[socket.getReceiveBufferSize()];
              int len = 0;
              while ((len = inputStream.read(buf)) != -1) {
                  outputStream.write(buf, 0, len);
                  outputStream.flush();
              }
  
      return file;
  } catch (IOException e) {
      e.printStackTrace();
  } finally {
      if (socket != null) {
          if (socket.isConnected()) {
              try {
                  if (outputStream != null) {
                      outputStream.close();
                  }
                  if (inputStream != null) {
                      inputStream.close();
                  }
                  socket.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>However, I always get "W/System.err" at the receiver side when running <code>bytestream.copy</code> . I wonder if I did something wrong. Did I close socket too early so the other side can not get data?
  error is as below:</p>
  
  <pre><code>10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at java.io.InputStream.read(InputStream.java:163)
  10-15 17:13:50.162  13921-14306/cc.megaman.led W/System.err﹕ at cc.megaman.led.service.ImportManager$ImportTask.doInBackground(ImportManager.java:420)
  10-15 17:13:50.172  13921-14306/cc.megaman.led W/System.err﹕ at cc.megaman.led.service.ImportManager$ImportTask.doInBackground(ImportManager.java:377)
  10-15 17:13:50.172  13921-14306/cc.megaman.led W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
  10-15 17:13:50.172  13921-14306/cc.megaman.led W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  10-15 17:13:50.172  13921-14306/cc.megaman.led W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  10-15 17:13:50.172  13921-14306/cc.megaman.led W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:140)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
  10-15 17:13:50.182  13921-14306/cc.megaman.led W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
  </code></pre>