TCP vs. UDP
Tags: networking
Published: 2020-03-13 15:09:41

---
title: TCP vs. UDP
published: true
date: 2020-03-11 14:12:00 UTC
tags: Networking
canonical_url: https://coderscat.com/tcp-vs-dup
---

![2020_03_10_tcp-vs-udp.org_20200311_120700.png](https://coderscat.com/wp-content/uploads/2020_03_10_tcp-vs-udp.org_20200311_120700.png)

Figure 1: Image credit [http://ithare.com](http://ithare.com)

`TCP` (Transmission Control Protocol) is a connection-oriented protocol in which the data can be transferred bidirectionally.

`UDP` (User Datagram Protocol) is a connectionless protocol in which data is sent in datagrams.

Let’s talk about the differences between them and how to choose them correctly according to scenarios.

## 1. The differences

### 1.1 Protocol

`TCP` is a connection-oriented protocol, which means before peers need to establish a connection before they begin to exchange data. This procedure is called [TCP way handshake](https://coderscat.com/tcp-3-way-handshake-in-detail).

During the whole process of data transmission, the connection’s state needs to be maintained until the termination. The peers go through a process of TCP 4-way handshake to disconnect the connection.

`UDP` is a datagram oriented protocol. It is much more lightweight than TCP. `UDP` is designed as close as to plain IP as possible. There is no connection during data transmission. Because of this, one peer could send data to multiple targets at one time.

### 1.2 Transmission reliability

`TCP` make checks for errors and try to fix them. TCP is designed to provide a **“reliable stream”**. There is an acknowledgment for every segment transferred. The sender will retry to deliver it if there is any segment without a response. There is a strategy of `flow control` to control the sending speed, make sure the receiver could process received data properly.

`UDP` have a checksum for the data but there is no guarantee of delivery, ordering, or duplicate protection. The application layer track lost packets and to recover from losses.

![2020_03_10_tcp-vs-udp.org_20200311_225123.png](https://coderscat.com/wp-content/uploads/2020_03_10_tcp-vs-udp.org_20200311_225123.png)

Figure 2: Image source [https://microchipdeveloper.com](https://microchipdeveloper.com)

### 1.3 Performance

Because `UDP` does not need to maintain the states of connection, the transmission speed is better than `TCP` ?

The answer depends on whether there are packet losses.

When a packet is lost during the transmission, TCP retransmission timeouts start to introduce delays and make a difference in the time performance.

Since `UDP` ignore losses, many parts of time could be saved.

### 1.4 Header Size

`TCP` header: 20 Bytes, `UDP` header: 8 Bytes.

![2020_03_10_tcp-vs-udp.org_20200311_225205.png](https://coderscat.com/wp-content/uploads/2020_03_10_tcp-vs-udp.org_20200311_225205.png)

Figure 3: Image source [https://microchipdeveloper.com](https://microchipdeveloper.com)

## 2. When to use TCP?

Most networking applications use `TCP`, such as Web browser, Email, FTP. It’s better to choose `TCP` whenever data losses can not be accepted.

## 3. When to use UDP?

In real-world applications, `UDP` is adopted in limited scenarios. It is OK to use `UDP` when application accept some packet loss, reordering, errors or duplication.

Typically, Domain Name System (DNS), Dynamic Host Configuration Protocol (DHCP), voice and video applications use `UDP`.

If you need the reliability, there are some open-source projects(such as [rudp](https://github.com/cloudwu/rudp/blob/master/rudp.c)) provide a reliable transmission on `UDP`.

## Others

In Portable Operating System Interface (POSIX), `TCP` and `UDP` will pass different for function of `socket`.

```
 // int socket(int domain, int type, int protocol)

 // Creating socket file descriptor for TCP
 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { 
     perror("UDP socket creation failed"); 
     exit(EXIT_FAILURE); 
 } 

/////////////////////////////////////////////////////////////

 // Creating socket file descriptor for UDP
 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
     perror("TCP socket creation failed"); 
     exit(EXIT_FAILURE); 
 } 

```

Here are simple demo implementations for [UDP Server-Client](https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/) and [TCP Server-Client](https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/).

## References

[TCP wiki](https://en.wikipedia.org/wiki/Transmission_Control_Protocol)

[UDP wiki](https://en.wikipedia.org/wiki/User_Datagram_Protocol)

[Great TCP-vs-UDP Debate](http://ithare.com/64-network-dos-and-donts-for-game-engines-part-iv-great-tcp-vs-udp-debate/)

The post [TCP vs. UDP](https://coderscat.com/tcp-vs-dup) appeared first on [CodersCat](https://coderscat.com).