What are the main differences between TCP/UDP?
- UDP has no connection establishment. TCP has three way handshaking (send SYN, receive SYN-ACK, send ACK), while UDP has no connection establishment, hence called connectionless transport protocol
- TCP keeps Connection state at the end systems, UDP doesn't. The This connection state includes receive and send buffers, congestion control parameters, and sequence and acknowledgment number parameters.
- TCP has a 20-byte segment header overhead, while UDP has 8 segment header overhead.
- TCP has a regulated send-rate. TCP has a congestion control mechanism that throttles the sender when one or more links between sender and receiver becomes excessively congested. This throttling can have a severe impact on real-time applications, which can tolerate some packet loss but require a minimum send rate. On the other hand, the speed at which UDP sends data is only constrained by the rate at which the application generates data, the capabilities of the source (CPU, clock rate, etc.) and the access bandwidth to the Internet.
2- What is Connected UDP?
Usually when creating a UDP socket we say:
socket = socket(PF_INET, SOCK_DGRAM, 0); //socket(family, type, proto)
Then to send using this socket we use:
void* buf, size_t nBytes,
const struct sockaddr* to,
socklen_t len); //ssize_t is how much data the OS accepted to send.
If we use a udp socket in a call to connect, we create what is called connected UDP. This simply tells the OS the address of the peer; no handshaking is necessary and no data is sent to establish that the peer exist (i.e., no data is sent as a result of calling connect() on a udp socket. Why use it? If we communicating with a single peer, this might be useful since:
- We can call sendto() with null destination address
- We can use write() and send()
- We can use read() and recv(), which will return only data from the remote peer we connect()ed to.