Android媒体代理连接停止

时间:2016-07-10 17:18:12

标签: java android networking proxy

我有一个媒体代理,可以从网址下载mp4视频并将其转发到连接的流。我遇到的问题是它从视频中下载了几个字节,然后抛出java.net.SocketException: sendto failed: EBADF (Bad file descriptor)并停止(您可以看到下面的日志)。我用几个mp4链接进行了测试。我读到该问题可能是由于连接关闭,但正如您在日志中看到的那样,连接是“保持活动”。我知道这可能是服务器问题,但我想确保客户端正确地执行所有操作。

private void processRequestOld(Socket client) throws IllegalStateException, IOException {
            HttpResponse realResponse = download(uri);
            if (realResponse == null) {

            return;
        }

        InputStream data = realResponse.getEntity().getContent();
        StatusLine line = realResponse.getStatusLine();
        HttpResponse response = new BasicHttpResponse(line);
        response.setHeaders(realResponse.getAllHeaders());

        StringBuilder httpString = new StringBuilder();
        httpString.append(response.getStatusLine().toString());

        httpString.append("\n");
        for (Header h : response.getAllHeaders()) {
            Log.d("Headers: ", h.getName() + " " + (h.getValue()));
            httpString.append(h.getName()).append(": ").append(h.getValue()).append(
                    "\n");
        }
        httpString.append("\n");

        try {
            byte[] buffer = httpString.toString().getBytes();
            int readBytes;
            client.getOutputStream().write(buffer, 0, buffer.length);

            byte[] buff = new byte[1024 * 50];
            while (isRunning && (readBytes = data.read(buff, 0, buff.length)) != -1) {
                client.getOutputStream().write(buff, 0, readBytes);
                for (int i = 0; i < number_slaves_os; i++) {
                    if (slaves_output_streams[i] != null)
                        slaves_output_streams[i].write(buff, 0, readBytes);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (data != null) {
                data.close();
            }
            client.close();
            for (int i = 0; i < number_slaves_os; i++) {
                if (slaves_output_streams[i] != null)
                    slaves_output_streams[i].close();
            }
        }
    }

private HttpResponse download(String url) {
    DefaultHttpClient seed = new DefaultHttpClient();
    DefaultHttpClient http = new DefaultHttpClient(seed.getParams());
    HttpGet method = new HttpGet(url);
    HttpResponse response = null;
    try {
        response = http.execute(method);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

Logcat显示:

07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Server nginx/1.4.6 (Ubuntu)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Date Sun, 10 Jul 2016 17:03:01 GMT
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Content-Type video/mp4
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Content-Length 245779
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Last-Modified Tue, 23 Jul 2013 15:04:27 GMT
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Connection keep-alive
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: ETag "51ee9b7b-3c013"
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Expires Sun, 10 Jul 2016 23:03:01 GMT
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Cache-Control max-age=21600
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect D/Headers:: Accept-Ranges bytes
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err: java.net.SocketException: sendto failed: EBADF (Bad file descriptor)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:511)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at com.example.android.wifidirect.MediaProxy.processRequestOld(MediaProxy.java:138)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at com.example.android.wifidirect.MediaProxy.run(MediaProxy.java:98)
07-10 20:02:59.801 25633-25644/com.example.android.wifidirect I/MediaHTTPConnection: response code = 200
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at java.lang.Thread.run(Thread.java:818)
07-10 20:02:59.801 25633-25644/com.example.android.wifidirect V/MediaHTTPConnection: mTotalSize is 245779
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err: Caused by: android.system.ErrnoException: sendto failed: EBADF (Bad file descriptor)
07-10 20:02:59.801 25633-25644/com.example.android.wifidirect V/MediaHTTPConnection: Server doesnt support Partial Request
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.Posix.sendtoBytes(Native Method)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.Posix.sendto(Posix.java:211)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:509)
07-10 20:02:59.801 25633-26003/com.example.android.wifidirect W/System.err:     ... 6 more
07-10 20:02:59.801 25633-25644/com.example.android.wifidirect W/MediaHTTPConnection: readAt 196608 / 32768 => java.net.ProtocolException
07-10 20:02:59.801 25633-25644/com.example.android.wifidirect V/MediaHTTPConnection: Return -EPIPE
07-10 20:02:59.911 25633-25676/com.example.android.wifidirect V/MediaPlayer: message received msg=5, ext1=190, ext2=240

0 个答案:

没有答案