4.3.18 · HinglishComputer Networks

UDP — header, use cases, checksum

2,026 words9 min readRead in English

4.3.18 · Coding › Computer Networks


UDP actually hai kya

  • Connectionless → koi handshake nahi; pehla packet seedha data carry karta hai.
  • Unreliable → koi ACK nahi, koi retransmission nahi. Lost = gone.
  • Message-oriented (datagram) → ek send() = ek datagram = ek recv(). (TCP ek byte-stream hai; UDP message boundaries preserve karta hai.)

UDP Header — "minimum kya chahiye?" se derive kiya

Header kaise figure out karein? Memorize mat karo — reconstruct karo yeh puchh ke "Transport ko IP mein kya add karna padega?"

  1. IP ek machine tak deliver karta hai, lekin ek machine mein bahut saare programs chalte hain. Hume identify karna hoga ki kaun sa programsource port + destination port (16 bits each, toh 65535 tak).
  2. Receiver ko pata hona chahiye datagram kahan khatam hota hai → ek length field.
  3. Hum chahenge kuch error detection (IP sirf apna header checksum karta hai, data nahi) → ek checksum.

Bas. Char fields, har ek 16 bits = kul 8 bytes.

Figure — UDP — header, use cases, checksum

Compare karo: UDP header = 8 bytes, TCP header = 20 bytes (minimum). Woh 12-byte ki bachat + koi handshake nahi — yahi toh pura point hai.


UDP Checksum — scratch se derive kiya

Yeh kaise build hota hai (16-bit words par 1's complement sum):

  1. Data ko 16-bit integers ki sequence ki tarah treat karo.
  2. Unhe add karo. Jab bhi sum 16 bits overflow kare, carry ko wapas wrap around karke low 16 bits mein add karo (yahi one's-complement addition hai).
  3. Final sum ka one's complement lo (har bit flip karo) → wahi checksum hai jo tum transmit karte ho.

One's complement + carry-wrap kyun?

  • Carry-wrap result ko 16 bits ke andar rakhta hai bina information khoye (overflow carry discard nahi hoti, recycle hoti hai).
  • Complement bhejna matlab receiver sirf sab kuch including checksum add kar sakta hai aur all-ones () expect kar sakta hai: kyunki .

Use cases — "unreliable" kyun choose karein?

Use case UDP kyun jeet ta hai
DNS query Ek chhota request, ek chhota reply. TCP handshake (3 packets) lookup se zyada costly hoga.
Video/voice (VoIP, streaming, games) Ek retransmitted purana frame itna late aata hai ki useless hota hai — drop karo, aage badho.
DHCP, ARP-like bootstrapping Client ka koi IP/connection nahi; datagram broadcast karo.
SNMP, NTP Tiny, frequent, loss-tolerant management/time packets.
QUIC / HTTP/3 TCP ki rigidity se bachne ke liye userspace mein custom reliability + encryption UDP ke upar build karta hai.

Common mistakes (steel-manned)


Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho tum apne dost ko ek quick note bhej rahe ho unke door ke neeche se. Tum knock nahi karte, "mil gaya!" ka intezaar nahi karte — bas neeche se daal dete ho aur chale jaate ho. Agar hawa usse uda le jaaye, toh tumhe kabhi pata nahi chalega. Woh hai UDP: super fast, koi waiting nahi, lekin koi promise nahi ki pahunch jaayega. Kam se kam ek phata hua note pakadne ke liye, tum neechey ek chhota sa number likhte ho jo saare words ke letter-counts ka sum hota hai (woh hai checksum); tumhara dost dobara add karta hai aur agar number galat hai, toh woh jaanata hai note smudge ho gaya aur bas usse phenk deta hai. "Game ball position X par hai" 60 baar per second bhejna ke liye perfect — agar ek note kho bhi jaaye, agla note waise bhi fix kar dega.


Flashcards

UDP header kitne bytes ka hota hai aur usmein kitne fields hote hain?
8 bytes, 4 fields (har ek 16 bits): Source Port, Destination Port, Length, Checksum.
UDP Length field kya count karta hai?
Header + data = 8 + payload bytes; minimum value 8 hai (empty payload).
UDP ko "unreliable" kyun kaha jaata hai agar usmein checksum hai?
Yeh corruption detect karta hai (checksum) lekin recover nahi karta — koi ACKs ya retransmission nahi. Corrupt datagrams silently drop ho jaate hain.
UDP checksum kaise compute hota hai?
Saare 16-bit words ka one's-complement sum (carry wrap-around ke saath), phir result ka one's complement lo.
Receiver ko saare words plus checksum sum karne par kya milna chahiye?
All ones, 0xFFFF (one's-complement arithmetic mein) → datagram accepted.
One's-complement addition mein carry wrap kyun kiya jaata hai?
Result ko 16 bits ke andar rakhne ke liye bina overflow information khoye.
UDP (IPv4) mein 0x0000 checksum field ka kya matlab hai?
Checksum disabled/not computed. Ek genuine zero result 0xFFFF ke roop mein bheja jaata hai instead.
Pseudo-header kya hota hai aur checksum mein include kyun kiya jaata hai?
Src IP, Dst IP, protocol=17, UDP length. Dest IP fold karne se receiver galat machine par misdeliver hue datagrams detect kar sakta hai.
TCP vs UDP: stream ya message?
TCP = byte stream (koi boundaries nahi). UDP = message/datagram-oriented (ek send = ek recv, boundaries preserve hoti hain).
Teen classic UDP use cases aur unka reason batao.
DNS (tiny request/reply, handshake too costly), VoIP/video/games (late retransmit useless hai), DHCP/SNMP/NTP (small loss-tolerant control packets).
HTTP/3 (QUIC) UDP ke upar kyun chalta hai?
Userspace mein custom reliability + encryption build karne ke liye, TCP ki rigidity aur head-of-line blocking se bachne ke liye.
Kya correct checksum ke saath bhi corrupted data pass ho sakta hai?
Haan — do compensating errors (jaise alag words mein +1 aur −1) cancel ho jaate hain; 16-bit checksum weak detection hai, cryptographic integrity nahi.
TCP header min size vs UDP header size?
TCP = 20 bytes minimum; UDP = 8 bytes.

Connections

  • TCP — header, three-way handshake, reliability (reliable counterpart)
  • IP — datagrams and addressing (UDP IP ke upar ek patli layer hai)
  • DNS resolution (primary UDP user)
  • QUIC and HTTP-3 (reliability rebuilt over UDP)
  • One's complement arithmetic (checksum ka math)
  • Transport layer — ports and multiplexing (port fields exist kyun karte hain)

Concept Map

thin wrapper over

is

is

is

adds

adds

identify program

marks datagram end

detects corruption

no handshake

no ACK or retry

only 8 bytes

uses

UDP Datagram Protocol

IP Layer

Connectionless

Unreliable

Message-oriented

Port Numbers

Length Field

Checksum

8-byte Header

Fast Low Overhead

Ones-complement sum