750 likes | 760 Views
Learn about TCP (Transmission Control Protocol) and UDP (User Datagram Protocol), including their differences, features, and common application scenarios.
E N D
UDP dan TCP Risanuri Hidayat TCP
End-to-End Protocols • Underlying best-effort network • drop messages • re-orders messages • delivers duplicate copies of a given message • limits messages to some finite size • delivers messages after an arbitrarily long delay • Common end-to-end services • guarantee message delivery • deliver messages in the same order they are sent • deliver at most one copy of each message • support arbitrarily large messages • support synchronization • allow the receiver to flow control the sender • support multiple application processes on each host TCP
UDP • UDP = User Datagram Protocol • Connection less, best effort • Tidak memerlukan setting connection • Header sangat sederhana/simple dibanding TCP • Source dan Destination port sama dengan TCP punya • Datagram/paket/servis yang bisa bebas (unreliable) dan tidak urut(unordered) • Tidak ada flow control • Endpoints identified by ports • servers have well-known ports • see /etc/services on Unix • Optional checksum • psuedo header + UDP header + data TCP
0 16 31 SrcPort DstPort Checksum Length Data UDP • Port, menunjukkan layanan aplikasi (= TCP) • valid port numbers antara 0 – 65535 (16 bit) • static port numbersadalah port yang telah terregister (reserved),dynamicport merupakan yang unregistered.Sesuai persetujuan, nilai port di atas 49151 adalah dynamic ports. • (Aplikasi) Pengirim mengirim paket UDP melalui Source Port. (Aplikasi) penerima menerima (permintaan) aplikasi melalui Destination Port. TCP
UDP • Length, menunjukkan panjang data dalam octets. Jumlah bytes termasuk header dan data. Ukuran maksimum secara teoritis adalah 65535 bytes (16 bit). Meskipun demikian, beberapa implementasi membatasi Length ini menjadi lebih kecil, kadang-kadang hanya sampai8192 bytes. • Checksums, untuk check kesalahan. Pada UDP, checksummerupakan pilihan. Jika checksum ini tidak diperlukan maka nilai di-reset (nol semua). • Pada TCP, checksums merupakan keharusan. TCP
Mengapa pakai UDP • Packet oriented • Not a byte stream, packet integrity • Tidak perlu set up koneksi, bisa lebih cepat • Throughput lebih besar, karena UDP paket lebih mudah diproses di bagian Pengirim • User tidak begitu peduli dengan reliabilitas (ketahanan paket) • User ingin menggunakan Transport Protocol sendiri • E.g. For video TCP
TCP • TCP = Transmission Control Protocol • TCP berada di atas IP, mengirim paket dengan fungsi-fungsi yang sangat berguna • Streams. TCP data terorganisir sebagai stream of bytes, seperti file. Bentuk-bentuk datagram tidak akan kelihatan di sini. • Reliable delivery. Sequence numbers digunakan untuk mengurutkan data-data yang dikirim dan diterima. TCP akan mengirim lagi suatu data yang rusak atau hilang. • Network adaptation. TCP secara dinamis mempelajari tunda (delay)pengiriman akibat jaringandan berusaha memaksimumkan throughputtanpa membebani jaringan tersebut. • Flow control. TCP mengatur data buffers, dan koordinasi dengan trafik sehingga buffer-nya tidak pernah overflow. Pengirim yang cepat akan memperlambat kirimannya untuk menyesuaikan dengan penerima. TCP
TCP • TCP merupakan end-to-end reliableconnection,yang IP sendiri belum support • Koneksi TCP sering dikenal dengan nama “socket” • Socket dapat dianggap sebagai pipa penghubung bi-directional antara dua host. • Untuk menyambung socket diperlukan IP penerima dan nomor port-nya • Ports (bisa dianggap layanan) memungkinkan suatu host menghubungi host lain dengan lebih dari satu hubungan (lebih dari satu layanan) • Nomor port sampai 1024 telah terpakai (Lihat di UNIX /etc/service) TCP
Application process Application process W rite Read bytes bytes … … TCP TCP Send buffer Receive buffer … Segment Segment Segment T ransmit segments TCP Overview • Full duplex • Flow control: keep sender from overrunning receiver • Congestion control: keep sender from overrunning network • Transmission Control Protocol • Connection-oriented • Byte-stream • app writes bytes • TCP sends segments • app reads bytes TCP
TCP Format TCP
TCP Format • Source dan Destination port, merupakan aplikasi (layanan). Bersamaan dengan IP address disebut juga dengan Socket • Seq. Number, nomor urutan. Sebagai contoh, jika SN suatu segment = 1343 dan segment tersebut berisi data 512 octets, maka segment selanjutnya akan mempunyai SN = 1856 (=1343+512). • Ack Number, menunjukkan next paket yang diharapkan diterima oleh destination. Jika Source menerima AN yang tidak cocok dengan next SN-nya (milik Source), maka Source tahu ada paket yang hilang (rusak) dan tahu juga paket mana yang hilang (rusak) tersebut. TCP
TCP Format • Header length(4 bit), panjang Header dalam 32 bit • 0, (6 bit) diset = 0 semua • Flag (6 bit), digunakan untuk data flow dan connection control. Flag meliputi Urgent (URG), ACK, Push (PSH), Reset (RST), Synchronize (SYN), dan Final (FIN). • Adv. Window, untuk flow control, menunjukkan berapa bytes yang akan dikirim setelah ACK diterima. • Cheksum, meliputi Header dan Data • Urgent pointer, menunjukkan posisi urgent data pada TCP paket (URG harus diset = 1) TCP
TCP Format • Flag bits • URG = Urgent pointer field in use • ACK = Indicates whether frame contains acknowledgement • PSH = Data sudah di “pushed”. It should be delivered tohigher layers right away. • RST = Koneksi di-Reset (diulangi dari awal) • SYN = Untuk memulai koneksi • FIN = Untuk mengakhiri koneksi TCP
Data (SequenceNum) Sender Receiver Acknowledgment + AdvertisedWindow TCP Format • Each connection identified with 4-tuple: • (SrcPort, SrcIPAddr, DsrPort, DstIPAddr) • Sliding window + flow control • acknowledgment, SequenceNum, AdvertisedWinow • Flags • SYN, FIN, RESET, PUSH, URG, ACK • Checksum • pseudo header + TCP header + data TCP
Koneksi • Transport layer bertanggung jawab terhadap layer di atasnya untuk sambungan end-to-end connection • Diperlukan: • Connection setup • Connection tear-down TCP
Connection Establishment Three-way Handshake TCP
Setting Up Koneksi • Ketika suatu aplikasi di client ingin berhubungan dengan suatu aplikasi (layanan) di server (host lain), maka client tersebut harus men-set up sambungan Transport Layer ke layanan server tersebut • Mengapa harus three-way handshake? • Karena setting up a connection is not as easy as it seems (tidak semudah yang diduga) TCP
Setting Up Koneksi • Contoh yang buruk (Naïve) : • Source mengirim suatu connection setup packetke Destination • Destination mengirim balik acknowledgmentterhadap connection setuppackettadi, dan kemudian terjadi koneksi TCP
Naïve • Dapat terjadi duplikasiconnection request(CR) atau accept connection(AC) • Anggap bahwa jaringan mempunyai tunda yang lama (karena ramai, congestion) TCP
Penyelesaian • Dengan sequence number dan 3-way-handshake • Sequence number harus berulang pada suatu nilai yang besar untuk supaya yakin bahwa tidak ada sequencenumber yang sama TCP
3-way handshake: kasus 1 • Successful Connection : TCP
3-way handshake: Kasus 2 • Duplikasi CR TCP
2-way handshake • Problem Duplikasi SYN TCP
3 way handshake • Duplikasi CR dan ACK TCP
Fixing 2-way handshake • Assume sender and receiver have sameclock • Use time of clock to pick sequence numbers • can reject based on clock value • Assume receiver can remember all oldsequence numbers • can recall last SYN and reject TCP
3 way handshake: kasus 4 • Simultanous Open TCP
Connection Tear-Down • Two types of connection tear-down: • Asymmetric Release: • Either host may terminate the connection • TCP: Symmetric Release: • Both sides keep a unidirectional connection to theother • For each connection, the source tears it down whenno more packets will be sent TCP
Problem 1: Data lost • In asymmetric tear-down, data may be lost: TCP
Problem 1: Data lost • Partial solution: • Use 3-way handshake for connection tear-down • Destination host starts a timer after it receives adisconnect request (DR) • The destination finally releases the connection once itsacknowledgement is also acknowledged • If no return acknowledgement arrives within the timeoutinterval, the connection is disconnected TCP
Problem #2: Lost tear-downrequests • What if all disconnect requests are lost? TCP
Problem #2: Lost tear-downrequests • Solution: • Require a host to close a connection if nopackets have been received for a specifiedamount of time • Hosts transmit keep-alive packets to keep aconnection open when they have no data tosend TCP
TCP Connection Tear-down • Two double handshakes: TCP
Flow and Error Control • The transport layer, like the data link layer, mustprovide a flow-controlled and error-controlled link • The data link layer is hop-by-hop (node-to-node),while the transport layer is end-to-end • The same flow and error control protocols used inthe data link layer may be used with the transportlayer • One additional concern: packet resequencing TCP
Sending application Receiving application TCP TCP LastByteWritten LastByteRead LastByteAcked LastByteSent NextByteExpected LastByteRcvd Sliding Window Revisited • Sending side • LastByteAcked < = LastByteSent • LastByteSent < = LastByteWritten • buffer bytes between LastByteAcked and LastByteWritten • Receiving side • LastByteRead < NextByteExpected • NextByteExpected < = LastByteRcvd +1 • buffer bytes between NextByteRead and LastByteRcvd TCP
Sliding Window with Out of OrderArrivals • Sender side window is unaffected by out of orderreception of packets at the receiver • Receiver side window, however, behavesdifferently when packets are able to arrive out oforder • New techniques required TCP
Sliding Window with Out of OrderArrivals • Procedure for receiver-side sliding window: • Packets with sequence numbers outside the slidingwindow are discarded • When a packet arrives out of order, place a mark by thepacket’s sequence number in the window • When the first packet in the sliding window arrives,adjust the start of the sliding window up to the nextunmarked sequence number. Generateacknowledgements for each of the sequence numbersthe sliding window just passed. TCP
TCP Flow Control • TCP uses a modified version of the slidingwindow • In acknowledgements, TCP uses the“Window size” field to tell the sender howmany bytes it may transmit • TCP uses bytes, not packets, as sequencenumbers TCP
TCP Flow Control • Important information in TCP/IP packet headers TCP
TCP Flow Control TCP
TCP Flow Control • Piggybacking: Allows more efficient bidirectionalcommunication TCP
TCP Flow Control Problems • The Small Packet Problem • Occurs when the source sends many smallpackets • The Silly Window Syndrome • Occurs when the destination reads a smallnumber of bytes at a time from its buffer TCP
The Small Packet Problem (SPP) • Consider an interactive application where the source hostsends each keystroke one at a time to the destination host • Each keystroke is 1 byte. After adding TCP/IP overhead, a 41-byte packet is generated • When the destination receives the packet, it returns a 40-byteacknowledgement packet • When the destination removes the byte from its buffer, a 40-bytewindow update packet is sent • Some applications echo the typed character back to the source,creating another 41-byte packet TCP
How TCP Solves the SPP • Nagle’s Algorithm: • When data is sent one byte at a time, send only the firstbyte • Buffer all remaining bytes until the first one isacknowledged • After receiving the acknowledgement, send all thebuffered bytes in one packet • This algorithm reduces the amount of bandwidthrequired to support interactive applications TCP
Problems with Nagle’sAlgorithm • Works find if protocol is round trip oriented • Send packet, wait for response • What if protocol has several small packets? • Type ahead with telnet over a slow link. • X-windows data (plot point, draw-line) • Socket option to turn off Nagle in Unix. TCP
Silly Window Syndrome (SWS) • Consider an application where the source sends in largeblocks of data but the destination reads bytes from itsbuffer 1 byte at a time • Each time the destination reads a byte from its buffer, it returns awindow update to the source • The source sees that it is only free to send 1 more byte so it sends asingle byte • This process repeats itself until all the data has been sent, 1 byte ata time TCP
How TCP Solves the SWS • Clark’s Solution: • Prevent the receiver application from reading only 1byte from its TCP buffer • The receiver application should only read from the TCPbuffer when it has sufficient application buffer space tohandle a larger chunk of data • The sender may also help by refusing to send small datapackets TCP