Showing posts with label Experiments on Data Link Layer and Network Layer. Show all posts
Showing posts with label Experiments on Data Link Layer and Network Layer. Show all posts

Saturday, June 25, 2011

Go back N Protocol [Matlab code]


clc;
clear all;
n=input('Number Of Frames: ');
w=input('Window Size: ');
pt=1;
flag=0;
flag2=0;
a=1:n;
while flag==0
if flag2==0
%Transmitting The Frames In A Window
for i=1:w
fprintf('Frame %d Transmitted\n',a(pt));
pt=pt+1;
end
flag2=1;
end
s=randint(1,1,10);
if s>3
fprintf('PAK of Frame %d Received\n',a(pt-w));
fprintf('Frame %d Transmitted\n',a(pt));
if a(pt)==n
flag=1;
end
pt=pt+1;
else
fprintf('NAK Of Frame %d Received\n',a(pt-w));
for j=0:w-1
fprintf('Frame %d Discarded\n',a(pt-w+j));
end
pt=pt-w;
flag2=0;
end
end
%Last 'W' Frames are dealt seperately
i=n-w+1;
while (i<=n)
s=randint(1,1,10);
if s>4
fprintf('PAK of Frame %d Received\n',a(i));
i=i+1;
else
fprintf('NAK of Frame %d Received \n',a(i));
for j=i:n
fprintf('Frame %d Discarded\n',a(j));
end
for k=i:n
fprintf('Frame %d Transmitted\n',a(k));
end
end
end

Distance Vector Routing [Matlab]


clc;
clear all;
ack=0;
s=input('Enter the source router no: ');
z=input('Enter the destination node: ');
while ack==0
%delay routing tables for 5 modes
A=randint(1,5,100);%metric may be delay or no of hops
B=randint(1,5,100);
C=randint(1,5,100);
D=randint(1,5,100);
E=randint(1,5,100);
A(1)=0;B(2)=0;C(3)=0;D(4)=0;E(5)=0;
r=vertcat(A,B,C,D,E);
disp('All routers are updated with routing tables of neighbouring routers');
fprintf('The routing tables for router %d is \n',s);
disp(r);
d(1,5)=0;
count=1;
for i=s
for j=1:5
if j~=i
for k=1:5
if k~=j && k~=i
for l = 1:5
if l~=k && l~=j && l~=i
for m = 1:5
if m~=k && m~=j && m~=i && m~=l
if(m==z)
d(count,:)=[i,j,k,l,m];
count=count+1;
end
end
end
end
end
end
end
end
end
end
count1 =1;
for i=s
for j=1:5
if j~=i
for k=1:5
if k~=j && k~=i
for l=1:5
if l~=k && l~=j && l~=i
if l==z
c(count1,:)=[i,j,k,l];
count1= count1+1;
end
end
end
end
end
end
end
end
count2 = 1;
for i=s
for j=1:5
if j~=i
for k=1:5
if k~=j && k~=i
if k==z
e(count2,:) = [i,j,k,0];
count2= count2 + 1;
end
end
end
end
end
end
%finding the total delay of all possible paths
for i=1:6
del(i)=0;
for j=2:5
del(i)=del(i)+r(d(i,j-1),d(i,j));
end
end
count=i+1;
for i=1:6
del(count)=0;
for j=2:4
del(count)=del(count)+r(c(i,j-1),c(i,j));
end
count=count+1;
end
for i=1:3
del(count)=0;
for j=2:3
del(count)=del(count)+r(e(i,j-1),e(i,j));
end
count=count+1;
end
del(count)=r(s,z);
dc= sort(del);
%possible paths and latest delay paths
fprintf('Possble path delayS');
for i=1:count
for j=1:count
if dc(i)==del(j)
n(i)=j;
if j<=6
fprintf('\n%d %d %d %d %d %d %d',d(j,:),dc(i));
ma(i,:)=cellstr(num2str(d(j,:)));
elseif j<=12
fprintf('\n%d %d %d %d %d',c(j-6),dc(i));
ma(i,:)=cellstr(num2str(c(j-6)));
elseif j<=15
fprintf('\n%d %d %d %d',e((j-12),:),dc(i));
ma(i,:)=cellstr(num2str(e((j-12),:)));
elseif j==16
fprintf('\n%d %d %d',s,z,dc(i));
ma(i,:)=cellstr(num2str([s z]));
end
end
end
end
temp=dc(1);
for i=2:count
if(dc(i)<=temp)
temp=dc(i);
end
end
for i=1:count
if(dc(i)==temp)
break;
end
end
disp(sprintf('\n\n\t Least delay path is %s delay=%d',(char(ma(i))),dc(i)));
m(16,5)=0;
e=char(ma);
for i=1:length(ma)
m(i,1)=str2double(cellstr(e(i,1)));
m(i,2)=str2double(cellstr(e(i,4)));
m(i,3)=str2double(cellstr(e(i,7)));
m(i,4)=str2double(cellstr(e(i,10)));
m(i,5)=str2double(cellstr(e(i,13)));
end
k=m(1,2);
disp(sprintf('Packet sent to %d router',k));
disp('Updating the routing table..');
if (m(1,1)==s&&m(1,2)==z)
ack=1;
disp(sprintf('Packet sent to the destination router %d',z));
end
s=m(1,2);
z=m(1,3);
end

Bit stuffing in data frames[Matlab code]




clc;
clear all;
msg=input('Input Message Binary Bit Stream: ');
count=0;
stuffcount=0;
[M N]=size(msg);
for j=1:N-5+stuffcount
for i=j:j+5
if msg(i)==1
count=count+1;
else
count=0;
break;
end
end
if count==6
msg=[msg(1:j+4) 0 msg(j+5:end)];
count=0;
stuffcount=stuffcount+1;
end
end
fprintf('Stream after the bit is stuffed:');
disp(msg);














Google : The top most search engine

Google : MAGIC BOX

nRelate - Posts and Homepage

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Which is the toughest subject ?