CN Lab Manual
CN Lab Manual
INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
INDEX
Page
Exp Name of the Experiment
No
No
1
1 Learning to use basic commands and traceroute PDUs
6
2 Download a web page using TCP sockets
Applications using TCP sockets: Echo client and echo 10
3
server
4 Applications using TCP sockets: Chat 14
AIM:
To learn to use commands like tcpdump, netstat, ifconfig, nslookup and trace
route ping.
COMMANDS:
1. Tcpdump:
Display traffic between 2 hosts:
To display all traffic between two hosts (represented by variables host1 and
host2): # tcpdump host host1 and host2
Display traffic from a source or destination host only:
To display traffic from only a source (src) or destination (dst)
host: # tcpdump src host
# tcpdump dst host
Display traffic for a specific protocol
Provide the protocol as an argument to display only traffic for a specific
protocol, for example tcp, udp, icmp, arp
# tcpdump protocol
For example to display traffic only for the tcp
traffic: # tcpdump tcp
Filtering based on source or destination
port To filter based on a source or
destination port: # tcpdump src port ftp
# tcpdump dst port http
2. Netstat
Netstat is a common command line TCP/IP networking available in most versions
of Windows, Linux, UNIX and other operating systems. Netstat provides information
and statistics about protocols in use and current TCP/IP network connections.
The Windows help screen (analogous to a Linux or UNIX for netstat reads as
follows:
Displays protocol statistics and current TCP/IP network connections.
#netstat
3. ipconfig
In Windows, ipconfig is a console application designed to run from the Windows
command prompt. This utility allows you to get the IP address information of a
Windows computer.
Using ipconfig
From the command prompt, type ipconfig to run the utility with default
options. The output of the default command contains the IP address, network
mask, and gateway for all physical and virtual network adapter.
#ipconfig
4. nslookup
The nslookup (which stands for name server lookup) command is a network
utility program used to obtain information about internet servers. It finds name
server information for domains by querying the Domain Name System. The
nslookup command is a powerful tool for diagnosing DNS problems. You know
you're experiencing a DNS problem when you can access a resource by specifying
its IP address but not its DNS name.
#nslookup
5. Trace route:
Traceroute uses Internet Control Message Protocol (ICMP) echo packets with
variable time to live (TTL) values. The response time of each hop is calculated. To
guarantee accuracy, each hop is queried multiple times (usually three times) to
better measure the response of that particular hop. Traceroute is a network
diagnostic tool used to track the pathway taken by a packet on an IP network from
source to destination.
Traceroute also records the time taken for each hop the packet makes during its
route to the destination. Traceroute uses Internet Control Message Protocol (ICMP)
echo packets with variable time to live (TTL) values. The response time of each hop
is calculated. To guarantee accuracy, each hop is queried multiple times (usually
three times) to better measure the response of that particular hop.
Traceroute sends packets with TTL values that gradually increase from packet to
packet, starting with TTL value of one. Routers decrement TTL values of packets by
one when routing and discard packets whose TTL value has reached zero, returning
the ICMP error message ICMP Time Exceeded. For the first set of packets, the first
router receives the packet, decrements the TTL value and drops the packet
because it then has TTL value zero. The router sends an ICMP Time Exceeded
message back to the source. The next set of packets are given a TTL value of two,
so the first router forwards the packets, but the second router drops them and
replies with ICMP Time Exceeded.
Proceeding in this way, traceroute uses the returned ICMP Time Exceeded
messages to build a list of routers that packets traverse, until the destination is
reached and returns an ICMP Echo Reply message. With the tracert command
shown above, we're asking tracert to show us the path from the local computer all
the way to the network device with the hostname
www.google.com.
#tracert google.com
6. Ping:
The ping command sends an echo request to a host available on the network.
Using this command, you can check if your remote host is responding well or not.
Tracking and isolating hardware and software problems. Determining the status of
the network and various foreign hosts. The ping command is usually used as a
simple way to verify that a computer can communicate over the network with
another computer or network device. The ping command operates by sending
Internet Control Message Protocol (ICMP) Echo Request messages to the
destination computer and waiting for a response
# ping172.16.6.2
RESULT:
Thus the various networks commands like tcpdump, netstat, ifconfig, nslookup
and traceroute ping are executed successfully
AIM:
To write a java program for socket for HTTP for web page upload and download.
ALGORITHM:
Client:
1. Start.
2. Create socket and establish the connection with the server.
3. Read the image to be uploaded from the disk
4. Send the image read to the server
5. Terminate the connection
6. Stop.
Server:
1. Start
2. Create socket, bind IP address and port number with the created socket
and make server a listening server.
3. Accept the connection request from the client
4. Receive the image sent by the client.
5. Display the image.
6. Close the connection.
7. Stop
PROGRAM:
Client:
Import
javax.swing.*;
import java.net.*;
import
java.awt.image.*;
import
javax.imageio.*;
import java.io.*;
import
java.awt.image.BufferedImage;
import
java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import
javax.imageio.ImageIO;
Socket soc;
BufferedImage img
= null;
soc = new Socket("localhost", 4000); System.out.println("Client is
running.");
try {
System.out.println("Reading image from disk. ");
img=ImageIO.read(new
File("digital_image_processing.jpg"));
ByteArrayOutputStream baos = new
ByteArrayOutputStream(); ImageIO.write(img, "jpg",
b rray(); baos.close();
a System.out.println("Sending image to server."); OutputStream out =
o soc.getOutputStream(); DataOutputStream dos = new
s DataOutputStream(out); dos.writeInt(bytes.length);
) dos.write(bytes, 0, bytes.length); System.out.println("Image sent to
; server. "); dos.close();
baos.flush( out.close();
);
b
y
t
e
[
]
b
y
t
e
s
b
a
o
s
.
t
o
B
y
t
e
A
8 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
soc.close();
}
soc.close();
}
}
SERVER:
import
java.net.*;
import
java.io.*;
import
java.awt.image.*;
import
javax.imageio.*;
import
javax.swing.*;
class Server {
public static void main(String args[]) throws Exception { ServerSocket server
= null;
Socket socket;
server = new ServerSocket(4000); System.out.println("Server Waiting for
image"); socket = server.accept(); System.out.println("Client connected.");
InputStream in = socket.getInputStream(); DataInputStream dis = new
DataInputStream(in); int len = dis.readInt();
System.out.println("Image Size: " + len / 1024 + "KB"); byte[] data = new
byte[len];
dis.readFully(data);
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian); JFrame f = new
JFrame("Server");
ImageIcon icon = new ImageIcon(bImage); JLabel l = new
JLabel();
l.setIcon(ic
on);
f.add(l);
f.pack();
f.setVisible(true);
}
}
OUTPUT:
When you run the client code, following output screen would appear on client
side.
RESULT:
Thus the socket program for HTTP for web page upload and download
was developed and executed successfully.
Ex. No: 3
Applications using TCP sockets: Echo client and echo ser
AIM:
To write a java program for application using TCP Sockets Link.
ALGORITHM:
Client:
1. Start
2. Create the TCP socket
3. Establish connection with the server
4. Get the message to be echoed from the user
5. Send the message to the server
6. Receive the message echoed by the server
7. Display the message received from the server
8. Terminate the connection
9. Stop
Server:
1. Start
2. Create TCP socket, make it a listening socket
3. Accept the connection request sent by the client for connection establishment
4. Receive the message sent by the client
5. Display the received message
6. Send the received message to the client from which it receives
7. Close the connection when client initiates termination and server
becomes a listening server, waiting for clients.
8. Stop.
PROGRAM:
EServer.java
import java.net.*; import java.io.*;
System.out.println(e);
}
try {
c = s.accept();
is = new DataInputStream(c.getInputStream());
EClient.java
import java.net.*; import java.io.*;
}
try {
OUTPUT:
Server:
C:\Program Files\Java\jdk1.5.0\bin>javac
EServer.java C:\Program Files\Java\jdk1.5.0\
bin>java EServer C:\Program Files\Java\jdk1.5.0\
bin>Client
C:\Program Files\Java\jdk1.5.0\bin>javac
EClient.java C:\Program Files\Java\jdk1.5.0\
bin>java EClient Client: Hai Server
Server:Hai
Server Client:
Hello
Server:Hello
Client:end
Server:end
Client:ds
Socket Closed!
RESULT:
Thus the java application program using TCP Sockets was developed and
executed successfully.
Ex. No: 4
Applications using TCP sockets: Chat
AIM:
To write a java program for application using TCP Sockets Link for
Chat Application.
ALGORITHM:
Client:
1. Start
2. Create the UDP datagram socket
3. Get the request message to be sent from
the user 4.Send the request message to the
server
5. If the request message is ―END‖ go to step 10
6. Wait for the reply message from the server 7.Receive the reply message sent by
the server 8.Display the reply message received from the server 9.Repeat the steps
from 3 to 8
10.Stop
Server:
1. Start
2. Create UDP datagram socket, make it a listening
socket 3.Receive the request message sent by the
client
4. If the received message is ―END‖ go to step 10
5. Retrieve the client‘s IP address from the request message
received 6.Display the received message
7.Get the reply message from the
user 8.Send the reply message to
the client
PROGRAM:
UDPserver.java
import java.io.*; import java.net.*;
class UDPserver {
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024]; public static int clientport = 789,
serverport = 790;
UDPclient.java
import java.io.*; import java.net.*;
class UDPclient {
public static DatagramSocket ds;
public static int clientport = 789, serverport = 790;
public static void main(String args[]) throws Exception { byte buffer[] = new
byte[1024];
ds = new DatagramSocket(serverport);
BufferedReader dis = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("server waiting");
InetAddress ia = InetAddress.getLocalHost(); while (true) {
System.out.println("Client:"); String str = dis.readLine();
if (str.equals("end")) break;
buffer = str.getBytes();
ds.send(new DatagramPacket(buffer, str.length(), ia, clientport));
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
ds.receive(p);
String psx = new String(p.getData(), 0, p.getLength());
System.out.println("Server:" + psx);
}
}
}
OUTPUT:
Server
C:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.java C:\
Program Files\Java\jdk1.5.0\bin>java UDPserver
press ctrl+c to quit the program Client:Hai Server Server:Hello Client
Client:How are
You Server:I am
Fine
Client
C:\Program Files\Java\jdk1.5.0\bin>javac
UDPclient.java C:\Program Files\Java\jdk1.5.0\
bin>java UDPclient server waiting
Client:Hai Server
Server:Hello Clie
Client:How are
You Server:I am
Fine Client:end
RESULT:
Thus the java application program using TCP Sockets was developed and
executed successfully.
Ex. No: 5
Applications using TCP sockets: File Transfer
AIM:
To write a java program for file transfer using TCP Sockets.
ALGORITHM:
Server:
1.Import java packages and create class file
server. 2.Create a new server socket and bind
it to the port. 3.Accept the client connection
4.Get the file name and stored into the
BufferedReader. 5.Create a new object class file
and realine.
6.If file is exists then FileReader read the content until EOF is
reached.
7.Stop the program.
Client:
1.Import java packages and create class file
server. 2.Create a new server socket and bind
it to the port. 3.Now connection is established.
4. The object of a BufferReader class is used for storing data content which
has been retrieved from socket object.
5. The connection is
closed. 6.Stop the
program.
PROGRAM:
File Server :
import java.io.BufferedInputStream; import java.io.File;
import java.io.FileInputStream; import java.io.OutputStream;
import java.net.InetAddress;
File Client:
import java.io.BufferedOutputStream; import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress; import java.net.Socket;
OUTPUT:
Server:
E:\nwlab>java FileServer
Sending file ... 9%
complete! Sending file ...
19% complete! Sending file
... 28% complete! Sending
file ... 38% complete!
Sending file ... 47%
Client:
E:\nwlab>client E:\
nwlab>java FileClient
File saved successfully!
E:\nwlab>
RESULT:
Thus the java application program using TCP Sockets was developed and
executed successfully.
23 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
AIM:
To write a java program for DNS application
ALGORITHM:
Server:
1. Start
2. Create UDP datagram socket
3. Create a table that maps host name and IP
address 4.Receive the host name from the
client
5. Retrieve the client‘s IP address from the received
datagram 6.Get the IP address mapped for the host
name from the table. 7. Display the host name and
corresponding IP address
8. Send the IP address for the requested host name to the
client 9.Stop.
Client:
1. Start
2. Create UDP datagram socket.
3. Get the host name from the client
4. Send the host name to the server
5. Wait for the reply from the server
6. Receive the reply datagram and read the IP address for the requested host
name 7.Display the IP address.
8. Stop.
24 22UCS504 – COMPUTER
NETWORKS LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
PROGRAM:
DNS Server.java
import java.io.*; import java.net.*;
clientsocket.send(pack);
DatagramPacket recvpack = new DatagramPacket(receivedata,
receivedata.length); clientsocket.receive(recvpack);
String modified = new String(recvpack.getData()); System.out.println("IP
Address: " + modified); clientsocket.close();
}
}
OUTPUT:
Server:
javac udpdnsserver.java java udpdnsserver
Press Ctrl + C to Quit
Request for host
yahoo.com Request for
host cricinfo.com Request
for host youtube.com
Client:
>javac udpdnsclient.java
>java udpdnsclient
Enter the hostname :
yahoo.com IP Address:
68.180.206.184
>java udpdnsclient
Enter the hostname :
cricinfo.com IP Address:
80.168.92.140
>java udpdnsclient
Enter the hostname :
youtube.com IP Address: Host
Not Found
RESULT:
Thus the java application program using UDP Sockets to implement DNS was
developed and executed successfully.
27 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
AIM:
To write a java program for simulating ARP and RARP protocols using TCP.
ALGORITHM:
Client:
1. Start the program
2. Create socket and establish connection with the server.
3. Get the IP address to be converted into MAC address from the
user. 4.Send this IP address to server.
5.Receive the MAC address for the IP address from the server.
6.Display the received MAC address
7.Terminate the connection
Server:
1. Start the program
2. Create the socket, bind the socket created with IP address and port
number and make it a listening socket.
3. Accept the connection request when it is requested by the client.
4. Server maintains the table in which IP and corresponding MAC
addresses are stored.
5. Receive the IP address sent by the client.
6. Retrieve the corresponding MAC address for the IP address and send it
to the client.
7. Close the connection with the client and now the server becomes a listening
server waiting for the connection request from other clients
8. Stop
PROGRAM:
Client:
import java.io.*; import java.net.*; import java.util.*;
class Clientarp {
public static void main(String args[]) { try {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); Socket clsct = new
Socket("127.0.0.1", 139);
DataInputStream din = new DataInputStream(clsct.getInputStream());
DataOutputStream dout = new
DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical address(IP):");
String str1 = in.readLine(); dout.writeBytes(str1 + '\n'); String str =
din.readLine();
Server:
import java.io.*; import java.net.*; import java.util.*;
class Serverarp {
public static void main(String args[]) { try {
ServerSocket obj = new ServerSocket(139); Socket obj1 =
obj.accept();
while (true) {
din.readLine();
String ip[] = { "165.165.80.80", "165.165.79.1" };
String mac[] = { "6A:08:AA:C2", "8A:BC:E3:FA" };
for (int i = 0; i < ip.length; i++) { if
(str.equals(ip[i])) {
dout.writeBytes(mac[i] + '\n'); break;
}
}
obj.close();
}
} catch (Exception e) {
System.out.println(e);
}
}}}
OUTPUT:
E:\networks>java Serverarp E:\networks>java Clientarp Enter the
Logical address(IP): 165.165.80.80
The Physical Address is: 6A:08:AA:C2
RESULT:
Thus the program for implementing to display simulating ARP protocols was
30 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
AIM:
To write a Program for Reverse Address Resolution Protocol (RARP) using UDP
ALGORITHM:
Client:
1. Start the program
2.Create datagram
socket
3.Get the MAC address to be converted into IP address from the user.
4.Send this MAC address to server using UDP datagram.
5.Receive the datagram from the server and display the corresponding IP
address.
6.Stop
Server:
1. Start the program.
2. Server maintains the table in which IP and corresponding MAC
addresses are stored.
3. Create the datagram socket
4. Receive the datagram sent by the client and read the MAC
address sent. 5.Retrieve the IP address for the received MAC
address from the table.
6.Display the corresponding IP
address. 7.Stop
PROGRAM:
Client:
import java.io.*; import java.net.*; import java.util.*;
class Clientrarp12 {
public static void main(String args[]) { try {
DatagramSocket client = new DatagramSocket(); InetAddress
addr = InetAddress.getByName("127.0.0.1"); byte[] sendbyte =
new byte[1024];
byte[] receivebyte = new byte[1024];
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("Enter the Physical
address (MAC):");
String str = in.readLine(); sendbyte = str.getBytes();
DatagramPacket sender = new DatagramPacket(sendbyte,
sendbyte.length, addr, 1309); client.send(sender);
DatagramPacket receiver = new DatagramPacket(receivebyte,
receivebyte.length); client.receive(receiver);
String s = new String(receiver.getData());
System.out.println("The Logical Address is(IP): " +
s.trim()); client.close();
} catch (Exception e)
{ System.out.prin
tln(e);
}
}
}
Server:
import java.io.*; import java.net.*; import java.util.*;
class Serverrarp12 {
public static void main(String args[]) { try {
33 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
}
}
break;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT:
I:\ex>java
Serverrarp12 I:\
ex>java Clientrarp12
34 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
RESULT:
Thus the program for implementing to display simulating RARP protocols was
executed successfully and output is verified.
Aim:
To study and simulate congestion control algorithms using NS2 to understand their
behavior under different network conditions.
cd ~/ns-allinone-2.xx/ns-2.xx/
Write a TCL script to define the simulation parameters. Below is an example script to simulate
TCP congestion control:
# Create nodes
set node1 [$ns node]
set node2 [$ns node]
Analyzing Results:
Use the generated trace file (e.g., out.tr) to analyze the simulation results. You can
visualize the output using tools like xgraph:
xgraph out.tr
Modify and Experiment:
Modify the script to test different congestion control algorithms or adjust
network parameters (bandwidth, delay, etc.) to observe changes in
performance.
RESULT:
Thus we have Studied Network simulator (NS) and Simulation of Congestion
Control Algorithms using NS.
AIM:
To simulate the performance of TCP/UDP using NS2.
PROGRAM:
set ns [new Simulator]
$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow set n0 [$ns node] set n1 [$ns node] set n2 [$ns
node] set n3 [$ns node] set f [open tcpout.tr w]
$ns trace-all $f
set nf [open tcpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
38 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
$ns duplex-link-op $n2 $n3 queuePos 0.5 set tcp [new Agent/TCP]
$tcp set class_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n3 $sink"
$ns at 3.0 "finish" proc
finish {} { global ns f
nf
$ns flush-trace
close $f close
$nf
puts "Running nam.."
exec xgraph tcpout.tr -geometry 600x800 & exec
nam tcpout.nam & exit 0
}
$ns run
OUTPUT:
UDP PERFORMANCE:
ALGORITHM:
1. Create a Simulator object.
2. Set routing as dynamic.
3. Open the trace and nam trace files.
4. Define the finish procedure.
5. Create nodes and the links between them.
6. Create the agents and attach them to the nodes.
7. Create the applications and attach them to the UDP agent.
8. Connect udp and null agents.
9. Run the simulation.
PROGRAM:
set ns [new Simulator]
$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow set n0 [$ns node] set n1 [$ns node] set n2 [$ns
node] set n3 [$ns node] set f [open udpout.tr w]
$ns trace-all $f
set nf [open udpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5 set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0 set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 0
RESULT:
Thus the study of TCP/UDP performance is done successfully.
AIM:
To simulate the Distance vector routing protocols using NS2.
ALGORITHM:
1. Create a simulator object
2. Set routing protocol to Distance Vector routing
3. Trace packets on all links onto NAM trace and text trace file
4. Define finish procedure to close files, flush tracing and run NAM
5. Create eight nodes
6. Specify the link characteristics between nodes
7. Describe their layout topology as a octagon
8. Add UDP agent for node n1
9. Create CBR traffic on top of UDP and set traffic parameters.
10. Add a sink agent to node n4
11. Connect source and the sink
12. Schedule events as follows:
a. Start traffic flow at 0.5
b. Down the link n3-n4 at 1.0
c. Up the link n3-n4 at 2.0
d. Stop traffic at 3.0
e. Call finish procedure at 5.0
13. Start the scheduler
14. Observe the traffic route when link is up and down
15. View the simulated events and trace file analyze it
16. Stop
42 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
PROGRAM:
#Distance vector routing protocol – distvect.tcl #Create a simulator object
set ns [new Simulator]
#Use distance vector routing
$ns rtproto DV
#Open the nam trace file set nf [open out.nam w]
$ns namtrace-all $nf #
Open tracefile set nt [open
trace.tr w]
$ns trace-all $nt
#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 8 nodes set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
set n4 [$ns node] set n5 [$ns node] set n6 [$ns node] set n7 [$ns node]
set n8 [$ns node]
# Specify link characterestics
$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 $n6 1Mb 10ms DropTail
$ns duplex-link $n6 $n7 1Mb 10ms DropTail
OUTPUT:
$ ns distvect.tcl
RESULT:
Thus the simulation for Distance vector routing protocols was done using NS2.
AIM:
To simulate the Link state routing protocols using NS2.
ALGORITHM:
1. Create a Simulator object.
2. Set routing as dynamic.
3. Open the trace and nam trace files.
4. Define the finish procedure.
5. Create nodes and the links between them.
6. Create the agents and attach them to the nodes.
7. Create the applications and attach them to the udp agent.
8. Connect udp and null.
9. At 1 sec the link between node 1 and 2 is broken.
10. At 2 sec the link is up again.
11. Run the simulation.
PROGRAM:
set ns [new Simulator]
$ns rtproto LS
set nf [open linkstate.nam w]
$ns namtrace-all $nf
set f0 [open linkstate.tr w]
$ns trace-all $f0 proc
finish {} { global ns
f0 nf
$ns flush-
trace
close $f0
close $nf
48 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
exec nam linkstate.nam & exit 0
}
for {set i 0} {$i <7} {incr i}
{ set n($i) [$ns node]
}
for {set i 0} {$i <7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $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.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
OUTPUT:
RESULT:
Thus the Link state routing protocols was done using NS2.
Evaluation
Simulation tool
AIM:
To write a ns2 program for implementing unicast routing protocol.
PROGRAM:
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2
Red #Open
the Trace file
set file1 [open out.tr 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 3
}
# Next line should be commented out to have the static routing
$ns rtproto
DV #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]
OUTPUT:
AIM:
To write a ns2 program for implementing multicasting routing protocol.
ALGORITHM:
1. Start the program.
2. Declare the global variables ns for creating a new simulator.
3. Set the color for packets.
4. Open the network animator file in the name of file2 in the write mode.
5. Open the trace file in the name of file 1 in the write mode.
6. Set the multicast routing protocol to transfer the packets in network.
7. Create the multicast capable no of nodes.
8. Create the duplex-link between the nodes including the delay time,
bandwidth and
dropping queue mechanism.
9. Give the position for the links between the nodes.
10. Set an udp connection for source node.
11. Set the destination node, port and random false for the source and
destination
files.
12. Setup a traffic generator CBR for the source and destination files.
13. Down the connection between any nodes at a particular time.
14. Create the receive agent for joining and leaving if the nodes in the group.
15. Define the finish procedure.
16. In the definition of the finish procedure declare the global variables.
17. Close the trace file and namefile and execute the network animation file.
18. At the particular time call the finish procedure.
19. Stop the program.
PROGRAM:
# Create scheduler
#Create an event scheduler wit multicast turned on set ns [new
Simulator -multicast on] #$ns multicast
#Turn on Tracing
set tf [open output.tr w]
$ns trace-all $tf
# Turn on nam
Tracing set fd
[open mcast.nam
w]
$ns namtrace-
all $fd #
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] set
n6 [$ns
node] set
n7 [$ns
node] #
Create links
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
#post-processing
$ns at 10.0 "finish" proc finish {}
{
global ns tf
$ns flush-
trace
close $tf
exec nam mcast.nam & exit 0
}
# For nam
#Colors for packets from two mcast groups
$ns color 10 red
$ns color 11 green
$ns color 30 purple
$ns color 31 green
# Group 0 source
$udp0 set fid_ 10
$n0 color red
$n0 label "Source 1"
# Group 1 source
$udp1 set fid_ 11
$n1 color green
$n1 label "Source 2"
$n5 label "Receiver 1"
$n5 color blue
$n6 label "Receiver 2"
$n6 color blue
$n7 label "Receiver 3"
$n7 color blue
#$n2 add-mark
m0 red #$n2
delete-mark
m0"
# Animation rate
$ns set-animation-rate 3.0ms
$ns run
OUTPUT:
RESULT:
Thus the case study about the different routing algorithms to select the network
path with its optimum and economical during data transfer is done.
AIM:
To implement error checking code using java.
ALGORITHM:
1. Start the Program
2. Given a bit string, append 0S to the end of it (the number of 0s is the
same as the degree of the generator polynomial) let B(x) be the polynomial
corresponding to B.
3. Divide B(x) by some agreed on polynomial G(x) (generator polynomial)
and determine the remainder R(x). This division is to be done using Modulo 2
Division.
4. Define T(x) = B(x) –R(x)
5. (T(x)/G(x) => remainder 0)
6. Transmit T, the bit string corresponding to T(x).
7. Let T‘represent the bit stream the receiver gets and T‘(x) the associated
polynomial. The receiver divides T1(x) by G(x). If there is a 0 remainder, the
receiver concludes T = T‘ and no error occurred otherwise, the receiver
concludes an error occurred and requires a retransmission
8. Stop the
Program PROGRAM:
import java.io.*;
class crc_gen {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int[] data;
64 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
int[] div;
int[] dvisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length; System.out.println("Enter number
of data bits : "); data_bits = Integer.parseInt(br.readLine());
data = new int[data_bits]; System.out.println("Enter data bits : "); for
(int i = 0; i < data_bits; i++)
data[i] = Integer.parseInt(br.readLine()); System.out.println("Enter
number of bits in divisor : "); divisor_bits =
Integer.parseInt(br.readLine());
divisor = new int[divisor_bits]; System.out.println("Enter Divisor bits : ");
for (int i = 0; i < divisor_bits; i++)
divisor[i] = Integer.parseInt(br.readLine()); System.out.print("Data
bits are : ");
for (int i = 0; i < data_bits; i++) System.out.print(data[i]);
System.out.println(); System.out.print("divisor bits are : "); for (int i =
0; i < divisor_bits; i++)
System.out.print(divisor[i]); System.out.println();
tot_length = data_bits + divisor_bits - 1; div = new int[tot_length];
rem = new int[tot_length]; crc = new int[tot_length];
/* CRC GENERATION */
for (int i = 0; i < data.length; i++) div[i] = data[i];
System.out.print("Dividend (after appending 0's) are : "); for (int i = 0;
i < div.length; i++)
System.out.print(div[i]); System.out.println();
for (int j = 0; j < div.length; j++) { rem[j] = div[j];
}
rem = divide(div, divisor, rem); for (int i = 0; i < div.length; i++) {
// append dividend and remainder crc[i]=(div[i]^rem[i]);
}
System.out.println(); System.out.println("CRC code : "); for (int i = 0; i
< crc.length; i++)
System.out.print(crc[i]);
/* ERROR DETECTION */
System.out.println();
66 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
System.out.println("Enter CRC code of " + tot_length + " bits : "); for (int i
= 0; i < crc.length; i++)
crc[i] = Integer.parseInt(br.readLine()); System.out.print("crc bits are :
");
for (int i = 0; i < crc.length; i++) System.out.print(crc[i]);
System.out.println();
for (int j = 0; j < crc.length; j++) { rem[j] = crc[j];
}
rem = divide(crc, divisor, rem);
for (int i = 0; i < rem.length; i++) { if (rem[i] != 0) {
System.out.println("Error"); break;
}
if (i == rem.length - 1) System.out.println("No Error");
}
System.out.println("THANK YOU)");
}
static int[] divide(int div[], int divisor[], int rem[]) { int cur = 0;
while (true) {
for (int i = 0; i < divisor.length; i++)
rem[cur + i] = (rem[cur + i] ^ divisor[i]); while (rem[cur]
== 0 && cur != rem.length - 1)
cur++;
if ((rem.length - cur) < divisor.length) break;
}
return rem;
}
}
OUTPUT :
Enter number of data bits :
7
Enter data bits :
1
0
1
1
0
0
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are :
101100100 CRC code :
101100111
Enter CRC code of 9 bits :
10
1
1
0
0
1
0
1
crc bits are : 101100101
Error THANK YOU)
BUILD SUCCESSFUL (total time: 1 minute 34 seconds)
RESULT:
Thus the above program for error checking code using was executed
successfully.
68 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
ALGORITHM:
1. The source node transmits the frames continuously.
2. Each frame in the buffer has a sequence number starting from 1 and
increasing up to the window size.
3. The source node has a window i.e. a buffer to store the frames. This buffer
size is the number of frames to be transmitted continuously.
4. The size of the window depends on the protocol designer.
5. For the first frame, the receiving node forms a positive acknowledgement if
the frame is received without error.
6. If subsequent frames are received without error (up to window size)
cumulative positive acknowledgement is formed.
7. If the subsequent frame is received with error, the cumulative
acknowledgment error-free frames are transmitted. If in the same window two
frames or more frames are received with error, the second and the subsequent
error frames are neglected. Similarly even the frames received without error
after the receipt of a frame with error are neglected.
8. The source node retransmits all frames of window from the first error
frame.
9. If the frames are errorless in the next transmission and if the
acknowledgment is error free, the window slides by the number of error-
free frames being transmitted.
10. If the acknowledgment is transmitted with error, all the frames of window
at source are retransmitted, and window doesn‘t slide.
11. This concept of repeating the transmission from the first error frame in the
window is called as GOBACKN transmission flow control protocol.
69 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
PROGRAM:
#send packets
one by one set ns
[new Simulator]
set n0 [$ns node]
set n1 [$ns
node] set
n2 [$ns
node] set
n3 [$ns
node] set
n4 [$ns
node]
set n5 [$ns node] $n0 color "purple" $n1 color "purple" $n2 color "violet" $n3
color "violet" $n4 color "chocolate" $n5 color "chocolate" $n0 shape box ;
$n1 shape box ; $n2 shape box ; $n3 shape box ; $n4 shape box ; $n5 shape
box ;
$ns at 0.0 "$n0 label SYS0"
$ns at 0.0 "$n1 label SYS1"
$ns at 0.0 "$n2 label SYS2"
$ns at 0.0 "$n3 label SYS3"
$ns at 0.0 "$n4 label SYS4"
$ns at 0.0 "$n5 label SYS5"
set nf [open goback.nam w] $ns namtrace-all $nf set f [open goback.tr w] $ns
trace-all $f
$ns duplex-link $n0 $n2 1Mb 20ms DropTail $ns duplex-link-op $n0 $n2 orient
right-down $ns queue-limit
$n0 $n2 5
$ns duplex-link $n1 $n2 1Mb 20ms DropTail $ns duplex-link-op $n1 $n2 orient
right-up $ns duplex-link $n2
$n3 1Mb 20ms DropTail $ns duplex-link-op $n2 $n3 orient right
$ns duplex-link $n3 $n4 1Mb 20ms DropTail $ns duplex-link-op $n3 $n4 orient
right-up $ns duplex-link $n3
$n5 1Mb 20ms DropTail $ns duplex-link-op $n3 $n5 orient right-down
70 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
OUTPUT:
RESULT:
Thus the Go-back N and Selective Repeat protocols were Simulated and studied.
AIM:
To write an ns2 program for implementing carrier sense multiple access.
ALGORITHM:
1. Start the program.
2. Declare the global variables ns for creating a new simulator.
3. Set the color for packets.
4. Open the network animator file in the write mode.
5. Open the trace file and the win file in the write mode.
6. Transfer the packets in network.
7. Create the capable no of nodes.
8. Create the duplex-link between the nodes including the delay time,
bandwidth and dropping queue mechanism.
9. Give the position for the links between the nodes.
10. Set a tcp connection for source node.
11. Set the destination node using tcp sink.
12. Set the window size and the packet size for the tcp.
13. Set up the ftp over the tcp connection.
14. Set the udp and tcp connection for the source and destination.
15. Create the traffic generator CBR for the source and destination files.
16. Define the plot window and finish procedure.
17. In the definition of the finish procedure declare the global variables.
18. Close the trace file and namefile and execute the network animation file.
19. At the particular time call the finish procedure.
PROGRAM:
set ns [new Simulator]
$ns color 1 blue
$ns color 2 red
set fi1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $fi1
set fi2 [open out.nam w]
$ns namtrace-all $fi2 proc finish {}
{
global ns fi1 fi2
$ns flush-trace close $fi1
close $fi2 exec nam
out.nam & exit 0
}
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
$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] set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetsize_ 552
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
75 22UCS504 – COMPUTER NETWORKS
LABORATORY
Dr. N.G.P. INSTITUTE OF TECHNOLOGY
(An Autonomous Institution)
___________________________________________________________________________________________________________________________
OUTPUT:
RESULT:
Thus the carrier sense multiple access are implemented and executed
successfully.