Revisit: Comparison between UDP and TCP

It has been a long time since I learned UDP and TCP. So I recently revisited the concepts and try to summarize the difference between these two protocol in short.

  • UDP(User Datagram Protocol): UDP is a connectionless protocol. It never provides packet segmentation, retransmission and order fix functionality. It is faster as error recovery is not attempted. UDP only provides one single error checking mechanism which is used for checksums. The small size(8 bytes) of header is another guarantee of the efficiency.

  • TCP(Transmission Control Protocol): TCP is a connection-oriented protocol. It reads data as streams of bytes. It provides packet segmentation, re-order and retransmission functionality. The header size 20 bytes. Plus with extensive error checking and error recovery of TCP, it has slower speed in communication.

Three way handshake of TCP

To establish a connection between server and client, TCP requires three way handshake.

  • First way handshake: Client sends SYN(Synchronize Sequence Number) packet to Server for requesting a connection and then enters SYN-SEND status

  • Second way handshake: Server end agrees the connection and send back SYN + ACK packet to Client. Now Server enters SYN-RECEIVED status

  • Third way handshake: Client confirms the connection and sends back ACK packet to Server then enters ESTABLISHED status. After Server received ACK packet, Server enters ESTABLISHED status.

There are four things Server and Client has to confirm when they try to create a connection with each other.

  1. Server need to confirm it can receive packet from Client
  2. Client need to confirm it can receive packet from Server
  3. Client need to confirm Server can receive packet from the Client itself
  4. Server need to confirm Client can receive packet from the Server itself

The first way handshake makes sure the first thing gets confirmed. The second way handshake ensures the second and third thing gets confirmed. Finally the third way handshake guarantees the forth thing. That’s the reason why TCP need three way handshake to establish the bi-direction connection.

Four way handshake of TCP

When TCP tries to terminate a connection, a four way handshake has been performed. Server and Client need to send FIN(FINISH) and ACK to each other.

  • Client —- FIN –» Server
  • Server —- ACK –» Client
  • Server —- FIN –» Client
  • Client —- ACK –» Server

Although the 2 and 3 can merge technically. But actually the Client is under a FIN_WAIT status for waiting the FIN packet from Server. But the Server may not send the FIN packet immediately after sending the ACK packet to Client. Sometimes the receiving buffer is not fully handled yet, so it is not appropriate to merge 2 and 3 together.

comments powered by Disqus