1

我正在尝试使用将流式传输视频的套接字制作本地 Web 服务器。

以下是我的服务器代码:

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

    }

所以问题是每当我在网络浏览器中点击 url 时抛出异常。

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

如果我使用HttpUrlConnection所有字节帧下载此文件,除了最后一个之外,都会下载相同的异常。

4

0 回答 0