Cs3581 CN Lab Manual Elakya
Cs3581 CN Lab Manual Elakya
Manual
Prepared By,
[Type here]
[Type here]
Ex.No.1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and trace route.
Capture ping and trace route PDUs using a network protocol analyzer and examine
Aim:
To use commands like tcpdump, netstat, ifconfig, nslookup and trace route. Capture ping and
trace route PDUs using a network protocol analyzer and examine.
1. Tcpdump
Tcpdump is a command line utility that allows you to capture and analyze network traffic going
through your system.
Procedure
$ which tcpdump
/usr/sbin/tcpdump
$ su
[Type here]
[Type here]
Filtering packets
To filter packets based on protocol, specifying the protocol in the command line. For example, capture
ICMP packets only by using this command:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
06:15:07.800786 IP localhost.localdomain > ec2-54-204-39-132.compute-1.amazonaws.com: ICMP
echo request, id 8180, seq 13, length 64
06:15:08.063488 IP ec2-54-204-39-132.compute-1.amazonaws.com > localhost.localdomain: ICMP
echo reply, id 8180, seq 13, length 64
[Type here]
[Type here]
5 packets captured
5 packets received by filter
0 packets dropped by kernel
2. netstat
netstat (network statistics) is a command line tool for monitoring network connections both incoming
and outgoing as well as viewing routing tables, interface statistics etc.
3. ifconfig
It displays the details of a network interface card like IP address, MAC Address, and the status of a
network interface card
[Type here]
[Type here]
4. nslookup
nslookup (stands for “Name Server Lookup”) is a useful command for getting information from
DNS server. It is a network administration tool for querying the Domain Name System (DNS) to obtain
domain name or IP address mapping or any other specific DNS record.
Non-authoritative answer:
Name: annauniv.edu
Address: 103.70.60.38
Non-authoritative answer:
206.26.217.172.in-addr.arpa name = maa03s23-in-f14.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f14.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f206.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f206.1e100.net.
Non-authoritative answer:
Name: annauniv.edu
Address: 103.70.60.38
annauniv.edu text = "v=spf1 ip4:103.70.60.40 -all"
annauniv.edu mail exchanger = 0 sonic.annauniv.edu.
annauniv.edu
origin = ns.annauniv.edu
mail addr = root.annauniv.edu
serial = 20170907
refresh = 300
[Type here]
[Type here]
retry = 900
expire = 604800
minimum = 86400
annauniv.edu nameserver = ns.annauniv.edu.
Non-authoritative answer:
annauniv.edu nameserver = ns.annauniv.edu.
5. traceroute
The traceroute command is used in Linux to map the journey that a packet of information undertakes
from its source to its destination.
Usage:
traceroute [ -46dFITnreAUDV ] [ -f first_ttl ] [ -g gate, .... ] [ -i device ] [ -m max_ttl ] [ -N squeries ] [ -
p port ] [ -t tos ] [ -l flow_label ] [ -w waittime ] [ -q nqueries ] [ -s src_addr ] [ -z sendwait ] [ --
fwmark=num ] host [ packetlen ]
Options:
-4 Use IPv4
-6 Use IPv6
-d --debug Enable socket level debugging
-F --dont-fragment Do not fragment packets
[Type here]
[Type here]
Capture ping and traceroute PDUs using a network protocol analyzer and examine.
Wireshark is free & Open source network packet analyzer that is used for network analysis,
troubleshooting, etc.
Wireshark is quite similar to tcpdump, the major difference between the two is that Wireshark has a
graphical interface with built-in filtering options, which make it easy to use.
To Open Wireshark
# sudo wireshark
In a konsole execute
# ping www.sudo.com
# traceroute www.google.com
[Type here]
[Type here]
Result:
Thus commands like tcpdump, netstat, ifconfig, nslookup and traceroute was used. Ping and
traceroute PDUs using a network protocol analyzer was captured and examined.
7
[Type here]
[Type here]
Ex.No.2 Write a HTTP web client program to download a web page using TCP sockets
Aim:
To write a program in java to create a HTTP web client program to download a web page
using TCP sockets.
Algorithm:
1. Start the program
2. Read the file to be downloaded from webpage
3. To download an image, use java URL class which can be found under java.net package.
4. The file is downloaded from server and is stored in the current working directory.
5. Stop the program
Program
Download.java
import java.io.*;
import java.net.URL;
public class Download
{
public static void main(String[] args) throws Exception
{
try
{
String fileName = "digital_image_processing.jpg";
String website = "http://tutorialspoint.com/java_dip/images/"+fileName;
System.out.println("Downloading File From: " + website);
URL url = new URL(website);
InputStream inputStream = url.openStream();
OutputStream outputStream = new FileOutputStream(fileName);
byte[] buffer = new byte[2048];
int length = 0;
while ((length = inputStream.read(buffer)) != -1)
{
System.out.println("Buffer Read of length: " + length);
outputStream.write(buffer, 0, length);
[Type here]
[Type here]
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
Output
F:\Abarna\Lab>javac Download.java
F:\ Abarna \Lab>java Download
Downloading File From: http://tutorialspoint.com/java_dip/images/digital_image_processing.jpg
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1097
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1744
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1440
Buffer Read of length: 2048
Buffer Read of length: 2048
[Type here]
[Type here]
Result:
Thus created a program in java for a HTTP web client program to download a web page using
TCP sockets is written and executed successfully.
10
[Type here]
[Type here]
Ex.No.3a APPLICATION USING TCP SOCKETS - ECHO CLIENT AND ECHO SERVER
Aim:
To write a program in Java to implement an applications using TCP Sockets like echo
client and echo server
Algorithm
1. Start the Program
2. In Server
a) Create a server socket and bind it to port.
b) Listen for new connection and when a connection arrives, accept it.
c) Read the data from client.
d) Echo the data back to the client.
e) Close all streams.
f) Close the server socket.
g) Stop.
3. In Client
a) Create a client socket and connect it to the server’s port number.
b) Send user data to the server.
c) Display the data echoed by the server.
d) Close the input and output streams.
e) Close the client socket.
f) Stop.
4. Stop the program
Program
Server.java
import java.net.*;
import java.lang.*;
import java.io.*;
public class Server
{
public static final int PORT = 4000;
public static void main( String args[])
11
[Type here]
[Type here]
{
ServerSocket sersock = null;
Socket sock = null;
try
{
sersock = new ServerSocket(PORT);
System.out.println("Server Started :"+sersock);
try
{
sock = sersock.accept();
System.out.println("Client Connected :"+ sock);
DataInputStream ins = new DataInputStream(sock.getInputStream());
System.out.println(ins.readLine());
PrintStream ios = new PrintStream(sock.getOutputStream());
ios.println("Hello from server");
ios.close();
sock.close();
}
catch(SocketException se)
{
System.out.println("Server Socket problem "+se.getMessage());
}
}
catch(Exception e)
{
System.out.println("Couldn't start " + e.getMessage()) ;
}
System.out.println(" Connection from : " + sock.getInetAddress());
}
12
[Type here]
[Type here]
}
Client.java
import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;
class client
{
public static void main(String args[])
{
Socket sock=null;
DataInputStream dis=null;
PrintStream ps=null;
System.out.println(" Trying to connect");
try
{
sock= new Socket(InetAddress.getLocalHost(),Server.PORT);
ps= new PrintStream(sock.getOutputStream());
ps.println(" Hi from client");
DataInputStream is = new DataInputStream(sock.getInputStream());
System.out.println(is.readLine());
}
catch(SocketException e)
{
System.out.println("SocketException " + e);
}
catch(IOException e)
{
System.out.println("IOException " + e);
}
finally
13
[Type here]
[Type here]
{
try
{
sock.close();
}
catch(IOException ie)
{
System.out.println(" Close Error :" + ie.getMessage());
}
}
}
}
Output
In server window:
F:\ Abarna \Lab>javac Server.java
F:\ Abarna \Lab>java Server
Server Started :ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4000]
Client Connected :Socket[addr=/192.168.1.18,port=1815,localport=4000]
Hi from client
Connection from : /192.168.1.18
In client window:
F:\ Abarna\Lab>javac client.java
F:\ Abarna\Lab>java client
Trying to connect
Hello from server
Result:
Thus a program in Java implemented an applications using TCP Sockets like echo client
and echo server
14
[Type here]
[Type here]
15
[Type here]
[Type here]
PrintWriter toClient;
BufferedReader fromUser, fromClient;
try
{
ServerSocket Srv = new ServerSocket(4000);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept();
System.out.println("Client connected");
toClient = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromClient = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
while(true)
{
CltMsg= fromClient.readLine();
if(CltMsg.equals("end"))
break;
else
{
System.out.println("Server : " +CltMsg);
System.out.print("Message to Client : ");
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
16
[Type here]
[Type here]
Srv.close();
}
catch (Exception E)
{
System.out.println(E.getMessage());
}
}
}
tcpchatclient.java
import java.io.*;
import java.net.*;
class tcpchatclient
{
public static void main(String args[])throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try
{
Clt = new Socket(InetAddress.getLocalHost(),4000);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
System.out.println("Type \"end\" to Quit");
while (true)
{
System.out.print("Message to Server : ");
CltMsg = fromUser.readLine();
17
[Type here]
[Type here]
toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client : " + SrvMsg);
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
}
}
}
Output
In server window:
F:\ Abarna\ Lab>javac tcpchatserver.java
F:\ Abarna\Lab>java tcpchatserver
Server started
Client connected
Server : hai
Message to Client : hello
Client Disconnected
In client window:
F:\ Abarna\Lab>javac tcpchatclient.java
F:\ Abarna\Lab>java tcpchatclient
Type "end" to Quit
Message to Server : hai
Client : hello
Message to Server : end
Result:
Thus a program in Java implemented an application using TCP Sockets like chat.
18
[Type here]
[Type here]
19
[Type here]
[Type here]
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 8080;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length, ipaddress,portaddr);
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();
}
}
dnsserver.java
import java.io.*;
import java.net.*;
public class dnsserver
{
20
[Type here]
[Type here]
21
[Type here]
[Type here]
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}
Output
In server window
F:\ Abarna\Lab>javac dnsserver.java
F:\ Abarna\Lab>java dnsserver
Press Ctrl + C to Quit
Request for host yahoo.com
In client window
F:\ Abarna\Lab>javac dnsclient.java
F:\ Abarna\Lab>java dnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
Result :
Thus a program in Java performed Simulation of DNS using UDP sockets.
22
[Type here]
[Type here]
import sys
from scapy.all import *
# Define the packet capturing
functiondef
packet_handler(packet):
print(packet.show())
# Capture packets on the network
interfacesniff(iface='eth0',
prn=packet_handler)
Output:
###[ Ethernet ]###
dst = 00:11:22:33:44:55
src = 66:77:88:99:00:11
type = IPv4 ###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 1500
id = 52786
flags = DF
frag = 0
ttl = 128
proto= tcp
chksum= 0xe877
src = 192.168.1.10
dst = 216.58.194.78
23
[Type here]
[Type here]
Result:
Thus the program was executed successfully using tool Wireshark to capture packet and
examine the packet.
24
[Type here]
[Type here]
Aim:
To write a program in java to simulate ARP protocols.
Algorithm:
1. Start the program
2. In Client
a. Start the program
b. Using socket connection is established between client and server.
c. Get the IP address to be converted into MAC address.
d. Send this IP address to server.
e. Server returns the MAC address to client.
3. In Server
a. Start the program
b. Accept the socket which is created by the client.
c. Server maintains the table in which IP and corresponding MAC addresses are stored.
d. Read the IP address which is send by the client.
e. Map the IP address with its MAC address and return the MAC address to client.
4. Stop the program
Program
Clientarp.java
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):");
25
[Type here]
[Type here]
String str1=in.readLine();
dout.writeBytes(str1+'\n');
String str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Serverarp.java
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)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
26
[Type here]
[Type here]
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
F:\ Abarna\2020-2021\Odd\Network Lab>java Serverarp
F:\ Abarna\2020-2021\Odd\Network Lab>java Clientarp
Enter the Logical address (IP): 165.165.80.80
The Physical Address is: 6A:08: AA: C2
Result:
Thus a program in java simulated ARP protocols.
27
[Type here]
[Type here]
28
[Type here]
[Type here]
29
[Type here]
[Type here]
30
[Type here]
[Type here]
INTRODUCTION :
Network Simulator (Version 2), widely known as NS2, is simply an eventdriven simulation tool
that has proved useful in studying the dynamic nature of communication networks. Simulation of wired
as well as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can be done
using NS2. In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviors. Due to its flexibility and modular nature, NS2 has gained
constant popularity in the networking research community since its birth in 1989. Ever since, several
revolutions and revisions have marked the growing maturity of the tool, thanks to substantial
contributions from the players in the field. Among these are the University of California and Cornell
University who developed the REAL network simulator, 1 the foundation which NS is based on.
Since 1995 the Defense Advanced Research Projects Agency (DARPA) supported development
of NS through the Virtual InterNetwork Testbed (VINT) project currently the National Science
Foundation (NSF) has joined the ride in development. Last but not the least, the group of Researchers
and developers in the community are constantly working to keep NS2 strong and versatile.
BASIC ARCHITECTURE :
Figure shows the basic architecture of NS2. NS2 provides users with executable command ns
which take on input argument, the name of a Tcl simulation scripting file. Users are feeding the name
of a Tcl simulation script (which sets up a simulation) as an input argument of an NS2 executable
command ns.
In most cases, a simulation trace file is created, and is used to plot graph and/or to create
animation. NS2 consists of two key languages: C++ and Object-oriented Tool Command Language
(OTcl). While the C++ defines the internal mechanism (i.e., a backend) of the simulation objects, the
31
[Type here]
[Type here]
OTcl sets up simulation by assembling and configuring the objects as well as scheduling discrete events
(i.e., a frontend).
The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object, variables in
the OTcl domains are sometimes referred to as handles. Conceptually, a handle (e.g., n as a Node handle)
is just a string (e.g., _o10) in the OTcl domain, and does not contain any functionality. Instead, the
functionality (e.g., receiving a packet) is defined in the mapped C++ object (e.g., of class Connector).
In the OTcl domain, a handle acts as a frontend which interacts with users and other OTcl objects. It
may define its own procedures and variables to facilitate the interaction. Note that the member
procedures and variables in the OTcl domain are called instance procedures (instprocs) and instance
variables (instvars), respectively. Before proceeding further, the readers are encouraged to learn C++
and OTcl languages.
NS2 provides a large number of built-in C++ objects. It is advisable to use these C++ objects
to set up a simulation using a Tcl simulation script. However, advance users may find these objects
insufficient. They need to develop their own C++ objects, and use a OTcl configuration interface to put
together these objects. After simulation, NS2 outputs either text-based or animation-based simulation
results. To interpret these results graphically and interactively, tools such as NAM (Network AniMator)
and XGraph are used. To analyze a particular behavior of the network, users can extract a relevant subset
of text-based data and transform it to a more conceivable presentation.
CONCEPT OVERVIEW
ns uses two languages because simulator has two different kinds of things it needs to do. On
one hand, detailed simulations of protocols require a systems programming language which can
efficiently manipulate bytes, packet headers and implement algorithms that run over large data sets. For
these tasks run-time speed is important and turn-around time (run simulation, find bug, fix bug,
recompile, re-run) is less important. On the other hand, a large part of network research involves slightly
varying parameters or configurations, or quickly exploring number of scenarios.
In these cases, iteration time (change the model and re-run) is more important. Since
configuration runs once (at the beginning of the simulation), run-time of this part of the task is less
important. ns meets both of these needs with two languages, C++ and OTcl.
32
[Type here]
[Type here]
Nodes
set n0 [$ns node]
set n1 [$ns node]
Links and queuing
$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>
<queue_type>: DropTail, RED, etc.
$ns duplex-link $n0 $n1 1Mb 10ms RED
Creating a larger topology
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 RED
}
Link failures
$ns rtmodel-at <time> up|down $n0 $n1
Creating UDP connection
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp
$ns attach-agent $n1 $null
$ns connect $udp $null
proc finish {}
{
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
33
[Type here]
[Type here]
}
Schedule Events
$ns at <time> <event>
Call ‘finish’
$ns at 5.0 "finish"
Run the simulation
$ns run
RESULT:
Thus the study of NS was done successfully.
34
[Type here]
[Type here]
Aim:
To perform simulation of Congestion Control Algorithms (sliding window) using NS.
Algorithm
1. Run NSG 2.1
2. Create two nodes n0 and n1.
3. Create a duplex-link and set the following parameters
a. Queuetype : Droptail
b. Capacity 0.2 Mbps
c. Propagation delay: 200 ms
d. Queue size: 10
4. Create link from n0 to n1.
5. In Agent tab do the following
a. Packet size: 500 bytes
b. Agent type: TCP and draw a line from n0.
c. Agent type: TCP Sink and draw a line from n1.
d. Draw a line from tcp to sink.
6. In Application tab do the following.
a. Application Type: ftp
b. Start time: 0.1
c. Stop time: 3.5
d. Draw line from tcp.
7. In parameters tab do the following
a. Simulation time: 5.0
b. Trace file: sliding.tr
c. Nam file: sliding.nam
d. Click done
8. Click TCL and specify the window side of the sliding window and the transfer of packets and
save the file as sliding.tcl in C:\cygwin\Your folder
Program:
#===================================
# Simulation parameters setup
#===================================
set val(stop) 5.0 ;# time of simulation end
35
[Type here]
[Type here]
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 2 nodes
#===================================
# Links Definition
#===================================
#Createlinks between nodes
36
[Type here]
[Type here]
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp1
37
[Type here]
[Type here]
$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\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam sliding.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
38
[Type here]
[Type here]
Steps to execute:
1. Run cygwin and change the directory
Result:
Thus performed simulation of Congestion Control Algorithms (sliding window) using NS.
39
[Type here]
[Type here]
40
[Type here]
[Type here]
41
[Type here]
[Type here]
42
[Type here]
[Type here]
Output
Result:
Thus studied the performance of TCP/UDP using Simulation Tool (NS2)
43
[Type here]
[Type here]
Aim:
To perform Simulation of Distance Vector Routing algorithm.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute
nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose distance vector routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
Program (Distance Vector Protocol)
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
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
44
[Type here]
[Type here]
$ns rtproto DV
45
[Type here]
[Type here]
Output
Result:
Thus performed Simulation of Distance Vector Routing algorithm.
46
[Type here]
[Type here]
Aim:
To perform Simulation of Link State Routing algorithm.
Algorithm:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam
on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose Link state routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
Program (Link State Protocol)
set ns [new Simulator]
set nr [open link.tr w]
$ns trace-all $nr
set nf [open link.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam link.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
47
[Type here]
[Type here]
$ns rtproto LS
48
[Type here]
[Type here]
Output:
Result:
Thus performed Simulation of Link State Routing algorithm.
49
[Type here]
[Type here]
Aim :
To write a program in Java to implement the Simulation of Error Correction Code
(CRC)
Algorithm :
At sender side
1. Start the program
2. Read the number of bits to be sent. Let n be the Number of bits in data to be sent from
sender side.
3. Read the number of bits in the divisor. Let k be the Number of bits in the divisor (key
obtained from generator polynomial).
4. The binary data is first increased by adding k-1 zeros in the end of the data
5. Use modulo-2 binary division to divide binary data by the divisor and store remainder
of division.
6. Append the remainder at the end of the data to form the encoded data and send the
same
At receiver side
1. Perform modulo-2 division again and if remainder is 0, then there are no errors.
Modulo 2 division
In each step, a copy of the divisor (or data) is XORed with the k bits of the dividend.
The result of the XOR operation (remainder) is (n-1) bits, which is used for the next
step after 1 extra bit is pulled down to make it n bits long.
When there are no bits left to pull down, we have a result. The (n-1)-bit remainder
which is appended at the sender side.
Program
import java.io.*;
import java.util.*;
class crc
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
50
[Type here]
[Type here]
// Read input
System.out.println("Enter the size of the data:");
n = input.nextInt();
int data[] = new int[n];
System.out.println("Enter the data, bit by bit:");
for(int i=0 ; i < n ; i++)
{
System.out.println("Enter bit number " + (n-i) + ":");
data[i] = input.nextInt();
}
//Read Divisor
System.out.println("Enter the size of the divisor:");
n = input.nextInt();
int divisor[] = new int[n];
System.out.println("Enter the divisor, bit by bit:");
for(int i=0 ; i < n ; i++)
{
System.out.println("Enter bit number " + (n-i) + ":");
divisor[i] = input.nextInt();
}
//Perform Division
int remainder[] = divide(data, divisor);
for(int i=0 ; i < remainder.length-1 ; i++)
{
System.out.print(remainder[i]);
}
System.out.println("\nThe CRC code generated is:");
51
[Type here]
[Type here]
{
System.out.print(remainder[i]);
}
System.out.println();
// Append the remainder
int sent_data[] = new int[data.length + remainder.length - 1];
System.out.println("Enter the data to be sent:");
for(int i=0 ; i < sent_data.length ; i++)
{
System.out.println("Enter bit number " + (sent_data.length-i)+ ":");
sent_data[i] = input.nextInt();
}
receive(sent_data, divisor);
}
static int[] divide(int old_data[], int divisor[])
{
int remainder[] , i;
int data[] = new int[old_data.length + divisor.length];
System.arraycopy(old_data, 0, data, 0, old_data.length);
if(remainder[0] == 1)
{
52
[Type here]
[Type here]
53
[Type here]
[Type here]
if(remainder[i] != 0)
{
System.out.println("There is an error in received data...");
return;
}
}
System.out.println("Data was received without any error.");
}
}
Output
D:\Abarna>javac crc.java
D:\Abarna>java crc
Enter the size of the data: 7
Enter the data, bit by bit:
Enter bit number 7:
1
Enter bit number 6:
0
Enter bit number 5:
0
Enter bit number 4:
1
Enter bit number 3:
1
Enter bit number 2:
0
Enter bit number 1:
1
Enter the size of the divisor: 4
Enter the divisor, bit by bit:
Enter bit number 4:
1
Enter bit number 3:
0
54
[Type here]
[Type here]
55
[Type here]
[Type here]
Result:
Thus a program in Java implemented the Simulation of Error Correction Code (CRC)
56
[Type here]