views:

1854

answers:

11

When will a TCP packet be fragmented at the application layer? When a TCP packet is sent from an application, will the recipient at the application layer ever receive the packet in two or more packets? If so, what conditions cause the packet to be divided. It seems like a packet won't be fragmented until it reaches the Ethernet (at the network layer) limit of 1500 bytes. But, that fragmentation will be transparent to the recipient at the application layer since the network layer will reassemble the fragments before sending the packet up to the next layer, right?

+1  A: 

If a 3000 byte packet enters an Ethernet network with a default MTU size of 1500 (for ethernet), it will be fragmented into two packets of each 1500 bytes in length. That is the only time I can think of.

Wireshark is your best bet for checking this. I have been using it for a while and am totally impressed

CodeToGlory
So, will the receiving application receive two packets or one at the application layer?
zooropa
That's not what he asked...
dwc
Application layer has no concept of MTU and packet fragmentation. It is a seperate layer with seperate responsibilities from the Network and Transport layer. So shot answer, App layer cannot see two packets.
CodeToGlory
+2  A: 

If a packet exceeds the maximum MTU of a network device it will be broken up into multiple packets. (Note most equipment is set to 1500 bytes, but this is not a necessity.)

The reconstruction of the packet should be entirely transparent to the applications.

Ben S
+1  A: 

Correct - the most informative way to see this is using Wireshark http://www.wireshark.org/ an invaluable tool. Take the time to figure it out - has saved me several times, and gives a good reality check

Jeff
+2  A: 

Different network segments can have different MTU values. In that case fragmentation can occur. For more information see TCP Maximum segment size

This (de)fragmentation happens in the TCP layer. In the application layer there are no more packets. TCP presents a contiguous data stream to the application.

lothar
+3  A: 

At the application layer there are any number of reasons why the whole 1500 bytes may not show up one read. Various factors in the internal operating system and TCP stack may cause the application to get some bytes in one read call, and some in the next. Yes, the TCP stack has to re-assemble the packet before sending it up, but that doesn't mean your app is going to get it all in one shot (it is LIKELY will get it in one read, but it's not GUARANTEED to get it in one read).

TCP tries to guarantee in-order delivery of bytes, with error checking, automatic re-sends, etc happening behind your back. Think of it as a pipe at the app layer and don't get too bogged down in how the stack actually sends it over the network.

Michael Kohne
+6  A: 

It will be split when it hits a network device with a lower MTU then the packets size. Most ethernet devices are 1500, but it can often be smaller, 1492 if that ethernet is going over PPPoE (DSL) because of the extra routing information, even lower if a second layer is added like Windows Internet Connection Sharing. And dialup is normally 576!

In general though you should remember that TCP is not a packet protocol. It uses packets at the lowest level to transmit over IP, but as far as the interface for any TCP stack is concerned, it is a stream protocol and has no requirement to provide you with a 1:1 relationship to the physical packets sent or received (for example most stacks will hold messages until a certain period of time has expired, or there are enough messages to maximize the size of the IP packet for the given MTU)

As an example if you sent two "packets" (call your send function twice), the receiving program might only receive 1 "packet" (the receiving TCP stack might combine them together). If you are implimenting a message type protocol over TCP, you should include a header at the beginning of each message (or some other header/footer mechansim) so that the receiving side can split the TCP stream back into individual messages, either when a message is received in two parts, or when several messages are received as a chunk.

David
+5  A: 

Fragmentation should be transparent to a TCP application. Keep in mind that TCP is a stream protocol: you get a stream of data, not packets! If you are building your application based on the idea of complete data packets then you will have problems unless you add an abstraction layer to assemble whole packets from the stream and then pass the packets up to the application.

dwc
+2  A: 

A the "application layer" a TCP packet (well, segment really; TCP at its own layer doesn't know from packets) is never fragmented, since it doesn't exist. The application layer is where you see the data as a stream of bytes, delivered reliably and in order.

If you're thinking about it otherwise, you're probably approaching something in the wrong way. However, this is not to say that there might not be a layer above this, say, a sequence of messages delivered over this reliable, in-order bytestream.

Curt Sampson
+1  A: 

Does anyone have a more authoritative reference for the fact that TCP is a stream protocol and that data sent in one 'write' call is not guaranteed to be received in one 'read' call?

I understand these facts, but I keep running into clients that don't. They keep telling me to 'send the data all in one packet' - I keep telling them that I have no way of doing that and furthermore, that I shouldn't have to. Some authoritative evidence would be very nice. I read the TCP RFC again but I couldn't find any quotable points on this (admittedly ridiculous) issue.

Any ideas?

See my answer: http://stackoverflow.com/questions/756765/when-will-a-tcp-network-packet-be-fragmented-at-the-application-layer/2678518#2678518
Kevin Panko
+1  A: 

The question makes an assumption that is not true -- TCP does not deliver packets to its endpoints, rather, it sends a stream of bytes (octets). If an application writes two strings into TCP, it may be delivered as one string on the other end; likewise, one string may be delivered as two (or more) strings on the other end.

RFC 793, Section 1.5:

"The TCP is able to transfer a continuous stream of octets in each direction between its users by packaging some number of octets into segments for transmission through the internet system."

The key words being continuous stream of octets (bytes).

RFC 793, Section 2.8:

"There is no necessary relationship between push functions and segment boundaries. The data in any particular segment may be the result of a single SEND call, in whole or part, or of multiple SEND calls."

The entirety of section 2.8 is relevant.

Kevin Panko
+1  A: 

This page is a good source of information about some of the issues that others have brought up, namely the need for data encapsulation on an application protocol by application protocol basis Not quite authoritative in the sense you describe but it has examples and is sourced to some pretty big names in network programming.

Cole