You are on page 1of 76

EC6611-COMPUTER NETWORKS LABORATORY

OBJECTIVES

The student should be made to:


1. Learn to communicate between two desktop computers.
2. Learn to implement the different protocols
3. Be familiar with socket programming.
4. Be familiar with the various routing algorithms
5. Be familiar with simulation tools.

LIST OF EXPERIMENTS

1. Implementation of Error Detection / Error Correction Techniques


2. Implementation of Stop and Wait Protocol and sliding window
3. Implementation and study of Goback-N and selective repeat protocols
4. Implementation of High Level Data Link Control
5. Study of Socket Programming and ClientServer model
6. Write a socket Program for Echo/Ping/Talk commands.
7. To create scenario and study the performance of network with CSMA / CA protocol and
compare with CSMA/CD protocols.
8. Network Topology-Star, Bus, Ring
9. Implementation of distance vector routing algorithm
10. Implementation of Link state routing algorithm
11.Study of Network simulator (NS) & simulation of Congestion Control Algorithms using NS
12. Encryption and decryption.

OUTCOMES
The student should be able to
1. Communicate between two desktop computers.
2. Implement the different protocols
3. Program using sockets.
4. Implement and compare the various routing algorithms
5. Use simulation tool.
INDEX

S.No Experiment Page Marks Signature

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.
PROGRAM: ERROR DETECTION AND CORRECTION

clc;
clear all;
% Input Generator Matrix
g=input('Enter The Generator Matrix: ')
disp ('G = ')
disp ('The Order of Linear block Code for given Generator Matrix is:')
[n,k] = size(transpose(g))
for i = 1:2^k
for j = k:-1:1
if rem(i-1,2^(-j+k+1))>=2^(-j+k)
u(i,j)=1;
else
u(i,j)=0;
end
end
end
u;
disp('The Possible Codewords are :')
c = rem(u*g,2)
disp('The Minimum Hamming Distance dmin for given Block Code is= ')
d_min = min(sum((c(2:2^k,:))'))
% Code Word
r = input('Enter the Received Code Word:')
p = [g(:,n-k+2:n)];
h = [transpose(p),eye(n-k)];
Hammimg Code
ht =
101
111
110
011
100
010
001
Syndrome of a Given Codeword is :
s =0 0 1
EXPT NO :1
DATE :

IMPLEMENTATION OF ERROR DETECTION /ERROR CORRECTION


TECHNIQUES

AIM:

To implement error detection and error correction techniques using Matlab.

SOFTWARE REQUIRED:

S No. COMPONENT
1. 2 Computers with Win XP or 7
2. Matlab

PROCEDURE:
1. Open Matlab simulator and start a new m file.
2. Perform error detection algorithmic steps in the code using Hamming Codes
3. Perform simulations and get necessary outputs
The Error is in bit:

i =7
The Corrected Codeword is :
r =1 0 0 0 1 0 1
disp('Hammimg Code')
ht = transpose(h)
disp('Syndrome of a Given Codeword is :')
s = rem(r*ht,2)
for i = 1:1:size(ht)
if(ht(i,1:3)==s)
r(i) = 1-r(i);
break;
end
end
disp('The Error is in bit:')
i
disp('The Corrected Codeword is :')
r
RESULT:

Thus error detection and error correction techniques were implemented using Matlab.
PROGRAM: (Stop and Wait Protocol)
# stop and wait protocol in normal situation
# features : labeling, annotation, nam-graph, and window size monitoring
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"
set nf [open A1-stop-n-wait.nam w]
$ns namtrace-all $nf
set f [open A1-stop-n-wait.tr w]
$ns trace-all $f
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10
Agent/TCP set nam_tracevar_ true
set tcp [new Agent/TCP]
$tcp set window_ 1
$tcp set maxcwnd_ 1
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_
$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet_0\""
$ns at 0.35 "$ns trace-annotate \"Receive Ack_0\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_1\""
EXPT NO :2
DATE :

IMPLEMENTATION OF STOP AND WAIT PROTOCOL AND SLIDING WINDOW

AIM:

To implement stop and wait, sliding window protocols using Network simulator.

SOFTWARE REQUIRED:

S No. COMPONENT
1. Computer with XP / 7 and Ethernet port
2. NS2

PROCEDURE:
1. Start the program.
2.Get the frame size from the user
3.Create the frame based on the user request.
4.Send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK
signal to client.
6.Stop the program
$ns at 0.79 "$ns trace-annotate \"Receive Ack_1\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_2\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack_2 \""
$ns at 1.43 "$ns trace-annotate \"Send Packet_3\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_3\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_4\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_4\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_5\""
$ns at 2.55 "$ns trace-annotate \"Receive Ack_5 \""
$ns at 2.75 "$ns trace-annotate \"Send Packet_6\""
$ns at 2.99 "$ns trace-annotate \"Receive Ack_6\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
proc finish {} {
global ns nf
$ns flush-trace
close $nf
puts "running nam..."
exec nam A1-stop-n-wait.nam &
exit 0
}
$ns run
OUTPUT: (Stop and Wait Protocol Paste it here)
PROGRAM: (Sliding Window Protocol)
# sliding window mechanism with some features
# such as labeling, annotation, nam-graph, and window size monitoring
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"
set nf [open A3-sliding-window.nam w]
$ns namtrace-all $nf
set f [open A3-sliding-window.tr w]
$ns trace-all $f
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10
Agent/TCP set nam_tracevar_ true
set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_
$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet_0,1,2,3\""
$ns at 0.34 "$ns trace-annotate \"Receive Ack_0,1,2,3\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_4,5,6,7\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack_4,5,6,7\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_8,9,10,11\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack_8,9,10,11 \""
$ns at 1.43 "$ns trace-annotate \"Send Packet_12,13,14,15\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_12,13,14,15\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_16,17,18,19\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_16,17,18,19\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_20,21,22,23\""
$ns at 2.56 "$ns trace-annotate \"Receive Ack_24,25,26,27\""
$ns at 2.76 "$ns trace-annotate \"Send Packet_28,29,30,31\""
$ns at 3.00 "$ns trace-annotate \"Receive Ack_28\""
OUTPUT: (Sliding Window Protocol Paste it here)
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
proc finish {} {
global ns
$ns flush-trace
# close $nf
# puts "filtering..."
# exec tclsh ../bin/namfilter.tcl A3-sliding-window.nam
puts "running nam..."
exec nam A3-sliding-window.nam &
exit 0
}
$ns run
RESULT:

Thus the implementation of stop and wait, sliding window protocols were performed using
Network simulator.
PROGRAM: (GBN)
clc;
clear all;
n=input('Number of frames:');
w=input('Window size:');
timer=input('enter the timer');
pt=1;
flag=0;
flag2=0;
a=1:n;
c=1;
x=zeros(1,c);
y=zeros(1,c);
z=zeros(1,c);
while flag==0
if flag2==0
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;
x(1,c)=1;
c=c+1;
elseif s<3
t=timer;
while t>0
disp(t);
t=t-1;
end
fprintf('timeout\n');
z(1,c)=1;
c=c+1;
for j=0:w-1
fprintf('Frame %d discarded \n',a(pt-w+j));
end
pt=pt-w;
flag2=0;

else
fprintf('NAK of frame %d received\n',a(pt-w));
y(1,c)=1;
c=c+1;
for j=0:w-1
fprintf('Frame %d discarded\n',a(pt-w+j));
end
pt=pt-w;
flag2=0;
EXPT NO :3
DATE :

IMPLEMENTATION AND STUDY OF GOBACK-N AND SELECTIVE REPEAT


PROTOCOLS
AIM:

To implement Goback - N, selective repeat protocols using Matlab.

SOFTWARE REQUIRED:

S No. COMPONENT
1. Computer with win-2K / XP / 7
2. MATLAB

Go Back N Protocols
Algorithm
* The algorithm for this process is as.
1. Start.
2. Establish connection (recommended UDP)
3. Accept the window size from the client(should be <=40)
4. Accept the packets from the network layer.
5. Calculate the total frames/windows required.
6. Send the details to the client(totalpackets,totalframes.)
7. Initialise the transmit buffer.
8. Built the frame/window depending on the windowsize.
9. Transmit the frame.
10. Wait for the acknowledgement frame.
11. Check for the acknowledgement of each packet and repeat the process
from the packet for which the first negative acknowledgement is
received.
Else continue as usual.
12. Increment the framecount and repeat steps 7 to 12 until all packets are
transmitted.
13. Close the connection.
14. Stop.
end
end
% LOST W FRAMES 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;
x(1,c)=1;
c=c+1;
elseif s<4

t=timer;
while t>0
disp(t);
t=t-1;
end
fprintf('timeout\n');
z(1,c)=1;
c=c+1;
for j=i:n;
fprintf('Frame %d discarded \n',a(j));
end
for k=i:n
fprintf('Frame %d transmitted \n',a(k));
end
else
fprintf('NAK of frame %d received\n',a(i));
y(1,c)=1;
c=c+1;
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
figure(1);
bar(x,'y');
hold on;
bar(y,'r');
hold on;
bar(z,'g');
legend('PAK','NAK','timeout');
OUTPUT:
GO BACK N

Number of frames:7
Window size:6
enter the timer3
Frame 1 transmitted
Frame 2 transmitted
Frame 3 transmitted
Frame 4 transmitted
Frame 5 transmitted
Frame 6 transmitted
PAK of frame 1 received
Frame 7 transmitted

i= 2

PAK of frame 2 received


3

timeout
Frame 3 discarded
Frame 4 discarded
Frame 5 discarded
Frame 6 discarded
Frame 7 discarded
Frame 3 transmitted
Frame 4 transmitted
Frame 5 transmitted
Frame 6 transmitted
Frame 7 transmitted
PAK of frame 3 received
PAK of frame 4 received
3

2
1

timeout
Frame 5 discarded
Frame 6 discarded
Frame 7 discarded
Frame 5 transmitted
Frame 6 transmitted
PROGRAM SELECTIVE REPEAT
clc;
clear all;
n=input('Number of frames:');
w=input('window size');
timer=input('enter timer value');
pt=w+1;
flag=0;
flag1=0;
ext=0;
flag2=0;
a=1:n;
i=1;
x=zeros(1,i);
y=zeros(1,i);
z=zeros(1,i);

for j=1:w
fprintf('Frame %d transmitted \n',a(j));
end
while flag==0
s=randint(1,1,10);
if s>3
fprintf('PAK of frame %d received\n',a(pt-w));
x(1,i)=1;
i=i+1;
elseif s<3
t=timer;
while t>0
t
t=t-1;
end
fprintf('timeout');
ext=ext+1;
for z=n+ext:-1:pt+1
a(z)=a(z-1);
end
a(pt)=a(pt-w);
y(1,i)=1;
i=i+1;
else
fprintf('NAK of frame %d received\n',a(pt-w));
ext=ext+1;
for z=n+ext:-1:pt+1
a(z)=a(z-1);
end
a(pt)=a(pt-w);
z(1,i)=1;
i=i+1;
end
fprintf('Frame %d transmitted\n',a(pt))
if a(pt)==n
flag=1;
end
pt=pt+1;
end
Frame 7 transmitted
3

1
timeout
Frame 5 discarded
Frame 6 discarded
Frame 7 discarded
Frame 5 transmitted
Frame 6 transmitted
Frame 7 transmitted
PAK of frame 5 received
PAK of frame 6 received
PAK of frame 7 received
>>

1
PAK
NAK
0.9 timeout

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
1 2 3 4 5 6 7 8 9 10
k=pt-w;
while flag2==0
test=randint(1,1,[1 7]);
if flag1==1
fprintf('Frame %d transmitted\n', a(k))
end
if test>3
fprintf('PAK OF Frame %d received\n',a(k))
k=k+1;
x(1,i)=1;
i=i+1;
elseif test<3
t=timer;
while t>0
t
t=t-1;
end
fprintf('timeout');
y(1,i)=1;
i=i+1;

else
fprintf('NAK of frame %d received\n',a(k))
z(1,i)=1;
i=i+1;
end
if k==n+ext+1
flag2=1;
end
end

figure(1);
bar(x,'y');
hold on;
bar(y,'r');
hold on;
bar(z,'g');
legend('PAK','timeout','NAK');
SELECTIVE REPEAT:
Number of frames:7
window size6
enter timer value3
Frame 1 transmitted
Frame 2 transmitted
Frame 3 transmitted
Frame 4 transmitted
Frame 5 transmitted
Frame 6 transmitted
t= 3
t= 2
t= 1
timeoutFrame 1 transmitted
PAK of frame 2 received
Frame 7 transmitted
t= 3
t= 2
t= 1
timeout
t= 3
t= 2
t= 1
timeout
t= 3
t= 2
t= 1
timeout
t= 3
t= 2
t= 1
timeoutPAK OF Frame 3 received
PAK OF Frame 4 received
NAK of frame 5 received
PAK OF Frame 5 received
t= 3
t= 2
t= 1
timeoutPAK OF Frame 6 received
NAK of frame 1 received
PAK OF Frame 1 received
PAK OF Frame 7 received
>>
SELECTIVE REPEAT:

1
PAK
timeout
0.9 NAK

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
1 2 3 4 5 6 7 8 9

RESULT:

Thus the implementation of Goback - N, selective repeat protocols were performed using
Matlab.
IMPLEMENTATION OF HIGH LEVEL DATA LINK CONTROL
Program:
set ns [new Simulator]
#Tell the simulator to use dynamic routing
$ns rtproto DV
$ns macType Mac/Sat/UnslottedAloha
#Open the nam trace file
set nf [open aloha.nam w]
$ns namtrace-all $nf
#Open the output files
set f0 [open aloha.tr w]
$ns trace-all $f0
#Define a finish procedure
proc finish {} {
global ns f0 nf
$ns flush-trace
#Close the trace file
close $f0
close $nf
exec nam aloha.nam &
exit 0
}
# Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
# Create duplex links between nodes with bandwidth and distance
$ns duplex-link $n0 $n4 1Mb 50ms DropTail
$ns duplex-link $n1 $n4 1Mb 50ms DropTail
$ns duplex-link $n2 $n5 1Mb 1ms DropTail
$ns duplex-link $n3 $n5 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 50ms DropTail
$ns duplex-link $n2 $n3 1Mb 50ms DropTail
# Create a duplex link between nodes 4 and 5 as queue position
$ns duplex-link-op $n4 $n5 queuePos 0.5
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n(3)
set null0 [new Agent/Null]
EXPT NO :4
DATE :

IMPLEMENTATION OF HIGH LEVEL DATA LINK CONTROL

AIM:

To implement High level data link control using Network simulator.

SOFTWARE REQUIRED:

S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. NS2

PROCEDURE:
1. Start the program.
2.Get the frame size from the user
3.Create the frame based on the user request.
4.Send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK
signal to client.
6.Stop the program
$ns attach-agent $n2 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n5 $n2
$ns rtmodel-at 2.0 up $n5 $n2
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
Output: Paste it here

RESULT:

Thus the implementation of High level data link control using Network simulator was
performed.
PROGRAM:
//TCP Date Server--tcpdateserver.java
import java.net.*;
import java.io.*;
import java.util.*;
class tcpdateserver
{
public static void main(String arg[])
{
ServerSocket ss = null;
Socket cs; PrintStream ps;
BufferedReader dis;
String inet; try
{
ss = new ServerSocket(4444);
System.out.println("Press Ctrl+C to quit"); while(true)
{
cs = ss.accept();
ps = new PrintStream(cs.getOutputStream());
Date d = new Date()
ps.println(d);
dis = new BufferedReader(new InputStreamReader(cs.getInputStream()));
inet = dis.readLine(); System.out.println("Client System/IP address is :"+ inet);
ps.close(); dis.close();
}
}
catch(IOException e)
{ System.out.println("The exception is :" + e);
}
}
}
EXPT NO :5
DATE :

STUDY OF SOCKET PROGRAMMING AND CLIENT SERVER MODEL

AIM:

To study the socket programming and client server model.

ALGORITHM:

Server

1. Create a server socket and bind it to port.


2. Listen for new connection and when a connection arrives, accept it.
3. Send servers date and time to the client.
4. Read clients IP address sent by the client.
5. Display the client details.
6. Repeat steps 2-5 until the server is terminated.
7. Close all streams.
8. Close the server socket.
9. Stop.

Client

1. Create a client socket and connect it to the servers port number.


2. Retrieve its own IP address using built-in function.
3. Send its address to the server.
4. Display the date & time sent by the server.
5. Close the input and output streams.
6. Close the client socket.
7. Stop.
// TCP Date Client--tcpdateclient.java
import java.net.*;
import java.io.*;
class tcpdateclient
{
public static void main (String args[])
{ Socket soc; BufferedReader dis;
String sdate; PrintStream ps;
try { InetAddress ia = InetAddress.getLocalHost();
if (args.length == 0)
soc = new Socket(InetAddress.getLocalHost(),4444);
else soc = new Socket(InetAddress.getByName(args[0]),4444);
dis = new BufferedReader(new InputStreamReader(soc.getInputStream()));
sdate=dis.readLine(); System.out.println("The date/time on server is : " +sdate);
ps = new PrintStream(soc.getOutputStream());
ps.println(ia);
ps.close();
catch(IOException e)
{
System.out.println("THE EXCEPTION is :" + e);
}
}
}

OUTPUT:

Server:

$ javac tcpdateserver.java $
java tcpdateserver
Press Ctrl+C to quit Client System/IP address is : localhost.localdomain/127.0.0.1 Client System/IP
address is : localhost.localdomain/127.0.0.1

Client: $

javac tcpdateclient.java $ java tcpdateclient The date/time on server is: Wed Jul 06 07:12:03 GMT
2011
Every time when a client connects to the server, servers date/time will be returned to the client for
synchronization.
Snapshots

RESULT:

Thus the socket programming and client server model was studied.
ECHO SOURCE CODE:
ECHO SERVER
import java.io.*;
import java.net.*;
class echoserver
{public static void main(String args[])
{
try
{
Socket s=null;
ServerSocket ss=new ServerSocket(8000);
s=ss.accept();
System.out.println(s);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
int i=1;
while(i>0)
{
String str=br.readLine();
if(str.equals("."))
break;
System.out.println("msg received by client:"+str);
print.println(str);
print.flush();
}}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
EXPT NO :6a
DATE :
ECHO COMMANDS
AIM:

To Write a program in java to demonstrate the ECHO command.

SOFTWARE REQUIRED:

S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. Java
ALGORITHM:

ECHOSERVER:

1. Start the program.


2. Import.net and other necessary packages.
3. Declare objects for DataInputStream, Socket and PrintWriter to receive server
message and send it back.
4. Store the message in a string and print the message using print() method.
5. Send the same received message to the server using PrintWriter and socket.
6. When the received message is end, then exit the program execution.

ECHOCLIENT:

1. Start the program.


2. Import.net and other necessary packages.
3. Declare objects for ServerSocket and Socket to send the message.
4. Declare objects for PrintStream to write message from server to client.
5. Get the user input and store it in a string.
6. Print the string in the socket using printStream to be received by the receiver.
7. Using the Print() method, receive the client echo message and print it.
8. If the message is end, terminate the program and exit.
ECHO CLIENT
import java.io.*;
import java.net.*;
class echoclient
{
public static void main(String a[])
{
try
{
Socket s=new Socket("LocalHost",8000);
DataInputStream in=new DataInputStream(System.in);
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
System.out.println("\n msg to be echo:");
String str=br1.readLine();
print.println(str);
print.flush();
System.out.println(br2.readLine());
}
catch(UnknownHostException e)
{}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
OUTPUT:
CLIENT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac echoclient.java
C:\Program Files\Java\jdk1.6.0\bin>java echoclient
msg to be echo:
GOD IS GREAT
GOD IS GREAT
C:\Program Files\Java\jdk1.6.0\bin>

SERVER:
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac echoserver.java
C:\Program Files\Java\jdk1.6.0\bin>java echoserver
Socket[addr=/127.0.0.1,port=1623,localport=8000]
msg received by client:GOD IS GREAT
C:\Program Files\Java\jdk1.6.0\bin>

RESULT:

Thus the ECHO command program was demonstrated using java simulator.
SOURCE CODE:
PING SERVER
import java.io.*;
import java.net.*;
public class pingserver
{
public static void main(String a[])
{
String line1,line2;
int i;
ServerSocket es;
DataInputStream di;
PrintStream ps;
Socket csoc;
es=null;
csoc=null;
try
{
es=new ServerSocket(9999);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("ping server");
try
{
csoc=es.accept();
di=new DataInputStream(csoc.getInputStream());
ps=new PrintStream(csoc.getOutputStream());
for(i=0;i<4;i++)
{
line1=di.readLine();
System.out.println("pinged by client");
ps.println(line1+"reply from host:bytes=3<time<1ms TT<=128");
}
di.close();
ps.close(); }
catch(Exception e)
{
System.out.println(e);
}}}
EXPT NO :6b
DATE :
PING COMMANDS

AIM:

To write a program in java to demonstrate the usage of PING command

SOFTWARE REQUIRED:

S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. Java

ALGORITHM:

1. Start the program.


2. Import.net and other necessary packages.
3. Initialize the ping server with both sockets as null value.
4. Start the serversocket.
5. At the client and give the IP address of the server.
6. The client program is then started by starting socket.
7. At the receiver end, the server is pinged.
PING CLIENT
import java.io.*;
import java.net.*;
public class pingclient
{
public static void main(String args[])
{
PrintWriter out=null;
int i,j,k;
BufferedReader networkIn=null;
try
{
System.out.println("enter the IP address:");
DataInputStream in = new DataInputStream(System.in);
String ip = in.readLine();
Socket thesocket = new Socket(ip, 9999);
networkIn = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(thesocket.getOutputStream());
System.out.println("\npinging" + ip + "with 32 bytes of data\n");
for (i = 0; i < 4; i++)
{
out.println(ip);
out.flush();
String inp = networkIn.readLine();
if (inp != null)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
}
System.out.println("reply from" + inp);
}
else
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
System.out.println("\nrequest time out");
}
OUTPUT
PING CLIENT
C:\Documents and Settings\SEENU.R>cd\

C:\>cd C:\Program Files\Java\jdk1.6.0\bin

C:\Program Files\Java\jdk1.6.0\bin>javac pingclient.java

C:\Program Files\Java\jdk1.6.0\bin>java pingclient

enter the IP address:

192.168.1.10

pinging192.168.1.10with 32 bytes of data

reply from5

reply from8

reply from9

reply from4

C:\Program Files\Java\jdk1.6.0\bin>
}
}
}
}
catch (IOException e)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 1000; j++)
{
for (k = 0; k < 5000; k++)
{
}
}
System.out.println("\nrequest timed out");
}
}
try
{
if(networkIn!=null)
networkIn.close();
if(out!=null)
out.close();
}
catch(Exception e){
System.out.println("\nrequested time out");
}
}
}
OUTPUT - PINGSERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac pingserver.java
C:\Program Files\Java\jdk1.6.0\bin>java pingserver
ping server
pinged by client
pinged by client
pinged by client
pinged by client
C:\Program Files\Java\jdk1.6.0\bin>

RESULT:

Thus the Ping command program was demonstrated using java simulator.
SOURCE CODE:
TALKSERVER:
import java.io.*;
import java.net.*;
public class talkserver
{
public static void main(String[] args)throws IOException
{
try
{
ServerSocket server=new ServerSocket(2000);
Socket client=server.accept();
System.out.println("connection established..");
DataInputStream dis=new DataInputStream(client.getInputStream());
PrintStream ps=new PrintStream(client.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s="";
int i=0;
while(i<10)
{
System.out.println("from client:"+dis.readLine());
System.out.println("To client");
s=br.readLine();
if(s.equals("end"))
break;
}
ps.close();
dis.close();
br.close();
client.close();
server.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
EXPT NO :6c
DATE :
TALK COMMANDS

AIM:

To send and receive message from client and server using connection oriented service.

SOFTWARE REQUIRED:

S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. Java

ALGORITHM:

SERVER:

1. Create server and client sockets.


2. Use input streams to get the message from user.
3. Use output stream to send message to the client.
4. Wait for client to display this message and write a new one to be displayed by the server.
5. Display message given at client using input streams read from socket.
6. If in the message given by server or client, the word end is encountered, exit both the
programs.
CLIENT:

1. Create a client socket that connects to the required host and port.
2. Using input streams read message given by server and print it.
3. Using input streams, get message from user to be given to the server.
4. Use output streams to write message to the server.
TALKCLIENT:
import java.io.*;
import java.net.*;
public class talkclient
{
public static void main(String[] args)
{
try
{
Socket server=new Socket("localhost",2000);
System.out.println("connected..");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataInputStream dis=new DataInputStream(server.getInputStream());
PrintStream ps=new PrintStream(server.getOutputStream());
int i=0;
String s=" ";
while(i<10)
{
System.out.println("\n Message to server:");
s=br.readLine();
ps.println(s);
if(s.equals("end"))
break;
System.out.println("\n Message from server:"+dis.readLine());
}
ps.close();
dis.close();
br.close();
}
catch(Exception e)
{
}
}
}
OUTPUT
TALKSERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac talkerver.java
D:\Chitra\Java>java talkserver

TALK SERVER
----------------------
Node successfully connected.
Message Received
The Message =HAI
HI..
Message sent successfully
Message Received
The Message =HOW ARE YOU
I AM FINE
Message sent successfully
Message Received
The Message =K BYE
end
D:\Chitra\Java>

TALKCLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac talkclient.java
D:\Chitra\Java>java talkclient

TALK CLIENT
--------------------
send message to server: HAI
message sent successfully
message received from talkserver: HI..
send message to server: HOW ARE YOU
message sent successfully
message received from talkserver: I AM FINE
send message to server: K BYE
message sent successfully
send message to server:
end
D:\Chitra\Java>
RESULT:

Thus the Talk command program was demonstrated using java simulator.
CSMA CA Code:

set ns [new Simulator]


#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files
set file1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace file
set file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam &
exit 0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n1 color red
$n1 shape box
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail
MAC/Csma/Cd Channel]
# $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
# $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail
#Give node position (for NAM)
# $ns duplex-link-op $n0 $n2 orient right-down
# $ns duplex-link-op $n1 $n2 orient right-up
# $ns simplex-link-op $n2 $n3 orient right
# $ns simplex-link-op $n3 $n2 orient left
# $ns duplex-link-op $n3 $n4 orient right-up
# $ns duplex-link-op $n3 $n5 orient right-down
#Set Queue Size of link (n2-n3) to 10
# $ns queue-limit $n2 $n3 20
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
EXPT NO :7
DATE :

CSMA CA vs CSMA CD PROTOCOL


AIM:
To create a scenario and study the performance of network with CSMA / CA protocol and
compare with CSMA/CD protocols.

SOFTWARE REQUIREMENT:

SL.NO FACILITIES/MATERIALS REQUIRED

1. Computer

2. Network Simulator 2

PROCEDURE

1. Start the program complier


2. Initiatilise the headers and assign node parameters
3. Initiate collision sequence automatically or manually
4. Assign detection algorithmic equations for the node transmissions
5. Print the message onto window.

BLOCK DIAGRAM:
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the
# tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file} {
global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
set wnd [$tcpSource set window_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet drop\""
# PPP
$ns at 125.0 "finish"
$ns run
PROCEDURE:
CSMA/CA

1. Start the program complier


2. Initiatilise the headers and assign node parameters
3. Initiate collision sequence automatically or manually
4. Assign detection algorithmic equations for the node transmissions
5. Print the message onto window
6. Initiate retransmissions after predefined time from point of collision
7. Assign routines for repeated tries if needed
CSMA CD Code:

set opt(vtr) "lan06.tr"


set opt(timev) 0.04; #for recording
set opt(tr) "out"
set opt(namtr) "out.nam"
set opt(stop) 2.5
set opt(tstep) 0.06; #for varying the interval
set opt(nodepairs) 10
set opt(qsize) 300
set opt(intrvlstart) 0.02
set opt(intrvlstep) 0.0008
set opt(bw) 10Mb
set opt(delay) 1ms
set opt(ll) LL
set opt(ifq) Queue/DropTail
set opt(mac) Mac/802_3
set opt(chan) Channel
set opt(agt) UDP
set opt(sink) LossMonitor
set opt(app) Traffic/CBR;
proc finish {} {
global ns opt trfd
$ns flush-trace
close $trfd
exit 0
}
proc create-trace {} {
global ns opt
set vtr [open $opt(vtr) w]
puts $vtr "time interval rcvpkts lostpkts"; #heading
set trfd [open $opt(tr) w]
$ns trace-all $trfd
if {$opt(namtr) != ""} {
$ns namtrace-all [open $opt(namtr) w]
}
set trlist [list $trfd $vtr]
return $trlist
}
proc create-topology {} {
global ns opt
global lan node source node0
set num [expr $opt(nodepairs)*2]
for {set i 0} {$i < $num} {incr i} {
set node($i) [$ns node]
lappend nodelist $node($i)
}
set lan [$ns newLan $nodelist $opt(bw) $opt(delay) \
-llType $opt(ll) -ifqType $opt(ifq) \
-macType $opt(mac) -chanType $opt(chan)]
set node0 [$ns node]; #only for a better lay-out
$ns duplex-link $node0 $node(0) 2Mb 1ms DropTail
$ns duplex-link-op $node0 $node(0) orient right
}
proc record {} {
global ns traffic sink vtr opt
set time $opt(timev)
set rcvps 0
set lps 0
set intval 0
for {set i 0} {$i<$opt(nodepairs)} {incr i} {
set rcvps [expr $rcvps+[$sink($i) set npkts_]]
set lps [expr $lps+[$sink($i) set nlost_]]
set intval [expr $intval+[$traffic($i) set interval_]]
}
set rcvps [expr $rcvps/$opt(nodepairs)] ; #compute the meanvalues
set lps [expr $lps/$opt(nodepairs)]
set intval [expr $intval/$opt(nodepairs)]
set nw [$ns now]
puts $vtr "$nw $intval $rcvps $lps"
for {set i 0} {$i<$opt(nodepairs)} {incr i} {
$sink($i) set npkts_ 0; #reset
$sink($i) set nlost_ 0
}
$ns at [expr $nw+$time] "record"
}
proc schedule-interval {} {
global ns traffic rng opt
set oldintrvl [$traffic(0) set interval_]
set factor [expr $opt(intrvlstep)/10];
#certain factor for computing the random-range

if {[expr ($oldintrvl-$opt(intrvlstep))>$opt(intrvlstep)]} {
$traffic(0) set interval_ [expr $oldintrvl-$opt(intrvlstep)]
for {set i 1} {$i<$opt(nodepairs)} {incr i} {
set rndmdelta [$rng uniform $factor [expr $factor*9]];
#random-range for randomizing the intervals
$traffic($i) set interval_ [expr $oldintrvl-
$opt(intrvlstep)+$rndmdelta];
#lessen interval
}
}
set nw [$ns now]
$ns at [expr $nw+$opt(tstep)] "schedule-interval"
}
## MAIN ##
global srcdst agent traffic sink vtr rng
set rng [new RNG]
$rng seed 0
set ns [new Simulator]
set traces [create-trace]
set trfd [lindex $traces 0]
set vtr [lindex $traces 1]; #important trace file
create-topology
#connections
for {set j 0} {$j < $opt(nodepairs)} {incr j} {
#connect even nodes with odd nodes
set srcdst [$ns create-connection-list $opt(agt) $node([expr $j*2]) \
$opt(sink) $node([expr $j*2+1]) 0]
set agent($j) [lindex $srcdst 0]
set sink($j) [lindex $srcdst 1]
puts "$j : [expr $j*2] -> [expr $j*2+1]"
set traffic($j) [$agent($j) attach-app $opt(app)]
$traffic($j) set packetSize_ $opt(qsize); #set start values
$traffic($j) set interval_ $opt(intrvlstart)
}
#data-recording
$ns at 0.0 "record"

#time-schedule
set now [$ns now]
for {set i 0} {$i<$opt(nodepairs)} {incr i} {
$ns at 0.0 "$traffic($i) start"
}
$ns at 0.0 "schedule-interval"
$ns at [expr $now+$opt(stop)] "finish"
$ns run
RESULT:

Thus the scenario was created and the performance of network with CSMA / CA protocol was
compared with CSMA/CD protocols.
PROGRAM

STAR TOPOLOGY

#Create a simulator object


set ns [new Simulator]

#Open the nam trace file


set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam on the trace file
exec nam out.nam &
exit0
}

#Create four nodes


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Change the shape of center node in a star topology


$n0 shape square

#Create links between the nodes


$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n0 $n5 1Mb 10ms DropTail

#Create a TCP agent and attach it to node n0


set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
EXPT NO :8
DATE :
Network Topology - Star, Bus, Ring

AIM

To implement the network topologies using Network Simulator.

APPARATUS REDUIRED

S.No Apparatus
1. PC
2. Network Simulator

PROCEDURE

1. Create the Simulator object

2. Open the nam trace file

3. Define a finish procedure

4. Create the required number of nodes

5. Create the links between all nodes

6. Create the traffic agents (UDP/TCP) & set the parameters

7. Create & schedule the CBR Traffic agents

8. Call the finish procedure & introduce the random errors between the links

9. Save the file & Run the simulation

10. Observe the traffic & Plot the behavior with respect to different topologies.
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0
# Create a CBR traffic source and attach it to tcp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

BUS TOPOLOGY

#Create a simulator object


set ns [new Simulator]

#Open the nam trace file


set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

#Create Lan between the nodes


set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel]

#Create a TCP agent and attach it to node n0


set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
Star Topology
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents


$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Run the simulation


$ns run

RING TOPOLOGY

#Create a simulator object


set ns [new Simulator]

#Open the nam trace file


set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam on the trace file
exec nam out.nam &
exit0
}

#Create four nodes


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
Bus Topology
#Create links between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n0 1Mb 10ms DropTail

#Create a TCP agent and attach it to node n0


set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents


$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Run the simulation


$ns run
Ring Topology

RESULT:

Thus the implementation of network topologies using Network Simulator was performed.
Program : Implementation of distance vector routing algorithm
#distvect.tcl
set ns [new Simulator]
#Tell the simulator to use dynamic routing
$ns rtproto DV
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define finish procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam a out.nam &
exit 0
}
#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
#Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.0 "$n(0) label Source"
$ns at 0.0 "$n(3) label Destination"
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
NS2 code for Implementation of Distance Vector routing algorithm

Exercise No.9
Title : Implementation of Distance Vector routing algorithm
Date :

Aim:
To implement distance vector routing algorithm using Network Simulator.

Software required:
S.No Component
1. PC
2. Network Simulator NS2

Algorithm :
Create a simulator object and specify the simulator to use dynamic routing or
Link state routing
Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
Create seven nodes that forms a network numbered from 0 to 6
Create duplex links between the nodes and add Orientation to the nodes for
setting a LAN topology
Setup UDP Connection between nodes 0 and 3 and apply CBR Application over
UDP
Label the nodes 0 and 3 as Source and destination
Schedule route model and events to achieve distance vector routing algorithm
Snapshots:

RESULT:

Thus the implementation of distance vector routing algorithm was performed using Network
Simulator.
NS2 code for Implementation of Link state routing algorithm

#linkstate.tcl
set ns [new Simulator]
#Tell the simulator to use dynamic routing
$ns rtproto LS
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define finish procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam a out.nam &
exit 0
}
#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
#Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.0 "$n(0) label Source"
$ns at 0.0 "$n(3) label Destination"
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
Exercise No.10
Title : Implementation of Link state routing algorithm
Date :

Aim:

To implement Link state routing algorithm using Network Simulator.

Software required:
S.No Component
3. PC
4. Network Simulator NS2

Algorithm :
Create a simulator object and specify the simulator to use Link state routing
Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
Create seven nodes that forms a network numbered from 0 to 6
Create duplex links between the nodes and add Orientation to the nodes for
setting a LAN topology
Setup UDP Connection between nodes 0 and 3 and apply CBR Application over
UDP
Label the nodes 0 and 3 as Source and destination
Schedule route model and events to achieve Link state routing algorithm
Snapshots:

RESULT:
Thus the implementation of link state routing algorithm was performed using Network
Simulator.
TCP vs UDP Congestion Control using NS2 Program in .tcl:

#create simulator
set ns [new Simulator]

set nr [open thro.tr w]


$ns trace-all $nr
set nf [open thro.nam w]

$ns namtrace-all $nf


proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
#to create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

# to create the link between the nodes with bandwidth, delay and
queue
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 200ms DropTail
$ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 30ms DropTail

# Sending node is 0 with agent as Reno Agent


set tcp1 [new Agent/TCP/Reno]
$ns attach-agent $n0 $tcp1

# receiving (sink) node is n4


set sink1 [new Agent/TCPSink]
$ns attach-agent $n4 $sink1

# establish the traffic between the source and sink


$ns connect $tcp1 $sink1

# Setup a FTP traffic generator on "tcp1"


set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP

# start/stop the traffic


$ns at 0.1 "$ftp1 start"
$ns at 75.0 "$ftp1 stop"
Exercise No.11
Title : Study& Simulation of Congestion Control using NS2
Date :

Aim:

To study and simulate the congestion control using Network Simulator.

Software required:
S.No Component
1. PC
2. Network Simulator NS2

Input parameters:
Router: 3 to 7
Connectivity between the routers to be given
Source / Destination routers to be selected

Procedure:
Open ns2 working window and type in the codes
Assign source and destinations for transmission and reception of data
Define TCP connections and UDP connections separately
Define animation window parameters to simulate data flow in slow styles
Save the file as congestion_control.tcl and run the code
$ns at 80.0 "$ftp1 start"
$ns at 95.0 "$ftp1 stop"

# Set simulation end time


$ns at 100.0 "finish"
# procedure to plot the congestion window
proc plotWindow {tcpSource outfile} {
global ns
set now [$ns now]
set cwnd [$tcpSource set cwnd_]

# the data is recorded in a file called congestion.xg (this can be


plotted # using xgraph or gnuplot. this example uses xgraph to plot
the cwnd_
puts $outfile "$now $cwnd"
$ns at [expr $now+0.1] "plotWindow $tcpSource $outfile"
}

set outfile [open "congestion.xg" w]


$ns at 0.0 "plotWindow $tcp1 $outfile"
proc finish {} {
exec xgraph congestion.xg -bg "white" -fg "red" -zg "black" -lw
"2" -t "Congestion Window" -x "Simulation Time (Seconds)" -y
"Congestion Window" -geometry 600x600 &
exit 0
}
# Run simulation
$ns run
Congestion window snapshot:

RESULT:
Thus the congestion control algorithm was studied and simulated using Network simulator.