Chapter 6 Transport Protocols (TCP and UDP)

IP delivers data between host computers. The transport protocols deliver data between applications.

Internet applications like ftp and email normally use TCP because it offers reliable byte-stream service. Applications with very little requirements based on the Trivial File Transfer Protocol, use UDP.
Reliable Protocol Connection Checksum Data Service virtual circuits
TCP Y oriented Y byte stream Y
UDP N less Optional datagram N
IP N less Y datagram N

6.1 Ports

A port is like an IP address except that TCP/IP associates a port with a protocol rather than a host computer. In the same way that IP datagrams store source and destination IP addresses, transport protocols store source and destination port numbers.

Each application, regardless of whether it is a server or a client, has a unique port number. Whenever a client sends a message, the transport layer automatically inserts the correct port number within the source port field. When a server program is created, a request can be made to the network for a port number.

The commonly used applications of ftp, telnet, and mail use well-known port assignments, that is, port assignments that are commonly used for specific Internet applications or functions.
protocol well-known port assignment
Echo 7
Daytime 13
ftp 21
telnet 23
simple mail 25
time 37
whois 43
trivial ftp 69
finget 79

UDP port IP is like a mail truck and a transport protocol is like a mail person. The mail truck (IP) carries the mail between post offices (host computers), and the mail person (UDP) sort the letters (data) and place them in the mailboxes (ports). The mail person (UDT) does not notify the receipients (application protocols) that they have mail (dat); he simply deposits the mail in the correct mailbox (port).
TCP Port TCP data delivery focuses on the connection-not the port itself. An application that uses TCP can open multiple connections to the same port and communicate without problems.
TCP is more like telephone communication than postal delivery. A business office (host computer) assignes each employee (application protocol) his own telephone number (port), while leaving many phone numbers (ports) unassigned. When the phone company (IP) routes a call into the office (host computer), the teleohone number (port) of the incoming call determines who answer the call (connection).

Initially, the employee (application protocol) assigned to the number (port) always receives the incoming call (connection). If the employee and the caller agree to talk to each other, a conversation (exchange of data) ensues.

The employee (application protocol) can have another employee (application protocol) pick up the same telephone line (phone0 and share the caller's information (establish another connection using the same port). It can alos transfer the call to ararely used line (port) and continue the exchange of data without interrruption.

6.2 User Datagram Protocol (UDP)

UDP is similar to IP in that both are unreliable, connectionless protocols that use datagrams for data delivery.

 |------------------|------------------|
 | UDP  source port  UDP  destination port|
 |------16- bits------|------16- bits------|
 |UDP  message length |  UDP  checksum    |
 |      16- bits      |      16- bits      |
 |-------------------------------------|
~~|            UDP data area            ~~|
 ---------------------------------------

Unlike the IP checsum, the UDP checksum includes the data area in the calculations. However, theprotocol does not require that the checksum will be calculated. The TCP and IP, on the other hand, require that the calculation will take place.

6.3 Transport Control Protocol (TCP)

More complex than UDP because it provides a reliable, byte-stream, connection-oriented data deliver syervice. It also tries to optimize bandwidth. To optimize network throughput, TCP dynamically controls the flow of data between connections. As such, if the data transfer at the receiving end of the TCP connection starts to overflow, TCP will tell the sending end to reduce transmition speed.

Note TCP is reliable, connection-oriented, byte-based, but it works with IP that is connectionless, unreliable, datagram-based.

Ensuring Reliability

To ensure reliability and byte-streem sequencing, TCP uses acknowledgments. The reciever sends acknowledgement for each message it gets, and the sender resends each message for which it didn't get an acknowledgement in due time.

To improve throughhput, TCP does not send a message and then wait until it receives an acknowledgement before transmitting another. Instead, TCP uses a concept called a sliding window, which lets TCP transmit several messages berore waits for acknowledment. It sends a group of messages, and then add new transmitions when acknowldgements to old ones arrive.

TCP can increase or reduce the width of the window in response to the load oin the internet.

Window sizes are specified in bytes.

6.4 TCP Segments

TCP segment structure:

|-------------------------|-------------------------|
|       source port       |     destination port      |
|---------16-bits--------------------16-bits----------|
|                                                   |
|                  sequence number                   |
|----------------------32-bits-----------------------|
|                   aknowledgment                    |
|                      32-bits                       |
|------|--------|-|-||-||-|-------------------------|
|Hleenagdtehr|Reserved UR AC PSRS SY|FI|      Window  Size        |
|4-bits---6-bits--G-K-HT-NN-|---------16-bits----------|
|                         |                         |
|      TCP16ch-ebciktssum       |      Urge1n6t- pboitisnter      |
|---------------------------------|-----------------|
~                                 |                ~|
~         Options (if any)         |padding (if needed)~
|---------------------------------------------------|
~                 optional data area               ~|
~--------------------------------------------------~|

Source/Destination Ports
Identifications for the appropriate protocols. Each end of a TCP connection can be called a socket.
Sequence Number
Identifies the first byte of data in the data area of the TCP segment.
Acknowledgment Number
Identifies the next byte of data the sender expects from the data stream

Header Length
In units of 4 bytes. Allows to identify the start of the data.

URG
Notifies that the Urgent pointer field points to valid data. The TCP module must process urgent data before processing any other data.
ACK
Tells that the Acknowledgement field holds valid data.
PSH
Requests a push, that is, that the receiver immediately send the data to the destination application without waiting for its buffer to reach some threshold.

Telnet applications normally set this flag. By doing so, telnet forces TCP to immediately pass the user's keyboard inputs to the telnet server. This helps eliminate dalays in echoing the received character back to the sender.

RST
Asks the receiving TCP module to reset the TCP connection. ATCP will send a message with the RST flag when it detects a problem with the connection. Most applications simply terminate when they recieve this flag.
SYN
Asks to synchronize the sequence numbers
FIN
Tells the receiving TCP module that the sender has finished sending data.
Window Size
Tells the receiving TCP module the number of bytes that the sender is willing to accept, i.e., the width of the sliding window.
Urgent Pointer
Points to last byte of urgent data in the TCP data area.
Options

Establishing Connection

To establish a TCP connection, both ends of the connection must negotiate and agree to use packet indentefication information that the other end understands. Likewise, the network must somehow synchronize the receiving end of the TCP connection with the sending end.

Each time a program wants to use TCP to transport data, it transmits a request for a TCP connection to the computer's transport layer. The TCP module in the transport layer, in turn, sends a TCP message with a Synchronization (SYN) flag and a sequence number to the remote port to which the program wants to connect. The SYN flag tells the receiver that a connection is requested, and the sequence number tells how the data will be identified.

The receiver replies with an ACK flag, an Acknowledge number, and a sequence number. The sequence number of the receiver is independent of that of the sender. Both sequence numbers are needed because the TCP connections are full-duplex, that is, data flows both directions at the same time.

The Acknowledgement number is built out of the sequence number of the client. It is the next sequence number that the connection exopects to receive. As such, in its intial reply message, the server side TCP module stores the client side sequence number plus one.

Before it transfer data, the client-side TCP module must acknowledge the initial reply message from the server-side TCP module. The acknowledment is for the server's request for synchronization.

After this three-way handshake, the client and server have their sequence numbers synchronoized. That is, they have all the information they need to identify data in the communication channel.

#########                            #########
#########---------(SYN)-1000---------#########
#########|  (ACK)  1001, (SYN) 2000   #########
##client##----------------------------##server##
#########---------(ACK)-2001---------#########

From then on, the sequence numbers are increased by the size of the data being transmitted. For a transmition of 2000 bytes in four chunks of 500 bytes, we get

#########                            #########
#########---------(SYN)-1000---------#########
#########|  (ACK)  1001, (SYN) 2000   #########
#########----------------------------#########
#########---------(ACK)-2001---------#########
#########                            #########
#########------------1001------------#########
#########|        (ACK)1501          #########
#########----------------------------#########
##client##------------1501------------##server##
#########                            #########
#########---------(ACK)2001----------#########
#########            2001            #########
#########----------------------------#########
#########---------(ACK)2501----------#########
#########                            #########
#########------------2501------------#########
#########|        (ACK)3001          #########
#########----------------------------#########

Closing Connections

Is done using a two-way handshake. To close a connection, a message with the Finish flag FIN on is sent. An acknoledgement means that both sides agree to the closure. That is, to close a connection, we need an active close from the initiator an a passive close from the partner.

Since the communication is two-way in a full-duplex mode, closing a communication in one direction still leaves the one in the other direction openned.