Post

TCP / UDP

reliable connection https://soheeparklee.github.io/posts/n-reliableconnection/#-reliable-transmission

โœ… TCP

Transmission Control Protocol

  • reliable
  • conection-oriented
  • ๐Ÿ‘๐Ÿป assure transmission of data

  • exchange data when server is connected
  • network congestion avoidance algorithm

  • full duplex: ์ „์ด์ค‘, ์ „์†ก ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๋™์‹œ์—
  • point-to-point: ๊ฐ ์—ฐ๊ฒฐ์ด ์ •ํ™•ํžˆ 2๊ฐœ์˜ ์ข…๋‹จ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ

1๏ธโƒฃ Reliable

  • checksum, retransmission, ACK, timer, sequence number

๐Ÿ’ก reliable connection https://soheeparklee.github.io/posts/n-reliableconnection/#-reliable-transmission

2๏ธโƒฃ Connection Oriented

  • before transmission, TCP sets parameters for connection
  • Thus, pre-connection process before connection
  • called 3-way-handshake


  • TCP operates onlt on end systems
  • does not function within intermediate network elements
  • routers between the ends cannot detect TCP connection

  • full-duplex
  • point-to-point: one sender and one reciever, no multicasting

  • to terminate connection, 4-way-handshake

๐Ÿ“Œ Reliable TCP

  • provide reliable data transmission service
  • in an unreliable(best-effort) IP environment

What is reliable transmission?

a reliable transmission is transmission whereโ€ฆ

  • data is not corrupted,
  • in the right order


  • TCP uses single timer
  • (timer per segement would have huge overhead)

๐Ÿซฑ๐Ÿป TCP Sender

1. Recieve data from application and send

  • TCP recieves data from application
  • encapsulate data with segment
  • send segment to IP
  • If timer is not running, start timer when sending segment to IP

2. Timer

  • If timeout, retransmit segment
  • and restart timer

3. Recieve ACK from reciever

  • compare SendBase and ACK value y
    • SendBase: oldest order number that has not been acknowledged
    • y: value of ACK
  • TCP uses sequence numbers
  • y checks recievement of bytes before y
  • If y > SendBase
    • recievement of packets from SendBase to y-1 that has not recieved ACK has been confirmed
    • Thus, SendBase changes to y+1
    • Sliding window
  • If there is unchecked segment, restart timer

๐Ÿ“Œ Retransmission Scenarios in TCP

  • three scenarios might be possible for retransmission

Screenshot 2024-09-02 at 18 33 21

โœ”๏ธ lost ACK scenario

  • Reciever recieved segment 92 successfully
  • Reciever send ACK
  • ACK was lost

  • timeout occurs, segment 92 is retransmitted
  • Reciever has already recieved 92, thus throw away

โœ”๏ธ premature timeout

  • Sender sends segment 92, segment 100 to reciever
  • Reciever recieved both segments successfully
  • Reciever sends ACK100, ACK120 accordingly

  • However, sender recieves ACK100 after timeout
  • Sender retransmits segment 92
  • Sender recieve ACK120 before timeout
  • Sender does not retransmit segment 100

โœ”๏ธ cumulative ACK

  • Sender sends segment 92, segment 100 to reciever
  • Reciever recieved, sends ACK100, ACK120 accordingly
  • However, ACK100 was lost
  • Sender recieves only ACK120
  • Sender can assume until segment 100 was successfully recieved
  • Thus, retransmission does not occur

โณ When timeout, timeout โœ–๏ธ 2

  • When timeout occurs, TCP retransmits the segment with smallest order from segements that have not recieved ACK
  • Thus, when timeout occurs, TCP makes timeout period * 2
  • To reduce network congestion
  • This is called congestion control

๐Ÿš€ TCP fast retransmit

Screenshot 2024-09-02 at 18 47 24

  • ๐Ÿ‘Ž๐Ÿป Problem of retransmitting according to timeout:

    • retransmission according to timeout has problems too
    • when timeout period is too long


  • Sender can realize packet corruption after duplicate ACKs
  • When Reciever recieves segment in wrong order, sends the last correct ACK to sender
  • If sender recieves triple dublicate ACKs
  • sender can realize packet has been lost for that ACK
  • sender can retransmit


  • Sender does not wait for timeout,
  • thus this is fast retransmit

TCP ๐Ÿ†š GBN, SR

Is TCP GBN, or SR?

  • TCP is simmilar to GBN
  • Like GBN, TCP sender has to keep track of
    • SendBase(smallest order num without ACK)
    • NextSeqnum(next number segment to be transmitted)


  • TCP is different from GBN
  • If window size is 5, sent data 1, 2, 3, 4, 5 and ACK for packet 2 is lost
  • GBN would retransmit all 1, 2, 3, 4, 5
  • TCP would retransmit only 2
  • or, if sender recieves ACK 3/4/5(later than 2) before timeout, does not retransmit anything
  • bc TCP uses cumulative ACK


  • TCP is similar to SR
  • that it only retransmits the lost segment(selective)


  • TCP is different from SR
  • However, SR uses timer for each segment
  • TCP uses only one timer for the oldest segment that did not recieve ACK
  • SR does not use cumulative ACK
  • TCP uses cumulative ACK

  • โญ๏ธ conclusion: TCP is similar to GBN and SR, but is distinctive
  • TCP is more like a hybrid GBN, SR

โ˜‘๏ธ 3 way handshake

Screenshot 2024-09-02 at 21 34 12

PAR

Positive Acknowledgement with Re-transmission
TCP provides reliable connection with PAR
device using PAR resend the data until it recieves acknowledgement


โœ”๏ธ SYN:

  • synchronize
  • client sends SYN(Synchronize Sequence Number) to server
  • SYN bit = 1
  • send Sequence Number with random num

  • Result SYN:
  • client: port CLOSED โžก๏ธ SYN_SENT
  • server: LISTEN(port open, ready for communication), Wait for client

โœ”๏ธ SYN+ACK:

  • synchronize acknowledge
  • server ACK client request, request client to open port as well
  • server responds to client with SYN+ACK signals bit set
  • SYN bit = 1
  • Acknowledgement Number: Sequence Number + 1
  • client checks that server state is alive with server ACK message

  • Result SYN+ACK:
  • client: SYN_SENT
  • server: LISTEN โžก๏ธ SYN_RCVD(SYN recieved)

โœ”๏ธ ACK:

  • acknowledge
  • client acknowledges the response of server
  • client sends server ACK message so that server can check client and connect
  • establish reliable connection
  • can start exchanging data
  • server checks that client state is alive with client ACK message

  • Result ACK:
  • client: ESTABLISHED(port connected)
  • server: ESTABLISHED

โ˜‘๏ธ 4 way handshake

Screenshot 2024-09-02 at 23 10 34

terminate connection

โœ”๏ธ FIN:

  • client sends FIN to server
  • Fin bit = 1

  • Result
  • client: FIN_WAIT_1
  • server: CLOSE_WAIT

โœ”๏ธ ACK:

  • when server revieves FIN, sends ACK message to client
  • to send all data, server is in CLOSE_WAIT status

  • when client recieves ACK, change port status to FIN_WAIT_2
  • client waits for server FIN

  • Result
  • client: FIN_WAIT_2
  • server: CLOSE_WAIT

โœ”๏ธ FIN:

  • server tells client โ€œI finished sending dataโ€
  • if all data is sent, server send FIN to client
  • FIN bit = 1

  • Result
  • client: TIME_WAIT
  • server: LAST_ACK

โœ”๏ธ ACK:

  • client recieves FIN, sends ACK message to server
  • server closes after getting final ACK
  • client closes after TIME_WAIT

  • Result
  • client: CLOSED
  • server: CLOSED

โ“ Two way handshake?

Why not use two-way-handshake? What are the problems of two-way-handshake

Screenshot 2024-09-02 at 23 22 01

  • Two scenarios of two-way-handshake
  • 1๏ธโƒฃ Client request server for connection
  • 2๏ธโƒฃ Server replies ACK, connection created

  • However, following situations should not be connected
  • ๐Ÿ‘Ž๐Ÿป Client request that came to server is a retransmission request due to timeout
  • ๐Ÿ‘Ž๐Ÿป When client and server TCP communication is over

  • Thus, server has to check client state before connecting to client request
  • need to check if client state is alive
  • client has to tell server that its state is alive and possible for connection

โœ… Problems of TCP

  • packet loss
  • packet order miss
  • congestion
  • reciever overloaded

โ˜‘๏ธ Flow Control in TCP

Screenshot 2024-09-02 at 23 26 24

  • โš ๏ธ sender speed > reciever speed
  • reciever recieve too much segments even before sending read data to TCP socket
  • reciever buffer overflow โžก๏ธ cannot recieve more packet

  • ๐Ÿ’Š configure data sending speed according to reciever
  • configure senderโ€™s window size
  • ensure reciever Free Buffer Space(rwnd) is not overflowed
  • need to ensure reciever doesnt recieve too much packets
  • prevent reciever overflow

  • reciever sends feedback of state to sender
  • reciever send ACK with TCP header rwnd
  • with information about its Free Buffer Space(rwnd)

๐Ÿ’Š Solution

๐Ÿ’Š Stop and Wait

  • only send next packet when recieved message

๐Ÿ’Š Sliding window Screenshot 2024-07-27 at 00 59 08

  • only can packet size according to reciever
  • packet on air = sliding window
    • sliding window= last sent byte - last checked byte

โ˜‘๏ธ Congestion Control in TCP

Screenshot 2024-09-02 at 23 32 38

  • prevent that number of packet in network do not overexceed
  • configure window size to reduce network congestion
  • configure host, router to prevent congestion

What happens when network is congested?

  • more latency
  • packet loss

๐Ÿ’Š Solution

๐Ÿ’Š AIMD

Additive Increase/Multiplicative Decrease

Screenshot 2024-09-02 at 23 44 05

  • Additive increase: send n packet, if arrives successfully, send n+1
  • AIMD will, for every RTT, increase cwnd for 1
  • Multiplicative decrease: if fail, packet send speed /2



๐Ÿ’Š Slow Start

Screenshot 2024-09-02 at 23 51 27

  • For each RTT, send n packet, if arrives successfully, send n*2
  • double cwnd
  • If cwnd > ssthresh(OS threshold)
  • then for each RTT, increase cwnd for 1
  • this moment is called the slow start phrase
  • if packet loss, make ssthresh = current cwnd/2
  • packet loss can be detected with 3 duplicate ACK or timeout



โœ”๏ธ Fast Retransmit

  • if reciever recieves the wrong packet, send ACK
  • but in this ACK, send the number of packet that reciever missed to recieve
  • sender will acknowledge he didnt sent this packet, and send.
  • if repeated w same number packet more than three times, reduce window size

๐Ÿ’Š TCP Tahoe

  • If packet loss is detected, make cwnd to 1

๐Ÿ’Š TCP RENO

  • If packet loss is detected, make cwnd to cwnd/2(half)
  • widely used method

Flow control ๐Ÿ†š Congestion control

โœ”๏ธ Flow control:

  • control traffic from sender to reciever
  • protect reciever overflow
  • usually data link layer

โœ”๏ธ Congestion control:

  • control congestion on network
  • prevent network from congestion
  • usually network, transport layer

โœ… UDP

User Datagram Protocol
manage data in datagram unit

Screenshot 2024-09-01 at 14 43 46

  • conectionless
  • NOT reliable (transmission not granted)
  • ๐Ÿ‘๐Ÿป speed

  • data might arrive in different order from sent order
  • does not check if data was recieved
  • no flow control
  • no logical connection between client and server(no handshake)
  • packets are independent

  • 1:1
  • 1:N
  • N:N


๐Ÿ‘๐Ÿป Why use UDP?

Faster than TCP UDP header is simple too used for for real time services


Why does Domain Name Service use UDP?

  • DNS: application layer protocol
  • DNS request is small โžก๏ธ small enough to be stored inside UDP segment
  • do not need 3 way handshake(TCP overhead > UDP overhead)
  • loss for request will be configured by application layer


When does DNS have to use TCP?

zone transfer(request between DNS servers) if data is bigger than 512 bytes

1๏ธโƒฃ Not reliable

  • data can be lost
  • data can be revieced in different order
  • can check data integrity: with checksum header
  • if reliability is needed, compliment reliability in application level

โญ๏ธ UDP checksum header

  • purpose: verify data integrity
  • divide UDP data segment into word(16bit)
  • add each word, add 1, create checksum
  • send segment with UDP checksum header


  • recipient recieve segment, create checksum on recieved segemnt
  • check if recieved checksum matches
  • if matches, the segment has not been altered

2๏ธโƒฃ Connectionless

  • no pre-connection process, no handshake
  • faster

3๏ธโƒฃ faster speed, less overhead

  • no reliability, no connection โžก๏ธ fast
  • less overhead

    • TCP: 20byte overhead
    • UDP: 8byte overhead
  • used in DNS, SNMP, streaming service
  • DNS:
    • purpose is to get IP address to domain name
    • use UDP for speed
    • and UDP less overhead
  • SNMP:
    • simple network management protocol
    • network device can be numerous
    • UDP is more adequate, less overhead and faster speed
  • internet calls, streaming service:
    • minimum transmission guaranteed
    • allow little data loss
    • thus, UDP is adequate

TCP ๐Ÿ†š UDP

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-06 แ„‹แ…ฉแ„’แ…ฎ 5 58 08

  • ๊ณตํ†ต์ :
  • way to establish connection
  • unit of transport layer: segment

โ˜‘๏ธ TCP: Transmission Control Protocol

  • ์˜ˆ์˜๋ฐ”๋ฅธ ํ”„๋กœํ† ์ฝœ
  • โ€œ๋ฐ์ดํ„ฐ ์ „์†กํ•ด๋„ ๋ ๊นŒ์š”?โ€ ๋ฌผ์–ด๋ณด๊ณ  ๋ฌธ์ œ ์—†๋‹ค๊ณ  ํ•˜๋ฉด ๊ทธ์ œ์•ผ ๋ณด๋ƒ„.
  • ๋ณด๋‚ธ ํ›„ โ€œ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹คโ€ ์ธ์‚ฌ๊นŒ์ง€ ํ•˜๊ณ  ๋ฌผ๋Ÿฌ๊ฐ„๋‹ค.

  • ์—ฐ๊ฒฐ ์ง€ํ–ฅํ˜• ํ”„๋กœํ† ์ฝœ
  • ๋ฐ์ดํ„ฐ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ์‹ ๋ขฐ์„ฑ/ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ์ „์†ก ๊ณ„์ธต ํ”„๋กœํ† ์ฝœ
  • UDP์— ๋น„ํ•ด์„œ๋Š” ๋Š๋ฆผ
  • ์ •ํ™•์„ฑ ํ•„์š”ํ•˜๋ฉด TCP

๐Ÿ’ก ์—ฐ๊ฒฐ๊ณผ์ • 3-way-handshake

  1. SYN: ๋‚˜ ๋ฐ์ดํ„ฐ ๋ณด๋‚ด๋„ ๋ผ?
  2. SYN/ACK: ์‘ ๋ณด๋‚ด
  3. ACK: ๊ทธ์ œ์•ผ ๋ฐ์ดํ„ฐ ๋ณด๋‚ธ๋‹ค.

๐Ÿ’ก ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์ง„ ์‹œ ๋™์ž‘

  1. ๋ฐ์ดํ„ฐ(ํŒจํ‚ท) ์ˆœ์„œ ๋ณด์žฅ
  2. ๋ฐ์ดํ„ฐ ํ๋ฆ„ ์ œ์–ด
  3. ๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ ๋ณด์žฅ(์žฌ์ „์†ก)

๐Ÿ’ก ์—ฐ๊ฒฐ ํ•ด์ œ ๊ณผ์ • 4-way-handshake

  1. FIN
  2. ACK
  3. FIN
  4. ACK ํ•˜๊ณ  ๋‚˜์„œ์•ผ ์—ฐ๊ฒฐ ํ•ด์ œ

โ˜‘๏ธ UDP: User Datagram Protocol

๋ง‰๋ฌด๊ฐ€๋‚ด ํ”„๋กœํ† ์ฝœ
๊ทธ๋ƒฅ ์ผ๋‹จ ๋ฐ์ดํ„ฐ ๋ณด๋ƒ„.
๋ฐ›์œผ๋ฉด ๋ฐ›๋Š”๊ฑฐ๊ณ  ์•ˆ ๋ฐ›์œผ๋ฉด ์•ˆ ๋ฐ›๋Š”๊ฒจ~

๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ๋ณด๋‹ค๋Š” ์†๋„์™€ ๊ฐ„๋‹จํ•œ ํ†ต์‹ ์„ ์ค‘์‹œํ•˜๋Š” ์ „์†ก๊ณ„์ธต ํ”„๋กœํ† ์ฝœ
์†๋„ ์ค‘์š”ํ•˜๋ฉด UDP

  • ์—ฐ๊ฒฐ๊ณผ์ • 3-way-handใ…œshake ์ƒ๋žต
  • ๋ฐ์ดํ„ฐ(ํŒจํ‚ท) ์ˆœ์„œ ๋ณด์žฅ โŒ
  • ๋ฐ์ดํ„ฐ ํ๋ฆ„ ์ œ์–ด โŒ
  • ๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ ๋‚ฎ์Œ
  • ์—ฐ๊ฒฐ ํ•ด์ œ ๊ณผ์ • 4-way-handshake ์ƒ๋žต
This post is licensed under CC BY 4.0 by the author.