Difference between TCP and UDP

Several Internet socket types are available:

·       Datagram sockets, also known as connectionless sockets, which use User Datagram Protocol (UDP).

·       Stream sockets, also known as connection-oriented sockets, which use Transmission Control Protocol (TCP).

The Transmission Control Protocol (TCP) is connection-oriented and transports a stream of data over a logical connection established between the sender and the receiver. As a result, data sent from a sender to a receiver is guaranteed to be received in the order they were sent.


The Connection-Oriented Sockets are based on the stream-mode I/O model of the UNIX Operating System – data is transferred using the concept of a continuous data stream flowing from a source to a destination. Data is inserted into the stream by a sender process usually called the server and is extracted from the stream by the receiver process usually called the client.     
The server process establishes a connection socket and then listens for connection requests from other processes. Connection requests are accepted one at a time. When the connection request is accepted, a data socket is created using which the server process can write or read from/to the data stream. When the communication session between the two processes is over, the data socket is closed and the server process is free to accept another connection request. Note that the server process is blocked when it is listening or waiting for incoming connection requests. This problem could be alleviated by spawning threads, one for each incoming client connection request and a thread would then individually handle the particular client.


  Program Flow in the Connection Listener (Server) and Connection Requester (Client) Processes


Connection listener(server)
Connection requester (client)


Create   connection   socket   and   listen   for
Create data socket and request a connection
connection request



Accept connection
Get an output stream for writing to the socket


Create data socket for reading from or writing
Write to stream
to socket stream



Get input stream for reading to the socket
Get input stream for reading to the socket


Read from stream
Read from stream


Get an output stream for writing to the socket
Close data socket


Write to stream



Close data socket



Close connection socket