CS3591 CN LabManual
CS3591 CN LabManual
E
Anna University Regulation: 2021
O
C
CS3591- Computer Networks
E
III Year/V Semester
AC
Lab Manual
R
G
Prepared By,
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
$ which tcpdump
O
/usr/sbin/tcpdump
$ su
Use the command tcpdump -D to see which interfaces are available for capture.
1
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
06:10:31.483765 IP 172.16.20.138.47997 > 51.158.186.98.123: NTPv4, Client, length 48
O
5 packets captured
5 packets received by filter
Filtering packets
G
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
2
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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.
E
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost.localdo:53318 ec2-52-206-98-166:https ESTABLISHED
O
tcp 0 0 localhost.localdo:36418 sg2plpkivs-v03.any:http TIME_WAIT
3. ifconfig
It displays the details of a network interface card like IP address, MAC Address, and the status of a
R
3
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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.
E
Non-authoritative answer:
Name: annauniv.edu
O
Address: 103.70.60.38
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
4
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
retry = 900
expire = 604800
minimum = 86400
annauniv.edu nameserver = ns.annauniv.edu.
Non-authoritative answer:
E
annauniv.edu nameserver = ns.annauniv.edu.
O
5. traceroute
C
The traceroute command is used in Linux to map the journey that a packet of information undertakes
from its source to its destination.
E
[cse@localhost ~]$ traceroute
Usage:
AC
-6 Use IPv6
-d --debug Enable socket level debugging
G
5
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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.
E
O
# sudo apt install wireshark
To Open Wireshark
In a konsole execute
# ping www.sudo.com
# traceroute www.google.com
R
G
6
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
O
C
E
AC
R
G
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
Program
O
Download.java
import java.io.*;
import java.net.URL;
public class Download
{
C
E
public static void main(String[] args) throws Exception
AC
{
try
{
String fileName = "digital_image_processing.jpg";
R
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
E
Output
O
F:\Abarna\Lab>javac Download.java
F:\ Abarna \Lab>java Download
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
Buffer Read of length: 2048
O
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
C
E
Buffer Read of length: 1863
AC
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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.
E
d) Echo the data back to the client.
O
e) Close all streams.
f) Close the server socket.
3. In Client
g) Stop.
C
a) Create a client socket and connect it to the server’s port number.
E
b) Send user data to the server.
AC
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
{
ServerSocket sersock = null;
Socket sock = null;
try
{
sersock = new ServerSocket(PORT);
System.out.println("Server Started :"+sersock);
try
{
sock = sersock.accept();
E
System.out.println("Client Connected :"+ sock);
O
DataInputStream ins = new DataInputStream(sock.getInputStream());
System.out.println(ins.readLine());
C
PrintStream ios = new PrintStream(sock.getOutputStream());
ios.println("Hello from server");
ios.close();
E
sock.close();
AC
}
catch(SocketException se)
{
R
}
}
catch(Exception e)
{
System.out.println("Couldn't start " + e.getMessage()) ;
}
System.out.println(" Connection from : " + sock.getInetAddress());
}
12
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
}
Client.java
import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;
class client
{
public static void main(String args[])
{
E
Socket sock=null;
O
DataInputStream dis=null;
PrintStream ps=null;
try
{
C
System.out.println(" Trying to connect");
E
sock= new Socket(InetAddress.getLocalHost(),Server.PORT);
AC
}
G
catch(SocketException e)
{
System.out.println("SocketException " + e);
}
catch(IOException e)
{
System.out.println("IOException " + e);
}
finally
13
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
{
try
{
sock.close();
}
catch(IOException ie)
{
System.out.println(" Close Error :" + ie.getMessage());
}
}
E
O
}
}
Output
In server window:
F:\ Abarna \Lab>javac Server.java
C
E
F:\ Abarna \Lab>java Server
AC
In client window:
G
14
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
e) Repeat steps 3-4 until the client sends "end"
O
f) Close all streams
g) Close the server and client socket
h)
3. In Client
a)
Stop
C
Create a client socket and connect it to the server’s port number
E
b) Get a message from user and send it to server
AC
g) Stop
G
15
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
OutputStreamWriter(Clt.getOutputStream())), true);
fromClient = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
O
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
while(true)
{
C
CltMsg= fromClient.readLine();
E
if(CltMsg.equals("end"))
AC
break;
else
{
System.out.println("Server : " +CltMsg);
R
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
16
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
Srv.close();
}
catch (Exception E)
{
System.out.println(E.getMessage());
}
}
}
tcpchatclient.java
E
import java.io.*;
import java.net.*;
O
class tcpchatclient
{
{
Socket Clt;
C
public static void main(String args[])throws Exception
E
PrintWriter toServer;
AC
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client : " + SrvMsg);
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
E
}
O
}
}
Output
In server window:
F:\ Abarna\ Lab>javac tcpchatserver.java
C
E
F:\ Abarna\Lab>java tcpchatserver
AC
Server started
Client connected
Server : hai
Message to Client : hello
R
Client Disconnected
G
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
e) Lookup the host array for the domain name
O
f) If found then retrieve corresponding address
g) Create a datagram packet and send ip address to client
h)
i)
j)
Close the server socket
Stop
C
Repeat steps 3-7 to resolve further requests from clients
E
3. In Client
AC
19
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
{
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]);
E
byte[] senddata = new byte[1024];
O
byte[] receivedata = new byte[1024];
int portaddr = 8080;
C
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
E
senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length, ipaddress,portaddr);
AC
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,
receivedata.length);
clientsocket.receive(recvpack);
R
20
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
public static void main(String arg[])throws IOException
O
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com","facebook.com"};
C
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140","69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");
E
while (true)
{
AC
serversocket.receive(recvpack);
G
21
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
F:\ Abarna\Lab>java dnsserver
O
Press Ctrl + C to Quit
Request for host yahoo.com
In client window
F:\ Abarna\Lab>javac dnsclient.java
F:\ Abarna\Lab>java dnsclient
C
E
Enter the hostname : yahoo.com
AC
IP Address: 68.180.206.184
R
G
Result :
Thus a program in Java performed Simulation of DNS using UDP sockets.
22
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
import sys
O
from scapy.all import *
# Define the packet capturing
functiondef
packet_handler(packet):
C
E
print(packet.show())
AC
Output:
###[ Ethernet ]###
dst = 00:11:22:33:44:55
G
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
O
C
E
AC
R
G
Result:
Thus the program was executed successfully using tool Wireshark to capture packet and
examine the packet.
24
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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.
E
3. In Server
a. Start the program
O
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.
import java.net.*;
import java.util.*;
class Clientarp
R
{
public static void main(String args[])
G
{
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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);
}
E
}
O
}
Serverarp.java
import java.io.*;
import java.net.*;
C
E
import java.util.*;
class Serverarp
AC
{
public static void main(String args[])
{
try
R
{
G
26
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');
break;
}
}
obj.close();
}
E
}
O
catch(Exception e)
{
System.out.println(e);
}
}
C
E
}
AC
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
R
Result:
Thus a program in java simulated ARP protocols.
27
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
e. Server returns the IP address to client.
O
3. In Server
a. Start the program
b.
c.
C
Accept the socket which is created by the client.
Server maintains the table in which IP and corresponding MAC addresses are
stored.
E
d. Read the MAC address which is send by the client.
AC
e. Map the MAC address with its MAC address and return the IP address to client.
4. Stop the program
Program
clientrarp.java
R
import java.io.*;
G
import java.net.*;
import java.util.*;
public class clientrarp
{
public static void main(String args[]){
try {
DatagramSocket client = new DatagramSocket();
InetAddress addr = InetAddress.getByName("127.0.0.1");
byte[] sendByte = new byte[1204];
28
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
String s = new String(receiver.getData());
System.out.println("The Logical Address is :" + s.trim());
O
client.close(); }
catch(Exception e) {
System.out.println(e);
}}}
C
E
serverrarp.java
import java.io.*;
AC
import java.net.*;
import java.util.*;
public class serverrarp
{
R
{
try
{
DatagramSocket server = new DatagramSocket(1309);
while(true){
byte[] sendByte = new byte[1204];
byte[] receiveByte = new byte[1204];
DatagramPacket receiver = new DatagramPacket(receiveByte,receiveByte.length);
server.receive(receiver);
29
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
DatagramPacket sender = new DatagramPacket(sendByte,sendByte.length,addr,port);
O
server.send(sender);
break;
}
break;
}
C
E
}
AC
}catch(Exception e)
{
System.out.println(e);
}
R
}
G
}
Output:
F:\ Abarna\2020-2021\Odd\Network Lab>java serverrarp
F:\ Abarna\2020-2021\Odd\Network Lab>java clientrarp
Enter the Physical address : D4:3D:7E:12:A3:D9
The Logical Address is: 10.0.3.186
Result:
Thus a program in java simulated RARP protocols.
30
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
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.
O
Since 1995 the Defense Advanced Research Projects Agency (DARPA) supported
development of NS through the Virtual InterNetwork Testbed (VINT) project currently the National
C
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
E
versatile.
BASIC ARCHITECTURE :
AC
R
G
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
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
O
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
C
(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.
E
CONCEPT OVERVIEW
AC
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,
R
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.
G
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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]
}
E
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms RED
O
}
Link failures C
$ns rtmodel-at <time> up|down $n0 $n1
Creating UDP connection
set udp [new Agent/UDP]
E
set null [new Agent/Null]
$ns attach-agent $n0 $udp
AC
proc finish {}
{
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
33
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
}
Schedule Events
$ns at <time> <event>
Call ‘finish’
$ns at 5.0 "finish"
Run the simulation
$ns run
E
O
C
E
AC
R
G
RESULT:
Thus the study of NS was done successfully.
34
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
4. Create link from n0 to n1.
5. In Agent tab do the following
O
a. Packet size: 500 bytes
b. Agent type: TCP and draw a line from n0.
C
c. Agent type: TCP Sink and draw a line from n1.
d. Draw a line from tcp to sink.
E
6. In Application tab do the following.
a. Application Type: ftp
AC
#===================================
# Simulation parameters setup
#===================================
set val(stop) 5.0 ;# time of simulation end
35
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
E
#Open the NAM trace file
O
set namfile [open sliding.nam w]
$ns namtrace-all $namfile
#===================================
C
# Nodes Definition
E
#===================================
AC
#Create 2 nodes
#===================================
# Links Definition
#===================================
#Createlinks between nodes
36
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
E
$tcp1 set windowInit_ 4
O
$tcp1 set maxcwnd_ 4
#===================================
# Applications Definition
R
#===================================
#Setup a FTP Application over TCP connection
G
37
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
$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\""
E
$ns at 2.11 "$ns trace-annotate \"Receive Ack 16,17,18,19\""
O
$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\""
C
$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\""
E
#===================================
AC
# Termination
#===================================
#Define a 'finish' procedure
R
proc finish {} {
global ns tracefile namfile
G
$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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
O
Steps to execute: C
1. Run cygwin and change the directory
Admin@CS03C037/
$ ls
Cygwin.bat Cygwin.ico Abarna bin cygdrive dev etc home lib opt proc tmp usr var
admin@CS03C037 /
$ cd Abarna
admin@CS03C037 /Abarna
R
$ ls
sliding.tcl
admin@CS03C037 /Abarna
G
$ ns sliding.tcl
admin@CS03C037 /Abarna
$ nam: no display name and no $DISPLAY environment variable
Result:
Thus performed simulation of Congestion Control Algorithms (sliding window) using NS.
39
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
attached to n3.
O
6. As default, the maximum size of a packet that a "tcp" agent can generate is 1KByte.
7. A tcp "sink" agent generates and sends ACK packets to the sender (tcp agent) and
C
frees the received packets.
8. A "udp" agent that is attached to n1 is connected to a "null" agent attached to n3.
9. A "null" agent just frees the packets received.
E
10. A "ftp" and a "cbr" traffic generator are attached to "tcp" and "udp" agents
AC
respectively, and the "cbr" is configured to generate 1 KByte packets at the rate of 1
Mbps.
11. The "cbr" is set to start at 0.1 sec and stop at 4.5 sec, and "ftp" is set to start at 1.0 sec
and stop at 4.0 sec
R
Program
#Create a simulator object
G
40
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
O
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
C
set n3 [$ns node]
E
AC
41
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
set ftp [new Application/FTP]
O
$ftp attach-agent $tcp
$ftp set type_ FTP
C
#Setup a UDP connection
E
set udp [new Agent/UDP]
AC
42
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
puts "CBR packet size = [$cbr set packet_size_]"
O
puts "CBR interval = [$cbr set interval_]"
Result:
Thus studied the performance of TCP/UDP using Simulation Tool (NS2)
43
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
6. Setup UDP Connection between n(0) and n(5)
O
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
C
9. Choose distance vector routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
Program (Distance Vector Protocol)
E
set ns [new Simulator]
AC
proc finish { } {
global ns nr nf
G
$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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
set cbr0 [new Application/Traffic/CBR]
O
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
C
set null0 [new Agent/Null]
E
$ns attach-agent $n(5) $null0
AC
$ns rtproto DV
45
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
$ns at 2.0 "$cbr1 start"
O
$ns at 45 "finish"
$ns run
C
Output
E
AC
R
G
Result:
Thus performed Simulation of Distance Vector Routing algorithm.
46
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
O
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
C
9. Choose Link state routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
E
Program (Link State Protocol)
set ns [new Simulator]
AC
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
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
O
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
C
E
set null0 [new Agent/Null]
AC
$ns rtproto LS
48
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
$ns at 2.0 "$cbr1 start"
O
$ns at 45 "finish"
$ns run
Output:
C
E
AC
R
G
Result:
Thus performed Simulation of Link State Routing algorithm.
49
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
obtained from generator polynomial).
O
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.
C
6. Append the remainder at the end of the data to form the encoded data and send the
same
E
At receiver side
AC
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
R
50
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
// 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();
}
E
//Read Divisor
O
System.out.println("Enter the size of the divisor:");
n = input.nextInt();
int divisor[] = new int[n];
C
System.out.println("Enter the divisor, bit by bit:");
for(int i=0 ; i < n ; i++)
E
{
AC
{
System.out.print(remainder[i]);
}
System.out.println("\nThe CRC code generated is:");
51
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
{
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();
E
}
O
receive(sent_data, divisor);
}
C
static int[] divide(int old_data[], int divisor[])
{
int remainder[] , i;
E
int data[] = new int[old_data.length + divisor.length];
AC
if(remainder[0] == 1)
{
52
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
remainder[j-1] = exor(remainder[j], 0);
O
System.out.print(remainder[j-1]);
}
}
C
remainder[divisor.length-1] = data[i+divisor.length];
System.out.println(remainder[divisor.length-1]);
E
}
AC
return remainder;
}
static int exor(int a, int b)
{
R
if(a == b)
{
G
return 0;
}
return 1;
}
static void receive(int data[], int divisor[])
{
53
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
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
E
D:\Abarna>java crc
O
Enter the size of the data: 7
Enter the data, bit by bit:
Enter bit number 7:
1
C
Enter bit number 6:
E
0
AC
54
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
Remainder : 0110
O
5.) First data bit is : 0
Remainder : 1100
6.) First data bit is : 1
Remainder : 1110
C
7.) First data bit is : 1
E
Remainder : 1010
AC
101
The CRC code generated is:
1001101101
Enter the data to be sent:
R
55
CS3591_CN
4931_Grace College of Engineering,Thoothukudi
E
Remainder : 1010
O
3.) First data bit is : 1
Remainder : 0011
4.) First data bit is : 0
Remainder : 0111
C
5.) First data bit is : 0
E
Remainder : 1110
AC
Result:
Thus a program in Java implemented the Simulation of Error Correction Code (CRC)
56
CS3591_CN