sending video to client in http server | Sololearn: Learn to code for FREE!

+6

sending video to client in http server

i built a http server with java(pure java networking package) It takes a long time when the server sends a video to the client.(150kb/s) While I have another app in my device that does this much faster. What's striking is that the browser waits until the whole video is downloaded. why? i see in normal sites unless it. this is my class that petform response. https://code.sololearn.com/cnwCv5mLKxLa/?ref=app

7/8/2021 9:01:29 PM

Mehran

26 Answers

New Answer

+8

Mehran Awesome... That code link gives a few clues to work with. For clarification... the code works and the only issue is the browser doesn't start playing the media until the entire video is downloaded... Is that correct? If so... it appears that the server is loading the entire file and thereby returning the entire video in a single HTTP request / response exchange. The session is then ended with an HTTP STATUS 200 (a.k.a. OK) The scenarios that involves video playback after some of the video has been downloaded involves multiple partial content requests whereby a range of bytes are specified in the HTTP request content headers and the server loads only those bytes to return in the response stream. I believe the headers could specify multipart byte ranges as well. However, you'll need to research the expected behaviors in that scenario. Regardless... the server will also need to send an HTTP STATUS 206. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206 I hope this helps. 👌

+7

Mehran Do you believe that the information you've provided is sufficient for anyone to accurately answer? To say that you've "built a http server with java" without any additional context hurts my brain thinking of all the back and forth dialog we'll have to have just to pull the specific relevant information to begin to entertain answering this question. There are just way too many assumptions to provide a straightforward answer. It might help if we could review the server code and the browser code. Otherwise, any answers you get will be based on some very loose speculation about your code.

+3

Mehran Since you're the author of the code who is having issues, it would be much more efficient for you to provide the necessary context about how your code works between the client and server. Otherwise, we are literally playing guess how you wrote the code by asking 20 questions before we can begin to understand the problem. It's critical that you anticipate all relevant details that are unknown to everyone else to help them help you. In doing so, you might actually discover the issue yourself as you are forcing yourself to think through the important bits.

+3

🤷‍♂️ - Many possible reasons. I'm literally blind without any context.

+2

visph David Carroll Eventually I noticed the reason why the file was slow to read. It seems that reading the file in byte-to-byte is very slow i used a buffer to reading file and it worked well and much faster. see the code again. i updated the code

+1

visph i have another question you may know. when we convert string to bytes non ISO characters convert to two bytes. how machines detect these two bytes are one character(when reading it)?we have this issue in emojies too. because they are four bytes and machines use specific range in utf16 characters known as "sorougate" to detect them.

0

that's the difference between downloading a video and play it, or streaming the video... the former have to wait for full video, while the later start playing as soon as it has enough data buffered ;P

0

visph it's correct. my code: while(! file ended) { read a byte from file write it to response } browser waits for whole video. and then display it

0

http responses are send when responses are complete ^^ you must use another protocol to stream your video ;)

0

so how other servers implement this?

0

I don't know... it's very specific question and you must do your own research on internet ;P

0

I searched, but I didn't find anything.

0

search better ^^ as it is specific, it would not be easy to find ;)

0

visph why do you downvote me?

0

>:)

0

i am opposite to "http response are sent when responding complete" in Java i/o and socket programing if an inputStream writes to the stream another outputStream can read it immediately. i see it in action when i sent image.

0

visph see this https://code.sololearn.com/cOP8mpJDuPTZ/?ref=app

0

that's a too wide subject for some posts in Q&A... search on internet, you should find a lot of ressources explaining character encoding... maybe you could start by this link: https://www.w3.org/International/talks/0505-unicode-intro/

0

visph why says default chrset in java is utf16. this code says unless https://code.sololearn.com/cwD6GCusV889/?ref=app they equal even by reference

0

https://stackoverflow.com/questions/1006276/what-is-the-default-encoding-of-the-jvm