0

I`m trying to serve stream with latest (2.3.1) NanoHttpd. Here is code for the server:

public class Server extends NanoHTTPD {
PipedInputStream in;

public void setStream(final PipedInputStream in) {
    this.in = in;
}

public Server() throws IOException {
    super(null, 8888);
}
@Override
public Response serve(IHTTPSession session) {
    Response res = newChunkedResponse(Response.Status.OK, "audio/mpeg", in);
    res.setChunkedTransfer(true);
    return res;
}

Then I`m creating server instance like this:

 new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                final PipedInputStream in = new PipedInputStream(out);


                audioTrack2 = new Server();
                audioTrack2.start();
                audioTrack2.setStream(in);

            } catch (IOException e) {
                Log.e("server", "DOESNT START");
                e.printStackTrace();
            }
        }
    }).start();

where out:

 PipedOutputStream out = new PipedOutputStream();

Then, I have a callback. An In this callback I`m trying to write data to the outStream:

out.write(bytes);

So what I am waiting for: I open my browser on ip-adress:8888 and I can see audio play icon. But can not listen the stream I wrote to the pipe. I event didnt receive that stream because frist time I`m trying to open web-link I am getting this errors stack:

12:32:26.685 ss.serven.rduwan.airtunesandroid E/request #: 0 10-30 12:32:26.780 ss.serven.rduwan.airtunesandroid E/fi.iki.elonen.NanoHTTPD: Could not send response to the client java.net.SocketException: sendto failed: EPIPE (Broken pipe) at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542) at libcore.io.IoBridge.sendto(IoBridge.java:511) at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500) at java.net.PlainSocketImpl.-wrap1(PlainSocketImpl.java) at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266) at java.io.OutputStream.write(OutputStream.java:82) at fi.iki.elonen.NanoHTTPD$Response$ChunkedOutputStream.write(NanoHTTPD.java:1442) at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1694) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1654) at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624) at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957) at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192) 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:211) at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278) at libcore.io.IoBridge.sendto(IoBridge.java:509) at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500)  at java.net.PlainSocketImpl.-wrap1(PlainSocketImpl.java)  at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)  at java.io.OutputStream.write(OutputStream.java:82)  at fi.iki.elonen.NanoHTTPD$Response$ChunkedOutputStream.write(NanoHTTPD.java:1442)  at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1694)  at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667)  at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1654)  at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624)  at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957)  at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)  at java.lang.Thread.run(Thread.java:818)  10-30 12:32:26.798 ss.serven.rduwan.airtunesandroid E/request #: 1 10-30 12:32:26.803 ss.serven.rduwan.airtunesandroid E/fi.iki.elonen.NanoHTTPD: Could not send response to the client java.io.IOException: InputStream is closed at java.io.PipedInputStream.read(PipedInputStream.java:293) at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1690) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1654) at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624) at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957) at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192) at java.lang.Thread.run(Thread.java:818) 10-30 12:32:29.937 ss.serven.rduwan.airtunesandroid E/request #: 2 10-30 12:32:29.939 ss.serven.rduwan.airtunesandroid E/fi.iki.elonen.NanoHTTPD: Could not send response to the client java.io.IOException: InputStream is closed at java.io.PipedInputStream.read(PipedInputStream.java:293) at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1690) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1654) at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624) at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957) at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192) at java.lang.Thread.run(Thread.java:818)

In chromes dev console I can see Id received 135b And then timeout out (with failed) after 5 seconds. Iam sure Im sending a lot of bytes in out.write() so its deffenetly not only 136bytes. To be honest, as for now Iam never trying to close thats piped streams, so data should come to the browser endless. Heres chrome screenshot:enter image description here What Am I doing wrong?

LionisIAm
  • 751
  • 3
  • 17
  • "A pipe is said to be _broken_ if a thread that was providing data bytes to the connected piped output stream is no longer alive". – M. Prokhorov Oct 30 '17 at 13:58
  • So why? If no client connected to the web pipe is writting OK. – LionisIAm Oct 30 '17 at 14:01
  • because thread has stopped, presumably. I also don't understand what do you mean by "if no client connected the pipe is OK". How do you know it's OK if noone's listening? – M. Prokhorov Oct 30 '17 at 14:07
  • Becase Iam writting to it successfully. And after client is connected I can not write to pipe anymore. (IOException) – LionisIAm Oct 30 '17 at 14:09
  • `Then I'm creating server instance like this:` Why are you doing that in the run() of a runnable of a thread? You dont need a thread as far as i know. – greenapps Oct 30 '17 at 14:14
  • I`ve tried with/without separated Threads. Result is the same. Anything else? – LionisIAm Oct 30 '17 at 14:19
  • `res.setChunkedTransfer(true);` Why are you setting that? Try to do without. Why would you use a chunked response at all? – greenapps Oct 30 '17 at 14:22
  • Done. Its not a problem. Tried. – LionisIAm Oct 30 '17 at 14:24

0 Answers0