CN External
CN External
n1 to n3. give all the nodes labels and colours for the above wirless communication
# Create Simulator
set ns [new Simulator]
# Create nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
}
# End simulation
$ns at $val(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam tcp_udp.nam &
exit 0
}
# Run simulation
$ns run
# Simulation Parameters
set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Max packet queue length
set val(nn) 10 ;# Number of nodes
set val(rp) DSDV ;# Routing protocol
set val(x) 600 ;# X dimension of topology
set val(y) 600 ;# Y dimension of topology
set val(stop) 50 ;# Simulation stop time
# Create Simulator
set ns [new Simulator]
# Create 10 nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) color Blue
$ns at 0.5 "$node_($i) label Node$i"
# Run simulation
$ns run
create a random topology with 2 tcps and give different colors for the flows also create a
distance vector in it
# Finish procedure
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
# Set up routing
$ns rtproto DV
# Start traffic
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 3.0 "$tcp0 send"
$ns at 4.0 "$tcp1 send"
# Simulation end
$ns at 45 "finish"
$ns run
Create a wireless connection with 10 nodes , 4 are connection oriented and 4 are
connectionless , and specify each node with different colours And it was like we need to
just start the function but we should not specify any stop word to stop the running
process. But running should be stopped
# Create duplex links in a ring (node i to node i+1, and last to first)
for {set i 0} {$i < $num_nodes} {incr i} {
set next_node [expr ($i + 1) % $num_nodes]
$ns duplex-link $n($i) $n($next_node) 0.2Mb 10ms DropTail
$ns duplex-link-op $n($i) $n($next_node) orient right
$ns queue-limit $n($i) $n($next_node) 10
}
# Set up sliding window mechanism (TCP) between specific nodes in the ring
set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4
$ns attach-agent $n(0) $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n(3) $sink
$ns connect $tcp $sink
# Define simulator
set ns [new Simulator]
# Define nodes
set n(0) [$ns node]
set n(1) [$ns node]
set n(2) [$ns node]
set n(3) [$ns node]
set n(4) [$ns node]
set n(5) [$ns node]
# Start traffic
$ns at 0.5 "$ftp1 start"
$ns at 1.0 "$ftp2 start"
$ns at 1.5 "$cbr1 start"
$ns at 2.0 "$cbr2 start"
# Run simulation
$ns run
# Number of nodes
set num_nodes 10
# Start traffic
$ns at 0.5 "$ftp1 start"
$ns at 1.0 "$ftp2 start"
$ns at 1.5 "$cbr1 start"
$ns at 2.0 "$cbr2 start"
close all
clear all
Message=input('Enter Message Bits to be transmitted (Binary Data Only in matrix form):');
disp(dec2bin(Message)')
if size(dec2bin(Message)',1)~=1
disp('The Entered Message is not Binary data, Please wait for 5 seconds to re-enter the
Message');
pause(5);
HammingCode_Transmitter;
else
disp('The Message to be Transmitted is');
disp(Message);
Message=flip(Message);
disp('The length of the Message is ')
disp(length(Message));
for K=1:length(Message)
if (power(2,K)-1) >= length(Message)+K
disp('The total number of Parity bits required are ');
disp(K);
break;
end
end
disp('The total length of the message with Parity bits is ');
disp(length(Message)+K);
PB=-K:-1;
disp('The Parity bits to be appended initially are ');
disp(PB);
for s=1:length(Message)+K
for t=0:K
if s==power(2,t)
tx(s)=PB(end-t);
elseif s>power(2,t-1) && s<power(2,t)
tx(s)=Message(s-t);
end
end
end
disp('The Message generated with initially generated parity bits is ');
disp(flip(tx));
txlength=1:length(Message)+K;
dec_num = dec2bin(txlength,K)-'0';
for j=size(dec_num,2):-1:1
Parity_bit_positions=find((dec_num(:,j))');
actual_parity_bit_positions=Parity_bit_positions(2:end);
PB(j)=rem(sum(tx(actual_parity_bit_positions)),2);
end
disp('The actual Parity bits to be appended are ');
disp(PB);
for s=1:length(Message)+K
for t=0:K
if s==power(2,t)
tx(s)=PB(end-t);
end
end
end
end
disp('The actual Message to be transmitted with parity bits is');
disp(flip(tx))
BIT STUFFING
clc
close all;
msg=input('Input message in binary bit stream:');
count=0;
for j=1:length(msg)-5
for i=j:j+4
if msg(i)==1
count=count+1;
else
count=0;
break;
end
end
if count==5
msg=[msg(1:j+4) 0 msg(j+5:end)];
count=0;
elseif msg(end-4:end)==ones(1,5)
msg=[msg 0];
end
end
fprintf('stream after the bit stuffing is:')
disp(msg);
BIT DE-STUFFING
clc
close all
clear all
msg=input('Enter Received Message in Binary Bit Stream: ');
disp('The length of the message is')
disp(length(msg));
count=0;
msg2=msg;
for j=1:length(msg)-6
for i=j:j+4
if msg(i)==1
count=count+1;
else
count=0;
break;
end
end
if count==5 && msg(j+5)==0
msg2=[msg(1:j+4) msg(j+6:end)];
disp('The length of the message after Bit Destuffing is')
msg=msg2;
disp(length(msg2));
disp('Stream after the bit is Destuffed is:');
disp(msg2);
elseif count==5 && msg(j+5)==1
disp('ERROR')
count=0;
else
disp('ERROR')
count=0;
end
end
CRC
clc;
close all;
clear all;
m=input("enter msg: ");
g=input("enter pattern: ");
lm=length(m);
lg=length(g);
t=[m,zeros(1,lg-1)];
lt=length(t);
xm=t(1:lg);
rem1=[];
for i=lg:lt
if(xm(1)==g(1))
rem1=bitxor(xm,g);
end
if i==lt
rem1=rem1(2:end);
break;
end
rem1=[rem1(2:end),t(i+1)];
xm=rem1;
end
disp(rem1);
if(rem1==0)
disp("rececived sequence is correct")
else
disp("received sequence is wrong")
end
DCRC
clc
close all
clear all
tm=input('enter transmitted frame ');
g=input('enter pattern ');
lg=length(g);
ltm=length(tm);
txm=tm(1:lg);
rem1=[];
for i=lg:1:ltm
if(txm(1)==g(1))
rem1=bitxor(txm,g);
end
if i==ltm
rem1=rem1(2:end);
break;
end
rem1=[rem1(2:end),tm(i+1)];
txm=rem1;
end
if rem1==zeros(1,lg-1)
rem1
disp('"NO ERROR" ready to receive another frame');
else
rem1
disp('"ERROR" please retransmit');
dy=input('if u want to retansmit press "y" else "n" ','s');
if(dy=='y')
DCRCmat
end
end