Terminating the connection to the Android media server

I have a media proxy that downloads mp4 video from a URL and forwards it to the connected streams. The problem I am facing is that it loads a few bytes from the video, then throws java.net.SocketException: sendto failed: EBADF (Bad file descriptor) and stops (you can see the log below). I tested several links on mp4. I read that the problem may be related to closing the connection, but, as you can see in the logs, the connection is "supported". I know that this may be a problem with the server, but I want to make sure that the client side is doing everything right.

 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 shows:

 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 
+5
source share

All Articles