-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMP FILES
79 lines (75 loc) · 1.41 KB
/
IMP FILES
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
GO BACK N ARQ
clc;
clear all;
close all;
n = 3;
frames = 10;
i = 1;
j = 1;
while(i<frames)
if(j-i < n)
fprintf(" Transmitted %d \n", j);
j = j + 1;
else
s = randi(10,1,1);
if(s >= 8)
fprintf(" Time Out %d \n ", i);
j = i;
else
fprintf(" Received frame %d \n", i);
i = i + 1;
end
end
end
SELECTIVE REPEAT AND ARQ
n = input(''); % Read n from the user
k = input(''); % Read k from the user
wind = []; % Using an array to simulate a queue
disp('transmitting frame :');
for p = 0:(k-1)
fprintf('%d ', p);
wind(end+1) = p; % Push to the queue
end
fprintf('\n');
i = 0;
while ~isempty(wind)
s = randi([0, n-1]); % Generate a random number between 0 and n-1
if s <= 2
fprintf('received frame :%d\n', wind(1)); % Front of the queue
wind(1) = []; % Pop from the queue
if i+k < n
wind(end+1) = i+k; % Push to the queue
end
if i+k < n
fprintf('transmitting frame : %d\n', i+k);
end
i = i + 1;
else
fprintf('timeout frame :%d\n', wind(1)); % Front of the queue
fprintf('transmitting frame :%d\n', wind(1));
wind(end+1) = wind(1); % Push the front to the back of the queue
wind(1) = []; % Pop from the queue
if i+k < n
wind(end+1) = i+k; % Push to the queue
end
if i+k < n
fprintf('transmitting frame : %d\n', i+k);
end
i = i + 1;
end
End
STOP AND WAIT
clc;
close all;
n= 8;
l= 1;
while l<n;
fprintf('Transmitting frame %d \n' , l);
s= randi(10,1,1);
if s >= 3;
fprintf('Timeout \n %d \n' , l);
else
fprintf ( 'received frame %d \n', l);
l = l+1;
end
end