The important issues in the transport layer are:
In the Internet world, this is handled by ports, which are simply integer numbers representing a location in a map. This map tells the transport layer what to do with communications that are being sent through a port in either direction. Any process desiring to communicate to processes on other nodes finds an available unused port and tells the transport layer (through the operating system) where to send outgoing or incoming data. In the Internet model, ports are integers from 0 to 65,535.
The unreliable datagram service in the Internet protocol stack is called Universal Datagram Protocol or UDP. This is an unreliable simplex protocol that simply drops the datagram to the network layer and forgets it. While ICMP does provide some measure of reliability, UDP is under no obligation to provide much of anything.
The packet format of UDP is:
Notice that UDP has to handle the multiplexing problem; what process on this host should get the incoming message. The answer is whichever process currently has opened the destination port for UDP data services. The Checksum is the same checksum that IP uses with the addition of the first four bytes of the UDP header. This is a little strange and seems contrary to our protocol stack philosophy that different layers are independent. Here, the designers of UDP wanted to be able to double check and insure that the message was delivered to the correct host. Finally, the length is the number of data bytes (not including header) in the message.
Reliable stream service in the Internet protocol stack is provided with the Transmission Control Protocol or TCP. The packet format for TCP is:
As with UCP, TCP has to handle the multiplexing problem and it uses the port concept as well. Based on the next two fields, Sequence Number and Acknowledgement Number, you might be suspicious that TCP is a sliding window protocol, which it is. TCP has options, so the Header Length is needed to know where the data starts. Advertised Window Size is used in controlling the sliding window protocol. The Checksum is exactly as in UDP and the Urgent Pointer indicates if there is urgent data in the packet and where it is located.
The TCP flags are:
The SYN and FIN flags are used to open and close a connection respectively. RESET can be used to reset the parameters of a connection is one end feels that it is confused by the current state. The Push flag is used to indicate that the packet is being sent due to orders by the controlling upper layer and will be discussed later. The URGENT point flag is used to indicate that the data contains data that requires immediate attention and will be discussed later. Finally, the ACK flag indicates that the value in the Acknowledgement Number field is meaningful.
So there are a few things that need to be explained, but first, lets see how TCP implements a sliding window protocol.
The application sends its messages, which are then buffered by TCP as a continuous stream of bytes. TCP then decides according to its sliding window policies to send data with certain segment sizes. In this case, it sent a two-byte segment, then a three-byte, then two five-byte segments and finally a two-byte segment. It has gotten acknowledgements for the first
two, and it has three bytes in the buffer that haven't been sent. While this may seem confusing, it is a very efficient implementation of a sliding window protocol and allows TCP to have very good control over the data it is managing. It only requires one buffer, which defines the maximum window size, but the window size itself can be adjusted dynamically via the Advertised Window Size field.
One the receiving end, Next Byte Expected and the Advertised Window Size specify an amount of information that can be received and stored while waiting for Next Byte Expected to advance, which only happens when a segment arrives that includes the next byte expected. TCP acknowledges data as it is received, and not after it is sent up to the higher layer.
So how does TCP decide what to send? It depends on the window size and the amount of unack'ed data that has been sent, and on a couple of other things. If enough data accumulates in the buffer, it will send a segment. The Maximum Segment Size is a quantity that limits the maximum amount of data that TCP can send at any one time. This is a value that is set when connection is established and can be up to 65,535, but is by default 536 bytes. If MSS bytes are in the buffer, and the receiver has a large enough window size, it will send a segment. TCP also doesn't like its connections to be idle for long, so if no data is sent for a period described by the inactivity timer, it will send any data in the buffer, or an empty packet if there is nothing available. If it receives a packet with an acknowledgement from the other end, it will send any data available in the buffer. Finally, if a message is delivered with the PUSH flag set, it sends everything in the buffer (possibly more than one segment).
The algorithm for the sending side is:
Similarly, the receiver algorithm is:
An interesting thing can happen with the TCP sliding window implementation. In the previous example, suppose that the segment containing "CDE" is lost. Eventually the timer for that segment expires and is resent, but TCP doesn't keep a record of segment boundaries. So what gets sent might be "CDEFG". At the receiving end it doesn't matter, because it will store the data and advance Next Byte Expected, completely ignoring that some of the data might be duplicated.
Due to the potential for delayed packet problems, TCP connections don't start the sequence numbers at zero. Instead, they generate a random number to use, and both sides generate their own.
The objective of the three way handshake is to insure that you don't inadvertently create a connection that should not exist. Remember that delayed packets can show up and inopportune times, so this procedure tries to make the likelihood that a delayed "connect" packet doesn't create a connection and also that the two endpoints are synchronized with regard to their starting sequence numbers.
There is also a three-way handshake for termination:
The purpose for this procedure is to insure that both endspoints have the opportunity to deliver any data in their buffer and to receive any data that might be in transit. Since either end can withhold an ACK for as long as needed, this will guarantee a proper shutdown of the connection. For those of you that write socket-layer stream programs, this is why the shutdown command should be used with an appropriate parameter specifying the intentions of the active side.
Most of this diagram is concerned with connecting and disconnecting, and the entire sliding window management process is encapsulated in the Established state. It would be possible to produce a state-transision diagram for this part of the process also. Compare the three-way handshake and three-way shutdown processes to the diagram and identify the critical parts.