We shall now understand the broad level difference between UDP and TCP with a simple example. Suppose three clients want to send some data to a server. Let us first understand how this can be done with the help of UDP. We shall then examine what happens in the case of TCP.
4.12.1 Using UDP for Data Transfer
In the case of UDP, a server is called as iterative. It means that when a server is dealing with UDP requests, it processes only one request at a time. A client prepares a UDP request datagram, encapsulates it inside an IP datagram, and sends it to the server. The server processes the request, forms a UDP response datagram and sends it back to the client. In the meanwhile, if more UDP requests arrive at the server, the server does not pay any attention to them. It completes servicing a UDP request before taking up any other UDP request. In order to ensure that the UDP requests received in the meantime are not lost, the server stores them in a queue of waiting UDP requests, and processes them one after the other. Note that the UDP requests could arrive from the same client, or from different clients. In any case, they are strictly processed one after another in a sequence.
Since UDP is connection-less, this is fine. This is shown in Fig. 4.20.
Fig. 4.20 UDP datagrams are queued
4.12.2 Using TCP for Data Transfer
In contrast to the UDP model, TCP works strictly on the basis of connections. That is a connection (virtual connection) must be first established between a client and a server before they can send data to each other. As
98
a result, if multiple clients attempt to use the same server at the same time, a separate connection is established for each client. Therefore, the server can process many client requests at the same time unlike what happens in UDP. For this reason, when using TCP, a server is said to be in concurrent mode. It means that a server can concurrently serve the requests of multiple clients at the same time, similar to the way a multiprogramming operating system executes many programs at the same time. A connection is established between the server and each client, and each such connection remains open until the entire data stream is processed.
At the implementation level, the concept of parent and child processes is used. That is, when a request for a connection is received from a new client, the server creates a child process, and allocates it to the new client.
The new client and the child server processes then communicate with each other. Thus, there is a separate connection between each client and a server child process. Once a parent server process creates a child process to serve the requests of a particular client, the parent process is free to accept more client requests, and create more child processes, as and when necessary. This is shown in Fig. 4.21.
When the communication between a client and a server is over, the parent process kills that particular server child process.
Fig. 4.21 TCP transmission
SUMMARY
l The Transmission Control Protocol (TCP) is one of the two transport layer protocols—the other is User Datagram Protocol (UDP).
l The main function of TCP is to ensure a correct delivery of packets between the end points.
99
l Since the lower layer protocol (IP) is a connection-less, best-effort delivery mechanism that does not worry about issues such as transmission impairments, loss of data and duplicate data, these features have been built into a higher layer protocol, i.e., TCP.
l The main features offered by TCP are reliability, end-to-end communication and connection management.
l Applications running on different hosts communicate with TCP with the help of a concept called as ports. A port is a 16-bit unique number allocated to a particular application.
l A socket is used to identify the IP address and the port number concatenated together.
l A pair of sockets identifies a unique TCP connection between two applications on two different hosts.
l TCP employs an acknowledgement mechanism to ensure correct delivery.
l At the receiver’s end, TCP reassembles the packets received, sequences them, removes duplicates, if any, and constructs back the original message as was sent by the sender.
l The User Datagram Protocol (UDP) is the simpler of the two protocols in the transport layer. Transmission Control Protocol (TCP) is a sophisticated protocol that provides a number of features for error control, flow control, accurate delivery, end-to-end communication, etc. UDP is a far simpler protocol that does not provide any of these features.
l UDP is a connectionless protocol that does not create a virtual connection between the source and the destination.
l In multimedia transmissions or voice transport, transmission speed is a major concern, more than accurate delivery of the message itself. The change of values of a few data bits is acceptable in such transmissions. UDP is a suitable candidate for such transmissions.
l UDP is never used for transmitting critical data.
REVIEW QUESTIONS
Multiple-choice Questions
1. Transport layer protocols are useful for ensuring delivery.
(a) host-to-host (b) host-to-router
(c) network-to-network (d) end-to-end
2. is reliable delivery mechanism.
(a) IP (b) TCP (c) UDP (d) ARP
3. When a packet is lost in transit, it should be handled by .
(a) sequence control (b) error control
(c) loss control (d) duplication control
4. When a single packet reaches the destination twice, it should be handled by .
(a) sequence control (b) error control
(c) loss control (d) duplication control
5. When packet 2 reaches packet 1 at the destination, it should be handled by .
(a) sequence control (b) error control
(c) loss control (d) duplication control
6. Combination of and makes a socket.
(a) TCP address, IP address (b) IP address, UDP address (c) IP address, port number (d) IP address, physical address
100
7. Well-known ports are generally required .
(a) only on the client (b) on the client and the server (c) on the client but not on the server (d) on the server
8. The client does .
(a) active open (b) passive open
(c) both (a) and (b) (d) none of the above
9. UDP is iterative because .
(a) it processes multiple requests at the same time (b) it performs round-robin checks
(c) it processes only one request at a time (d) it performs parallel processing 10. TCP is concurrent because .
(a) it processes multiple requests at the same time (b) it performs round-robin checks
(c) it processes only one request at a time (d) it performs parallel processing
Detailed Questions
1. Briefly discuss when to use TCP and when to use UDP.
2. Describe the broad-level features of TCP.
3. Discuss the idea of a port.
4. What is the difference between a port and a socket?
5. Discuss the idea of passive open and active open.
6. How does the three-way handshake for creating a TCP connection work?
7. What factors make TCP reliable?
8. What is the purpose of the field sequence number inside a TCP segment header?
9. Describe the main fields of UDP datagram header.
10. What is a persistent TCP connection? When is it useful?
Exercises
1. Transport layer programming using TCP or UDP involves the use of sockets. Learn the basics of socket programming, using either C or Java.
2. Investigate why it is a lot easier to do socket programming in Java as compared to C or other languages.
3. Try opening a socket on a server, and then communicate with that from a client socket.
4. What different things does one have to do in socket programming while using TCP vis-à-vis UDP?
5. What is your idea of persistent connections? Compare that with the persistent connections in a client-server environment.
101