0

I am using wifi direct to send file between two android phones. sender 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();
                        }
                    }
                }
            }

receiver 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();
            }
        }
    }
}

However, I always get "W/System.err" at the receiver side when running bytestream.copy . 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:

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)
vincentzhou
  • 710
  • 6
  • 18
  • Try this. I hope you got all your answers. http://stackoverflow.com/questions/17031618/android-send-file-between-two-phones-using-socket-and-progress-bar – Danial Hussain Oct 15 '14 at 09:25
  • Hi, I have read through that post but it does not help. My app works sometimes but sometimes get this error. – vincentzhou Oct 15 '14 at 10:29
  • 1
    You set a read timeout, it fired, so you got a read timeout exception. What part of that don't you understand? NB 1 The `isConnected()` test is pointless. It only tells you whether you ever connected this `Socket.` It doesn't tell you about the state of the connection. Any existing `Socket` object should be closed when you're finished with it, regardless of its `isConnected()` state. NB 2 Don't `flush()` inside loops. It destroys the point of buffering. NB 3 You don't need to check the file's existence and create it before calling `new FileOutputStream()`. You're just wasting time and space. – user207421 Oct 16 '14 at 02:49
  • @EJP Thank you for your suggestion. Where did I set the read timeout? Am I able to remove it or increase it? Because the wifi transfer always fails. I have searched for setsotimeout() in my code but found nothing. – vincentzhou Oct 16 '14 at 04:18

0 Answers0