What is the difference between TCP and UDP?
Explain the difference between TCP and UDP
tl;dr
TCP is connection based. UDP is connection-less.
TCP (Transmission Control Protocol)
- TCP is handshake based. a. There must be an established connection between sender and receiver before data can be sent b. 3 way handshake c. TCP is slower to connect because of this handshake
- TCP can handle and recover from packet loss
- TCP is more reliable than UDP
- TCP can increase latency because it will back off if it knows the network is busy
- TCP segments are numbered. You can reliably count on the order of packets received is correct a. You won't get a chunk of a PDF in the middle that should have been at the end
UDP (User Datagram Protocol)
- UDP is a connection-less protocol
- UDP is less "reliable" since it doesn't require an ack(nowledgement) of each segment
- UDP is commonly used for applications that can handle some loss of packets. like streaming audio or video. Use UDP where it's worse if a packet arives late than if it doesn't arrive at all.
- UDP is more concerned with getting the data to you immediately than getting it to you complete. a. e.g. A FPS game you need to know the location of the player is right now, not 2 seconds ago when you lost a packet.
- UDP is also used in DNS queries