CS8581 Networks Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Page | 1

EX.NO:1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute.
Capture ping and traceroute PDUs using a network protocol analyzer and examine

AIM: To Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute

1. Tcpdump

The tcpdump utility allows you to capture packets that flow within your network to assist in
network troubleshooting. The following are several examples of using tcpdump with different
options. Traffic is captured based on a specified filter.

Options Description
-D Print a list of network interfaces.
Specify an interface on which to
-i
capture.
Specify the number of packets to
-c
receive.
-v, -vv, -vvv Increase the level of detail (verbosity).
-w Write captured data to a file.
-r Read captured data from a file.

Many other options and arguments can be used with tcpdump. The following are some specific
examples of the power of the tcpdump utility.

1. Display traffic between 2 hosts

To display all traffic between two hosts (represented by variables host1 and host2):

# tcpdump host host1 and host2

2. 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

3. 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

Page | 2
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 -a -b -e -n -o -p proto -r -s -v interval

-a Displays all connections and listening ports.


Displays the executable involved in creating each connection or listening port. In some
cases well-known executables host multiple independent components, and in these
cases the sequence of components involved in creating the connection or listening port
-b
is displayed. In this case the executable name is in [] at the bottom, on top is the
component it called, and so forth until TCP/IP was reached. Note that this option can be
time-consuming and will fail unless you have sufficient permissions.
-e Displays Ethernet statistics. This may be combined with the -s option.
-n Displays addresses and port numbers in numerical form.
-o Displays the owning process ID associated with each connection.
Shows connections for the protocol specified by proto; proto may be any of: TCP, UDP,
-p proto TCPv6, or UDPv6. If used with the -s option to display per-protocol statistics, proto may
be any of: IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, or UDPv6.
-r Displays the routing table.
Displays per-protocol statistics. By default, statistics are shown for IP, IPv6, ICMP,
-s ICMPv6, TCP, TCPv6, UDP, and UDPv6; the -p option may be used to specify a subset of
the default.
When used in conjunction with -b, will display sequence of components involved in
-v
creating the connection or listening port for all executables.
Redisplays selected statistics, pausing interval seconds between each display. Press
interval CTRL+C to stop redisplaying statistics. If omitted, netstat will print the current
configuration information once.

Page | 3
3. Ifconfig

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. It also
allows some control over active TCP/IP connections. Ipconfig replaced the older winipcfg utility.

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

Examples:

Page | 4
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.

Page | 5
SIMULATION OF PING COMMAND
OBJECTIVE:
To simulate ping command using java language.

Page | 6
Page | 7
SIMULATION OF TRACEROUTE COMMAND
OBJECTIVE:
To simulate Traceroute command using java language.

Page | 8
Page | 9
EX.NO 2. Write a HTTP web client program to download a web page using TCP sockets.

Aim:
To write a java program for socket for HTTP for web page upload and download .

Algorithm

1. Start the program.


2. Get the frame size from the user
3. To create the frame based on the user request.
4. To send frames to server from the client side.
5. If your frames reach the server it will send ACK signal to client otherwise it will send
NACK signal to client.
6. Stop the program

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;

public class Client


{
public static void main(String args[]) throws Exception
{
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(newFile("digital_image_processing.jpg"));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
byte[] bytes = baos.toByteArray();
baos.close();

Page | 10
System.out.println("Sending image to server. ");
OutputStream out = soc.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length); System.out.println("Image
sent to server. "); dos.close();
out.close();
}
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);

Page | 11
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(icon);
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 program for creating sockets for HTTP web page upload and
download was implemented.

Page | 12
EX.NO 3 Applications using TCP sockets like:
A. Echo client and echo server
B. Chat
C File Transfer

EX.NO 3(A) Applications using TCP sockets like Echo client and Echo server
Aim
To write a java program for application using TCP Sockets Links

Algorithm
1. Start the program.
2. Get the frame size from the user
3. To create the frame based on the user request.
4. To send frames to server from the client side.
5. If your frames reach the server it will send ACK signal to client
otherwise it will send NACK signal to client.
6. Stop the program

EchoServer.java
import java.net.*;
import java.io.*;
public class EServer
{
public static void main(String args[ ])
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps; Socket
c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
}
try
{
System.out.println(e);
c=s.accept();
is=new DataInputStream(c.getInputStream( ));

Page | 13
ps=new PrintStream(c.getOutputStream( ));
while(true)
{
line=is.readLine();
ps.println(line);
}}
catch(IOException e)
{
System.out.println(e);
}
}}

EClient.java
import java.net.*;
import java.io.*;
public class EClient
{
public static void main(String arg[])
{
Socket c=null; String line;
DataInputStream is, is1;
PrintStream os;
try
{
InetAddress ia = InetAddress.getLocalHost( );
c=new Socket(ia,9000);
}
catch(IOException e)
{
}
try
{
System.out.println(e);
os=new PrintStream(c.getOutputStream( ));
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream( ));
while(true)
{
System.out.println("Client:");
line=is.readLine( );
os.println(line);
System.out.println("Server:" + is1.readLine( ));
}}
catch(IOException e)
{
System.out.println("Socket Closed!");

Page | 14
}}}

Output

Server

C:\Program Files\Java\jdk1.5.0\bin>javac EServer.java


Note: EServer.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details. 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


Note: EClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details. 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 program to concurrently communicate using TCP Sockets was executed
successfully.

Page | 15
B. CHAT

AIM:
To implement a CHAT application, where the Client establishes a connection with the
Server. The Client and Server can send as well as receive messages at the same time. Both the
Client and Server exchange messages.
Concept:
1. It uses TCP socket communication .We have a server as well as a client.
2. Both can be run in the same machine or different machines. If both are running in the machine,
the address to be given at the client side is local host address.
3. If both are running in different machines, then in the client side we need to specify the ip
address of machine in which server application is running.
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;
public static void main(String args[ ])throws Exception
{
ds=new DatagramSocket(clientport);
System.out.println("press ctrl+c to quit the program");
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
InetAddress ia=InetAddress.geyLocalHost( );
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData( ),0,p.getLength( ));
System.out.println("Client:" + psx);
System.out.println("Server:");
String str=dis.readLine( );
if(str.equals("end"))
break;
buffer=str.getBytes( );
ds.send(new
DatagramPacket(buffer,str.length( ),ia,serverport));
}
Page | 16
}
}

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);
}
}
}

Page | 17
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

Page | 18
C.File Transfer
AIM:

To write a java program for file transfer using TCP Sockets.

Algorithm

Server

Step 1: Import java packages and create class file server.


Step 2: Create a new server socket and bind it to the port.
Step 3: Accept the client connection
Step 4: Get the file name and stored into the BufferedReader.
Step 5: Create a new object class file and realine.
Step 6: If file is exists then FileReader read the content until EOF is reached.
Step 7: Stop the program.

Client

Step1: Import java packages and create class file server.


Step2: Create a new server socket and bind it to the port.
Step3: Now connection is established.
Step4: The object of a BufferReader class is used for storing data content which has been
retrieved from socket object.
Step5: The content of file is displayed in the client window and the connection is closed.
Step6: Stop the program.

Page | 19
Program:
File Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientfile
{ 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 file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.println("Enter the new file name:");
String str2=in.readLine();
String str1,ss;
FileWriter f=new
FileWriter(str2); char buffer[];
while(true)
{
str1=din.readLine();
if(str1.equals("-1")) break;
System.out.println(str1);
buffer=new char[str1.length()];
str1.getChars(0,str1.length(),buffer,0);
f.write(buffer);
}
f.close();
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}}}
Page | 20
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverfile
{
public static void main(String args[])
{
Try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new
DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;
while((s=b.readLine())!=null)
{
System.out.println(s);
dout.writeBytes(s+'\n');
}
f.close();
dout.writeBytes("-1\n");
}
}
catch(Exception e)
{ System.out.println(e);
}
}}

Page | 21
Output
File content Computer
networks jhfcgsauf
jbsdava
jbvuesagv
client
Enter the file name:
sample.txt

server
Computer networks
jhfcgsauf
jbsdava
jbvuesagv

client
Enter the new file name:
net.txt
Computer networks
jhfcgsauf
jbsdava jbvuesagv
Destination file
Computer networks
jhfcgsauf
jbsdava
jbvuesagv

RESULT

Thus the java program file transfer application using TCP Sockets was executed

Page | 22
EX.NO 4. Implementation of DNS using UDP sockets.

Aim :

To implement a DNS server and client in java using UDP Sockets.

Algorithm:

1. Start the program.


2. Get the frame size from the user

3. To create the frame based on the user request.


4. To send frames to server from the client side.

5. If your frames reach the server it will send ACK signal to client

otherwise it will send NACK signal to client.


6. Stop the program

/ UDP DNS Server

import java.io.*;
import java.net.*;
public class udpdnsserver
{
private static int indexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i < array.length; i++)
{
if (array[i].equals(str))
return i;
}
return -1;
}
public static void main(String arg[])throws IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
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");
while (true)
Page | 23
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(receivedata, receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress =
recvpack.getAddress(); int port =
recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);
if(indexOf (hosts, sen) != -1)
capsent = ip[indexOf (hosts, sen)];
else
capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket (senddata, senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}

Page | 24
//UDP DNS Client
import java.io.*;
import java.net.*;
public class udpdnsclient
{
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 = 1362;
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();
}
}

Page | 25
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 DNS application program was executed.

Page | 26
EX.NO 5(a) Program for Address Resolution Protocol (ARP) using TCP

Aim:
To implement a java program for ARP protocols using TCP
ALGORITHM:

Client

1. Start the program


2. Using socket connection is established between client and server.
3. Get the IP address to be converted into MAC address.
4. Send this IP address to server.
5. Server returns the MAC address to client.

Server

1. Start the program


2. Accept the socket which is created by the client.
3. Server maintains the table in which IP and corresponding MAC addresses are stored.
4. Read the IP address which is send by the client.
5. Map the IP address with its MAC address and return the MAC address to client.

Program

Client:
import java.io.*;
import
java.net.*;
import
java.util.*; class
Clientarp
{
public static void main(String args[])
{
try
{

Page | 27
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();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.printl
n(e);
} }
}

Server:
import
java.io.*;
import
java.net.*;
import
java.util.*;
class
Serverarp
{
public static void main(String args[])
{
try
Page | 28
{
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"};
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 ARP protocol using TCP Sockets program was executed.

Page | 29
EX.NO 5(b) Program for Reverse Address Resolution Protocol (RARP) using UDP

Aim:
To write a java program for simulating RARP protocols using UDP
ALGORITHM

Client

1.Start the program


2. using datagram sockets UDP function is established.
2.Get the MAC address to be converted into IP address.
3.Send this MAC address to server.
4.Server returns the IP address to client.

Server

1. Start the program.


2. Server maintains the table in which IP and corresponding MAC addresses are stored.
3. Read the MAC address which is send by the client.
4. Map the IP address with its MAC address and return the IP address to client.

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));
Page | 30
System.out.println("Enter the Physical address (MAC):")

String str=in.readLine(); sendbyte=str.getBytes();


DatagramPacket sender=newDatagramPacket(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.println(e);
}
}
}

Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp12
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
Page | 31
String s=str.trim();
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();

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(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=newDatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
} }
}

Output:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12
Enter the Physical address
(MAC): 6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80

Result :
Thus the RARP program using UDP was executed.

Page | 32
Page | 33
Figure 2.1 shows the basic architecture of NS2. NS2 provides users with an executable
command ns which takes 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
OTcl sets up simulation by assembling and configuring the objects as well as scheduling discrete
events (i.e., a frontend).

Page | 34
Page | 35
Page | 36
Congestion Control

AIM:

To simulate and study the Congestion Control algorithm using simulation.

Program:

set ns [new Simulator]

set nr [open thro_red.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

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]

$ns duplex-link $n0 $n3 1Mb 10ms RED

$ns duplex-link $n1 $n3 1Mb 10ms RED

$ns duplex-link $n2 $n3 1Mb 10ms RED

$ns duplex-link $n3 $n4 1Mb 10ms RED

$ns duplex-link $n4 $n5 1Mb 10ms RED


Page | 37
$ns duplex-link $n4 $n6 1Mb 10ms RED

$ns duplex-link $n4 $n7 1Mb 10ms RED

$ns duplex-link-op $n0 $n3 orient right-up

$ns duplex-link-op $n3 $n4 orient middle

$ns duplex-link-op $n2 $n3 orient right-down

$ns duplex-link-op $n4 $n5 orient right-up

$ns duplex-link-op $n4 $n7 orient right-down

$ns duplex-link-op $n1 $n3 orient right

$ns duplex-link-op $n6 $n4 orient left

set udp0 [new Agent/UDP]

$ns attach-agent $n2 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

set null0 [new Agent/Null]

$ns attach-agent $n5 $null0

$ns connect $udp0 $null0

set udp1 [new Agent/UDP]

$ns attach-agent $n1 $udp1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set interval_ 0.005

$cbr1 attach-agent $udp1

set null0 [new Agent/Null]

$ns attach-agent $n6 $null0

$ns connect $udp1 $null0

set udp2 [new Agent/UDP]

$ns attach-agent $n0 $udp2

set cbr2 [new Application/Traffic/CBR]

$cbr2 set packet size_ 500

$cbr2 set interval_ 0.005


Page | 38
$cbr2 attach-agent $udp2

set null0 [new Agent/Null]

$ns attach-agent $n7 $null0

$ns connect $udp2 $null0

$udp0 set fid_ 1

$udp1 set fid_ 2

$udp2 set fid_ 3

$ns color 1 Red

$ns color 2 Green

$ns color 2 Blue

$ns at 0.1 "$cbr0 start"

$ns at 0.2 "$cbr1 start"

$ns at 0.5 "$cbr2 start"

$ns at 4.0 "$cbr2 stop"

$ns at 4.2 "$cbr1 stop"

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run

Output:

RESULT:
Thus the Congestion Control Algorithm was Simulated and studied.

Page | 39
Page | 40
#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red

#Open the NAM trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#Define a 'finish' procedure

proc finish {} {

global ns nf

$ns flush-trace

#Close the NAM trace file

close $nf

#Execute NAM on the trace file

exec nam out.nam &

exit 0

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n2 2Mb 10ms DropTail

$ns duplex-link $n1 $n2 2Mb 10ms DropTail

$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail

#Set Queue Size of link (n2-n3) to 10

$ns queue-limit $n2 $n3 10

Page | 41
#Give node position (for NAM)

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for link (n2-n3). (for NAM)

$ns duplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection

set tcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

#Setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n3 $null

$ns connect $udp $null

$udp set fid_ 2

#Setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000


Page | 42
$cbr set rate_ 1mb

$cbr set random_ false

#Schedule events for the CBR and FTP agents

$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 4.0 "$ftp stop"

$ns at 4.5 "$cbr stop"

#Detach tcp and sink agents (not really necessary)

$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Print CBR packet size and interval

puts "CBR packet size = [$cbr set packet_size_]"

puts "CBR interval = [$cbr set interval_]"

#Run the simulation

$ns run

Page | 43
Output:

Result:

Thus the behaviour of TCP was observed and the basic terminologies of TCP transmission
were understood.

Page | 44
Page | 45
#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows

$ns color 1 Blue

$ns color 2 Red

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n3 $n2 1Mb 10ms SFQ

#Specify layout of nodes

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for the link 2??3 vertically

$ns duplex-link-op $n2 $n3 queuePos 0.5

#Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$udp0 set class_ 1

$ns attach-agent $n0 $udp0

Page | 46
# Create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n1

set udp1 [new Agent/UDP]

$udp1 set class_ 2

$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set interval_ 0.005

$cbr1 attach-agent $udp1

#Create a Null agent (a traffic sink) and attach it to node n3

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

#Connect traffic sources with the traffic sink

$ns connect $udp0 $null0

$ns connect $udp1 $null0

#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

Page | 47
}

#Define label for nodes

$ns at 0.0 "$n0 label Sender1"

$ns at 0.0 "$n1 label Sender2"

$ns at 0.0 "$n2 label Router"

$ns at 0.0 "$n3 label Receiver"

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$ns at 1.0 "$cbr1 start"

$ns at 4.0 "$cbr1 stop"

$ns at 4.5 "$cbr0 stop"

#Call finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Run the simulation

$ns run

Page | 48
OUTPUT:

Result:

Thus the behaviour of TCP was observed and the basic terminologies of TCP transmission
were understood.

Page | 49
SIMULATION OF DISTANCE VECTOR ROUTING ALGORITHM

AIM:

To simulate and study the Distance Vector routing algorithm using simulation.
SOFTWARE REQUIRED: NS-2

THEORY:
Distance Vector Routing is one of the routing algorithm in a Wide Area Network for computing
shortest path between source and destination. The Router is one main devices used in a wide area network.
The main task of the router is Routing. It forms the routing table and delivers the packets depending upon the
routes in the tableeither directly or via an intermediate devices.

Each router initially has information about its all neighbors. Then this information will be shared among nodes.

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.

Page | 50
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
}
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 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(5) $null0
$ns connect $udp0 $null0

set udp1 [new Agent/UDP]


$ns attach-agent $n(1) $udp1
Page | 51
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null0 [new Agent/Null]


$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto DV
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run

Page | 52
Output:

RESULT:
Thus the Distance vector Routing Algorithm was Simulated and studied.

Page | 53
SIMULATION OF LINK STATE ROUTING ALGORITHM

AIM:
To simulate and study the link state routing algorithm using simulation.

SOFTWARE REQUIRED: NS-2

THEORY:
In link state routing, each router shares its knowledge of its neighborhood with every other router in
the internet work.
(i) Knowledge about Neighborhood: Instead of sending its entire routing table a router sends info about its
neighborhood only.
(ii) To all Routers: each router sends this information to every other router on the internet work not just to its
neighbor .It does so by a process called flooding.
(iii)Information sharing when there is a change: Each router sends out information about the neighbors when
there is change.

PROCEDURE:
The Dijkstra algorithm follows four steps to discover what is called the shortest path tree(routing
table) for each router:

The algorithm begins to build the tree by identifying its roots. The root router’s trees the router itself. The
algorithm then attaches all nodes that can be reached from the root. The algorithm compares the tree’s
temporary arcs and identifies the arc with the lowest cumulative cost. This arc and the node to which it
connects are now a permanent part of the shortest path tree.

The algorithm examines the database and identifies every node that can be reached from its chosen node.
These nodes and their arcs are added temporarily to the tree. The last two steps are repeated until every node
in the network has become a permanent part of the tree.

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.

Page | 54
Coding:

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 }


$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 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(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1

set cbr1 [new Application/Traffic/CBR]


$cbr1 set packetSize_ 500

Page | 55
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null0 [new Agent/Null]


$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto LS
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green

$ns at 1.0 "$cbr0 start"


$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run

Output:

RESULT:
Thus the Link State Routing Algorithm was Simulated and studied.

Page | 56
SIMULATION OF DISTANCE VECTOR ROUTING PROTOCOL USING NS2

OBJECTIVE
Acquire tcl scripting skills to simulate distance vector routing protocol using NS2 simulator.
OUTCOME
Students become expertise to write tcl script to simulate distance vector routing protocol using
NS2 simulator.

PROGRAM
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
set tr [open out.tr w]
$ns trace-all $tr

proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exec nam out.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down


$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]


$ftp attach-agent $tcp

set sink [new Agent/TCPSink]


$ns attach-agent $n3 $sink

set udp [new Agent/UDP]


$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]


$cbr attach-agent $udp
set null [new Agent/Null]
Page | 57
$ns attach-agent $n3 $null
$ns connect $tcp $sink
$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3


$ns rtmodel-at 2.0 up $n1 $n3
$ns rtproto DV

$ns at 0.0 "$ftp start"


$ns at 0.0 "$cbr start"
$ns at 5.0 "finish"

$ns run

OUTPUT:

CONCLUSION :

Thus tcl programs were executed to simulate the performance of distance vector routing protocol
using ns2.

Page | 58
PERFORMANCE ANALYSIS OF ROUTING PROTOCOL USING NS2

AIM:
To simulate Distance Vector Multicast Routing Protocol in NS2.

PROGRAM:
#define options
set val(chan) Channel/WirelessChannel ;
set val(prop) Propogation/TwoRayGround ;
set val(netif) Phy/WirelessPhy ;
set val(mac) Mac/802_11 ;
set val(ifq) Queue/DropTail/PriQueue ;
set val(ll) LL ;
set val(ant) Antenna/OmniAntenna ;
set val(ifqlen) 50 ;
set val(nn) 3 ;
set val(rp) DSDV ;
set val(x) 500;
set val(y) 400;
set val(stop) 150;

set ns [new Simulator]


set tracefd [open simple.tr w]
set windowVsTime2 [open win.tr w]
set namtrace [open simwrls.nam w]

$ns trace-all $tracefd


$ns namtrace-all-wireless $namtrace $val(x) $val(y)

#set up topography object


set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)

# create nn mobilenodes $[val(nn)] and them to channel

#configure the nodes


$ns node-config -adhocRouting DSR \
-llType LL \
-macType Mac/802_11 \
Page | 59
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

for {set i 0} {$i < $val(nn)}


{
incr i
}{
set node_($i) [$ns node]
}

#provide initial location of mobile nodes


$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 490.0


$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 150.0


$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0

#generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"

#set up tcp connection between node_(0) and node_(1)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]

$ns attach-agent $node_(0) $tcp


$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
Page | 60
$ns at 10.0 "$ftp start"
#define node initial position in nam

for {set i 0} {$i < $val(nn)} {incr i}


{
#30 defines the node size for nam
$ns initial_node_pos $node_($i) 30
}
#telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) }
{ incr i }
{
$ns at $val(stop) "$node_($i) reset" ;
}

#ending nam and the simulation


$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150.0 "puts \"end simulation\" ; $ns halt"
proc stop {}
{
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam simwrls.nam &
}

$ns run

Page | 61
OUTPUT:

RESULT:
Thus Distance Vector Multicast Routing Protocol is simulated in NS2.

Page | 62
SIMULATION OF ERROR CORRECTION CODE ( CRC )

Page | 63
Page | 64
Page | 65
Page | 66
Page | 67

You might also like