1

I am trying to make a local web server using socket that will stream a video.

Following is my code for server:

class VideoStreamServer {

        public void startServer() {
            outFile = new File(outFilePath);
            Runnable videoStreamTask = new Runnable() {
                @Override
                public void run() {
                    try {
                        ServerSocket socket = new ServerSocket(port);
                        StringBuilder sb = new StringBuilder();
                        sb.append( "HTTP/1.1 200 OK\r\n");
                        sb.append( "Content-Type: audio/mpeg\r\n");
                        sb.append( "Connection: close\r\n" );
                        sb.append( "Accept-Ranges: bytes\r\n" );
                        sb.append( "Content-Length: " + outFile.length() + "\r\n" );
                        sb.append( "\r\n");


                        System.out.println("Waiting for client to connect.");
                        while (true) {
                            Socket client = socket.accept();
                            System.out.println("Thread Started");
                            BufferedOutputStream os = new BufferedOutputStream(client.getOutputStream());
                            FileInputStream in = new FileInputStream(outFile);
                            byte[] data = new byte[1024];
                            int length;
                            //System.setProperty("http.keepAlive", "false");
                            os.write(sb.toString().getBytes());
                            while ((length = in.read(data)) != -1){
                                os.write(data,0,length);
                            }
                            os.flush();
                            os.close();
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            };

            Thread streamServer = new Thread(videoStreamTask);
            streamServer.start();
        }

    }

So the problem is whenever I hit url in web browser following exception is thrown.

java.net.SocketException: sendto failed: EPIPE (Broken pipe)
     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
     at libcore.io.IoBridge.sendto(IoBridge.java:468)
     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:508)
     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:270)
     at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
     at java.io.BufferedOutputStream.write(BufferedOutputStream.java:139)
     at com.example.sachinchandil.myapplication.VideoStreamDownloaderFragment$VideoStreamServer$1.run(VideoStreamDownloaderFragment.java:285)
     at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
     at libcore.io.Posix.sendtoBytes(Native Method)
     at libcore.io.Posix.sendto(Posix.java:156)
     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
     at libcore.io.IoBridge.sendto(IoBridge.java:466)
    ... 7 more

And if i download this file using HttpUrlConnection all bytes frame are download except last and same exception is thrown.

Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • After a client connected you should first read from the inputstream before you write to the outputsream i think. – greenapps Jan 11 '16 at 22:01
  • @Greenapps if u see through the code care fully u can find that too. – Sachin Chandil Jan 12 '16 at 01:49
  • 1
    You read the input stream after you write the header. Client maybe is not ready to read yet (it could be blocked by its first write operation). Also I have experience with client app that tried to open second connection after reading the header from first connection, but first connection wasn't closed immediately (it was MediaPlayer implementation on some specific Android device). Your code is blocked with one connection and doesn't accept another. Probably you will need another thread for communication after you get Socket client, so you can wait for another socket.accept(). – Milos Fec Jan 12 '16 at 09:06
  • `if u see through the code care fully u can find that`. No. You aren't. Why would i otherwise tell you? – greenapps Jan 12 '16 at 09:24
  • sorry for that. Ok so you mean i should create request pool for more than one request? if so how would it help? i mean if second request is initiated then also client will get headers first and so on. same process will continue. please correct me and give your inputs over these queries. – Sachin Chandil Jan 12 '16 at 11:28
  • Request pool? Where are you talking about? Until now you talked about one request. The comments you became were about reading from the inputstream at the wrong moment. Two people mentioned that. – greenapps Jan 12 '16 at 17:58
  • Of course your server should handle every client in its own thread. Otherwise you will block. – greenapps Jan 12 '16 at 18:00
  • This is what i understood from your comment. right now i want to process only one request for a testing purpose later on i will manipulate is as per requirement. I am still stuck here, how would you suggest me to write headers in outputStream to resolve this problem. – Sachin Chandil Jan 12 '16 at 18:06
  • Two people told you to first read from the inputstream. After that do the writing to the outputstream. – greenapps Jan 12 '16 at 18:22
  • may be there is a misunderstanding. i am getting this inputStream from local file not from server. I think you are assuming it coming from server. – Sachin Chandil Jan 12 '16 at 18:32
  • 'client.getOutputStream()'. So you write to the output stream of the socket Now before you do that read all from the inputstream of the socket: client.getInputStream() – greenapps Jan 12 '16 at 21:42
  • No i have not read client's inputstream – Sachin Chandil Jan 13 '16 at 03:07
  • ¿????????????? Yes i know. That's why i said you should. Did you adapt your code already? Tell us the results. Strange that you did not do all already. – greenapps Jan 13 '16 at 09:09
  • Thanks for you replay, It is the first time i am working on Video streaming using Socket so i haven't done it already. What result do i tell you? – Sachin Chandil Jan 13 '16 at 09:16

0 Answers0