TCP vs UDP
On the internet, every network packet follow this five layer structure:
- Transport layer provides one network connection to allow multiple application simultaneously. Much like street names have house numbers, transport layer creates 65000 ports in your computer per network connection.
- These ports can be reserved and used by applications on your computer.
- One application can use multiple ports at the same time if it wants.
Purpose of the transport layer:
For example, an application reserved port 12437 to send a message to port 80 on some other machine. Whenever the application layer creates a message, it is passed on the Transport layer. In this layer we wrap the message in a segment. This segment contains some additional information like the source port at well as destination port.
When the segment is created, it is passed on to the Network layer for further processing.
- Our segment will show up on the reciever side, when it is passed on from the network layer to the transport layer.
- The segment will be examined to determine the destination port.
- Then the message is unwrapped and delivered to port 80.
This is the foundational basis of transport layer.
But there is more to it than that.
There are two major transport protocols:
- small packet sizes than TCP by about 60%
- UDP header 20 bytes
- TCP header 80 bytes
- Connectionless: No connection to create and maintain
- You dont have to create connection first before sending out data
- You have more control of when data is being sent out
- Data corruption is a common occurance on the Internet, UDP has a primitive form of error detection.
- No compensation for lost packets
- Packets can arrive out of order
- No congestion control
Conclusion: UDP may be light weight, but not that reliable.
Transmission Control Protocol
- Since TCP is connection based, we have to first negotiate a connection before we can do anything. This procedure is known as a three-way handshake.
- First, the initiator will ask the acceptor if it wants to set up a connection.
- The acceptor will reply to the request and when the initiator recieves this reply, it will send a packet to the acceptor that acknowledges that the connection is now established.
The similar procedure takes place when a connection is closed.
- When a sender doesnt get an acknowlegement after a certain period of time, it will assume that the packet got lost on its way. So, it will send it again-.
2. In-order delivery
- Because segments are ordered in TCP, it implements in-order delivery.
- Although packets may come out of order, TCP rearranges them before sending them to application.
3. Congestion Control
- Delays transmission when the network is congested
4. Error Detection
- Bigger header
- Data doesnt always sent out immeditaley
- side effect of congestion control
- Hence real time conversation will not feel real time as you wanted to be
- As you can see, congestion control can be a nuisance or an enhancemewnt
- Bigger overhead
- Retransmission of packets, acknowledgement of packets
- For example, to send HD video, you need lots of bandwidth. But if you are using TCP, you need more becaue of retransmission and acknowledgement. Hence, in this case it makes sense to use UDP than TCP.