Professional Documents
Culture Documents
OBJECTIVES
LIST OF EXPERIMENTS
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
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 :
AIM:
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 :
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 :
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
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 :
AIM:
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 :
AIM:
ALGORITHM:
Server
Client
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:
SOFTWARE REQUIRED:
S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. Java
ALGORITHM:
ECHOSERVER:
ECHOCLIENT:
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:
SOFTWARE REQUIRED:
S No. COMPONENT
1. 2 Computers with win-2K / XP and Ethernet port
2. Java
ALGORITHM:
192.168.1.10
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 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:
SOFTWARE REQUIREMENT:
1. Computer
2. Network Simulator 2
PROCEDURE
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
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
AIM
APPARATUS REDUIRED
S.No Apparatus
1. PC
2. Network Simulator
PROCEDURE
8. Call the finish procedure & introduce the random errors between the links
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
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:
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]
# 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
Aim:
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"
RESULT:
Thus the congestion control algorithm was studied and simulated using Network simulator.