views:

49

answers:

2

Hi,

When an application such as a web server sends HTTP data to a web browser, how does the browser know when it has received all of the data so that it can begin using it instead of waiting for more? TCP doesn't specify anywhere how large a segmented message is going to be.

Right now I'm thinking that it's up to the application layer, like HTTP's Content-Length header. But it seems like even that header could be split off into a 2nd or 3rd packet.

+1  A: 

TCP/IP is a connection oriented protocol. So, when the browser performs a HTTP connection using TCP/IP, the Network stack guarantees that stream will arrive in the same order the sender intended to.

So, there is no packet concept when you are dealing with TCP. TCP is an ordered stream of bytes arriving through a socket. No need to worry about packets at all. That's the beauty of a protocol stack: each layer does its own work, and abstracts the layer above it of the underlying complications of the problems it resolves.

Pablo Santa Cruz
TCP has the notion of a "frame" rather than a packet. Specific frames (composing a multitude of IP packets) are delivered complete.
Michael Mullany
Right, TCP was designed to transfer data as a stream. But from the application's point of view, I don't see any way it would be able to distinguish two files, for example, sent in the same TCP session, unless the application protocol (like HTTP) takes care of that like bmargulies says below.
Caleb Hearon
@Michael Mullaney: TCP has the notion of a *'segment',* not a packet, but there's no support for it in the API. The application just sees a byte stream.
EJP
Ah, got my terminology wrong, it's been too long since I have been doing networking!
Michael Mullany
A: 

Content-length indeed, except in the case where the client reads until it gets an end-of-file indication due to the other end closing the connection. Of course, in HTTP, 'RSVP', so that's not going to happen.

Absent content length, it's gotta look for </html> or some other delimiter in the content. The browser doesn't see packets at all. The connection looks like a stream, with no boundaries, and it's up to the two ends to make a protocol.

bmargulies
HTTP and HTML are distinct. HTTP is used for many many things besides HTML. Particularly assets like CSS and images but also web services and AJAX/JSON data.
Sam
Yes but there must be a way to determine the end of the file since TCP arrives as a stream with (at least as far as I know based on the answers so far) no way to determine when it ends. In HTML's case, it would be by looking for </html>.
Caleb Hearon
Upvoted pointless quibbling downvote. Answer is correct.
EJP
Yeah, I think sam misunderstood, this is the best answer. Also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
Caleb Hearon