th truyen du lieu buoi 4
TRANSCRIPT
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 1/8
D AT A s e q = 0 3s
A C K s e q = 0
k expected = 0
frame expected = 0
D AT A s e q = 1
BÀI TẬP THỰC HÀNH SỐ 4
MÔN HỌC: TRUYỀN DỮ LIỆU
MÔ PHỎNG GIAO THỨC STOP AND AIT
! G"#" $%"&'
Giao thức Stop and wait là một giao thức $(')*+ ,- +%.+ /0 1"&' $"+ 2.) được sử dụngở cả tầng 4 – Tanspot la!" #à tầng $ – %ata &in' &a!"( Giao thức nà! c) th* +ử l, c-c
d. li/0 12 l3i 1ng c-ch5
6 T7nh ch"c's0m cho m3i g)i tin6 Gởi 89: 6 ac'nowl"dg"m"nt sa0 'hi nh;n g)i tin thành c<ng6 =-nh d>0 th?i gian sa0 m3i lần gửi đ* đảm 1ảo 1@n gửi sA gửi lBi g)i tin nC0 'h<ng nh;n
được 89: tong một 'hoảng th?i gian +-c đ2nh(
TH!: TRUYỀN NHẬN TỐT
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 2/8
3s
ack expected = 0frame expected = 0checksum !=123ignore frame
checksum = 123,trả v gi ackD AT A s e q = 0 c h e c k s u m = 12 3
Timeout => gửi lại(start timer)C K s e q = 0
TH3: BAD CHC5SUM
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 3/8
D AT A s e q = 0
10s
A C K s e q = 0
k expected = 0
hậ !"#c ack ửi g%i ti&p theotop timer)
frame expected = 0
D AT A s e q = 1
TH6: RTT 7ROUND TRIP TIM8 9U DÀI
TH4: RTT 7ROUND TRIP TIM8 9U NG;N
<<<
+
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 4/8
ThDc hi/n
TưEc ti@nF ta c) m< hnh t0!Hn d. li/0 gi.a $ nod" như sa05
compile = "stopandwait.c"
messagerate = 100ms,propagationdelay = 70ms,probframecorrupt = 1
host perth { x=100 y=50 winx=0, winy=50 lin to sydney { !!
host sydney { east east east east of perth winx=550, winy=50!
TưEc ti@nF ta đ2nh nghIa c-c 1iCn toàn cục cho giao thức nà!(
include #cnet.h$include #stdlib.h$include #string.h$
typedef enum { %&'%()(, %&'(*+! -(/+%2
typedef struct { char data3(4'/(6/'/82! 62
typedef struct { -(/+% ind2 /* Chỉ có 2 loại là DATA hoặc ACK */ size_t len2 /* Chiều dài message */ int checsum2 /* checksum của frame */ int se92 /* or ! */
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 5/8
6 msg2! -(/2
define -(/':/(%/-'/ ;si<eof;-(/ > si<eof;6define -(/'/;f ;-(/':/(%/-'/ ? f.len
static 6 @lastmsg2static int lastlength = 02static *net)imer% lasttimer = A&&)/-2
static int acexpected = 02static int nextframetosend = 02static int frameexpected = 02
Tong hàm reboot'node; F ta đ2nh nghIa c-c hàm +ử l, sD 'i/n cho một nod"( Tong đ)F/B'%/CA60 đ* ta c) th* th"o dJi tBng th-i cKa giao thức stopandwait tong L0- tnh hoBt động(
EVENT_HANDLER(reboot_node){ if;nodeinfo.nodenumber $ 1 {
fprintf;stderr,")his is not a D>node networEFn"2exit;12
!
lastmsg = calloc;1, si<eof;62
*:/*+;*/)'set'handler; /B'(GG&*()H-/(%I, application'ready, 02 *:/*+;*/)'set'handler; /B'G:I*(&-/(%I, physical'ready, 02 *:/*+;*/)'set'handler; /B')/-1, timeouts, 02 *:/*+;*/)'set'handler; /B'%/CA60, showstate, 02
*:/*+;*/)'set'debug'string; /B'%/CA60, "tate"2
if;nodeinfo.nodenumber == 0*/)'enable'application;(&&H%/2
!
static EVENT_HANDLER(shostate)! printf; "FnFtacexpectedFt= JdFnFtnextframetosendFt=JdFnFtframeexpectedFt= JdFn",
acexpected, nextframetosend, frameexpected2"
Mi/c đầ0 ti@n 'hi c-c nod" được "1oot là một nod" sA tiCn hành tBo #à th<ng 1-o là c) c-c
m"ssag" đN sOn sàng đ* gửi( 9hPng ta làm #i/c nà! #Ei sư 'i/n /B'(GG&*()H-/(%I #à hàmapplication'ready;(cnet cho phQp c-c giao thức được giả l;p c) th* 'i*m so-t được tRc độ+0>t d. li/0 t 8pplication &a!"( 9hPng ta làm #i/c nà! 1ng c-ch enabling #à disabling
8pplication &a!" #Ei hàm */)'enable'application;(
static EVENT_HANDLER(app#ication_ready)
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 6/8
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 7/8
nh;n( Sa0 đ)F l>! th?i gian đ) nhZn [ đ* c) được ưEc t7nh \TT( Ta d]ng hàm*/)'start'timer; g^i sD 'i/n /B')/-1 1_t đầ0 t7nh th?i gian dD 'iCn cho đCn 'hi nh;nđược 89: cho d. li/0 đ)( 90Ri c]ngF ta d]ng hàm i@ng cKa cn"t */)'ccitt đ* t7nh ch"c's0m#à đưa #ào tư?ng ch"c's0m cKa Vam" tưEc 'hi g^i */)'write'physical; đ* gửi Vam"+0Rng lin' `(
SD 'i/n phức tBp nh>t là +ử l, 'hi c) một Vam" đCn t@n lin' 6 /B'G:I*(&-/(%I #Ei hàm*/)'read'physical; đ* đ^c Vam"( :hi nh;n được Vam"F nod" cần t7nh ch"c's0m đ* +-cđ2nh Vam" c) 12 l3i tong L0- tnh t0!Hn ha! 'h<ng( YC0 Vam" 12 l3i th 1 L0a Vam" đ) #ch_c ch_n ng Vam" đ) sA được gửi lBi( YC0 Vam" 'h<ng 12 l3i th c) $ tư?ng hợp sa05
6 YC0 Vam" là 89:F ta 1iCt ng d. li/0 được gửi thành c<ngF ngưng th?i gian tim"o0t #àg^i */)'enable'application; đ* 8pplication &a!" c) th* gửi d. li/0 tiCp th"o(
6 YC0 đ) là Vam" d. li/0F m"ssag" sA được đưa l@n tầng 8pplication #Ei hàm
*/)'write'application;F đUng th?i gửi 89: cho 1@n gửi #Ei hàmtransmit'frame;F đ* +-c nh;n là d. li/0 đN được nh;n thành c<ng(
static EVENT_HANDLER(physica#_ready){ -(/ f2 si<e't len2 int lin, checsum2
len = si<eof;-(/2 *:/*+;*/)'read'physical;Klin, Kf, Klen2
checsum = f.checsum2 f.checsum = 02 if;*/)'ccitt;;unsigned char @Kf, ;intlen E= checsum { printf;"FtFtFtFtC(% checsum > frame ignoredFn"2 return2 OO bad checsum, ignore frame !
switch ;f.ind { case %&'(*+ M if;f.se9 == acexpected { printf;"FtFtFtFt(*+ receiLed, se9=JdFn", f.se92 */)'stop'timer;lasttimer2 acexpected = 1>acexpected2
*/)'enable'application;(&&H%/2 !
brea2
case %&'%()( M printf;"FtFtFtFt%()( receiLed, se9=Jd, ", f.se92 if;f.se9 == frameexpected { printf;"up to applicationFn"2 len = f.len2
7/24/2019 TH Truyen Du Lieu Buoi 4
http://slidepdf.com/reader/full/th-truyen-du-lieu-buoi-4 8/8
*:/*+;*/)'write'application;Kf.msg, Klen2 frameexpected = 1>frameexpected2 ! else printf;"ignoredFn"2 transmit'frame;A&&, %&'(*+, 0, f.se92
brea2 !!
Tong Vil" m< hnh ta c) tham sR probframecorrupt6 +-c đ2nh tb l/ Vam" 12 l3i( :hi c)Vam" 12 l3iF 89: sA 'h<ng được gửi( Tong tư?ng hợp nà!F ta cần một hàm tim"o0tđ* 1@n gửi c) th* gửi lBi d. li/0 'hi hCt th?i gian tim"o0t(
static EVENT_HANDLER(timeouts)! printf;"timeout, se9=JdFn", acexpected2 transmit'frame;lastmsg, %&'%()(, lastlength, acexpected2
"
3 B-" $.=
`( e nghIa cKa probframecorrupt F propagationdelay tong Vil" c>0 hnhf
$( 9hB! m< hnh stop6and6waitF #à +"m 'Ct L0ả thRng '@ sa0 'hi 'Ct thPc m< hnh Wth@mtham sR 6s 'hi 'hởi động m< hnh – +"m 1ài thDc hành `X( Tha! đi gi- t2 probframecorrupt #à propagationdelay đ* th>! được sD ảnh hưởng cKa c-c 1iCn nà! l@nt7nh hi/0 L0ả cKa giao thức( Th0 th;p d. li/0 #à #A 1i*0 đU hi*n th2 t7nh hi/0 L0ảW"VVic"nc!X cKa giao thức th"o gi- t2 cKa probframecorrupt. Yh;n +Qt #H mRi L0an h/gi.a t7nh hi/0 L0ả #à gi- t2 cKa probframecorrupt W1i*0 đU t0!Cn t7nh ha! phức tBp hnX(
[( Tha! đi th?i gian ưEc t7nh \TT sao cho th?i gian nà! hCt tưEc 'hi nh;n được 89:(
6 0an s-t #à #A 1i*0 đU hành +ử cKa m< hnh6 #à 1i*0 đU gi.a t7nh hi/0 L0ả #à probframecorrupt
6 MA 1i*0 đU hi*n th2 t7nh hi/0 L0ả th"o gi- t2 cKa cKa \TT( Yh;n +Qt mRi li@n h/