streaming big files over http with nanohttpd can cause java.net.SocketException - special handling needed

See original GitHub issue

Hi All,

I am new to NanoHttpd. I am using this to stream a video in Android. For just normal playing this works like a charm without any issue. But when I try to seek to different time in the VideoPlayer, immediatly I get the following error and it creates a different socket and resumes to play after some time. But it shows the progress indicator for long time, which I think because of this issue and re-connecting with different socket. And Streaming is from local network server.

java.net.SocketException: sendto failed: EPIPE (Broken pipe)
        at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
        at libcore.io.IoBridge.sendto(IoBridge.java:515)
        at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
        at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
        at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1423)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1396)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1386)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.send(NanoHTTPD.java:1371)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:794)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:195)
        at java.lang.Thread.run(Thread.java:818)

 Caused by: android.system.ErrnoException: sendto failed: EPIPE (Broken pipe)
        at libcore.io.Posix.sendtoBytes(Native Method)
        at libcore.io.Posix.sendto(Posix.java:206)
        at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
        at libcore.io.IoBridge.sendto(IoBridge.java:513)
        at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
        at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
        at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1423)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1396)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1386)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$Response.send(NanoHTTPD.java:1371)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:794)
        at me.etrmntonwheels.surfacetest.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:195)
        at java.lang.Thread.run(Thread.java:818)

I am using serveFile function from SimpleWebServer sample to achieve partial content or streaming. I have read different articles and googled through stackoverflow but still could not figure out the reason.I am stuck at this issue from long time. Please help me to resolve this issue.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:36 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Gibitopcommented, Apr 20, 2018

One more workaround is to add try-catch block to the “sendBody” method, so it closes the connection to the client if the client can’t accept data

private void sendBody(OutputStream outputStream, long pending) throws IOException {
    try {
        long BUFFER_SIZE = 16 * 1024;
        byte[] buff = new byte[(int) BUFFER_SIZE];
        boolean sendEverything = pending == -1;
        while (pending > 0 || sendEverything) {
            long bytesToRead = sendEverything ? BUFFER_SIZE : Math.min(pending, BUFFER_SIZE);
            int read = this.data.read(buff, 0, (int) bytesToRead);
            if (read <= 0) {
                break;
            }
            outputStream.write(buff, 0, read);
            if (!sendEverything) {
                pending -= read;
            }
        }
    } catch (IOException e) {
        // TODO: REMOVE WORKAROUND
        outputStream.close();
    }
}
`
1reaction
proninyaroslavcommented, Nov 11, 2018

@OLEG4120 I used master version of NanoHTTPD (not 2.3.1), and it seems it doesn’t have this problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

streaming big files over http with nanohttpd can cause java.net ...
streaming big files over http with nanohttpd can cause java.net.SocketException - special handling needed.
Read more >
Video streaming using NanoHttpd, Error: java.net ...
I am using Nanohttpd library to run the server in my Android app. I can able to stream and play the video when...
Read more >
Diff - platform/external/nanohttpd - Google Git
SimpleWebServer" + +You should now have a HTTP file server running on <http://localhost:8080/>. ... SocketTimeoutException; +import java.net.
Read more >
NanoHTTPD.java - Android Code Search
import java.net. ... A simple, tiny, nicely embeddable HTTP server in Java ... <li>File server does the 301 redirection trick for directories without ......
Read more >
NanoHTTPD.java example - Javatips.net
Files are added to an internal * list, and deleted when no longer needed (that is, when * <code>clear()</code> is invoked at the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found