0% found this document useful (0 votes)
97 views71 pages

CN Lab Manual-1

The document describes an experiment to generate Hamming codes for error detection and correction. It outlines a 4-step algorithm: 1) calculate redundant bits, 2) place redundant bits in power of 2 positions, 3) calculate parity bits using the same rule, 4) correct errors by flipping bits. The program takes a 7-bit data code as input, generates an 11-bit Hamming code, introduces an error, detects the error position using parity checks, and outputs the corrected codeword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views71 pages

CN Lab Manual-1

The document describes an experiment to generate Hamming codes for error detection and correction. It outlines a 4-step algorithm: 1) calculate redundant bits, 2) place redundant bits in power of 2 positions, 3) calculate parity bits using the same rule, 4) correct errors by flipping bits. The program takes a 7-bit data code as input, generates an 11-bit Hamming code, introduces an error, detects the error position using parity checks, and outputs the corrected codeword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ADHIYAMAAN COLLEGE OF ENGINEERING (AUTONOMOUS)

DEPARTMENT OF IT

COMPUTER NETWORKS LABORATORY MANUAL

Year : III Year IT


Semester : V
Sub Code : 518ITP08
Regulation : 2018

Prepared by
[Link]
AP/IT
LIST OF EXPERIMENTS

EXPT TITLE
NO.
1 BASIC NETWORK COMMAND LINE UTILITIES SUCH AS PING,
NETSTAT, TRACERT, NSLOOKUP, PORT SCAN, ARP, IPCONFIG.
2 GENERATE HAMMING CODE FOR ERROR DETECTION AND
CORRECTION
3 IMPLEMENT ERROR DETECTION CODE USING CRC
4 IMPLEMENTATION OF STOP AND WAIT PROTOCOL
5 IMPLEMENTATION OF SLIDING WINDOW PROTOCOL
6 IMPLEMENTATION OF UDP
7 IMPLEMENTATION OF TCP
8 STUDY OF BASIC CONCEPTS OF NETWORK SIMULATOR (NS2),
ITS INSTALLATION AND WORKING ENVIRONMENT.
9 SIMULATION OF NS2 OBJECT,NETWORK
TOPOLOGY,NETWORK TRAFFIC, NAM AND TRACE
10 SIMULATION OF DUPLEX NETWORK
11 SIMULATION OF WIRED NETWORK
12 SIMULATION OF LINK STATE ROUTING AND DISTANCE
VECTOR ROUTING
13 PACKET CAPTURE AND NETWORK TRAFFIC USING WIRE
SHARK TOOL.
EX NO: 1 BASIC NETWORK COMMAND: PING, NETSTAT, TRACERT,
NSLOOKUP, PORT SCAN, ARP, IPCONFIG.
AIM:
To execute network basic commands in command prompt.
Ping:
The ping command helps to verify IP-level connectivity.
When troubleshooting, you can use ping to send an ICMP echo request to a
target host name or IP address. Use ping whenever you need to verify that a host
computer can connect to the TCP/IP network and network resources. Ping is the
primary tool for troubleshooting IP-level connectivity. Type ping -? at a command
prompt to see a complete list of available command-line options. Ping allows you
to specify the size of packets to use (the default is 32 bytes), how many to send,
whether to record the route used, what Time To Live (TTL) value to use, and
whether to set the "don't fragment" flag.
Netstat:
The netstat command is a Command Prompt command used to
display very detailed information about how your computer is communicating with
other computers or network devices. Specifically, the netstat command can show
details about individual network connections, overall and protocol-specific
networking statistics, and much more, all of which could help troubleshoot certain
kinds of networking issues.

Tracert:
The tracert command is a Command Prompt command, used to show several
details about the path that a packet takes from the computer or device to destination
you specify.A traceroute is a function which traces the path from one network to
another. It allows us to diagnose the source of many problems.
Nslookup:
nslookup (which stands for name server lookup) is a network utility program
used to obtain information about internet servers. As its name suggests, it finds
name server information for domains by querying the Domain Name System
(DNS). Nslookup is a useful tool for troubleshooting DNS problems, such as host
name resolution. When you start Nslookup, it shows the host name and IP address
of the DNS server that is configured for the local system, and then display a
command prompt for further queries. If you type a question mark ( ? ), Nslookup
shows all available commands. You can exit the program by typing exit .
port scan
The most common host-based tool for checking for open ports on Windows
or Unix systems is the netstat command. But running this command means
actually walking or remotely accessing each and every server and you miss other
host systems that might be listening on improper ports. This is where port scanners
come in with this tool, a system, network, or security administrator can check a
group of hosts all at once.
ARP:
Address Resolution Protocol (ARP) is a protocol for mapping an Internet
Protocol address (IP address) to a physical machine address that is recognized in
the local [Link] you try to ping an IP address on your local network, say
[Link], your system has to turn the IP address [Link] into a MAC
address. This involves using ARP to resolve the address, hence its [Link]
keep an ARP look-up table where they store information about what IP addresses
are associated with what MAC addresses. When trying to send a packet to an IP
address, the system will first consult this table to see if it already knows the MAC
address. If there is a value cached, ARP is not used.
If the IP address is not found in the ARP table, the system will then send a
broadcast packet to the network using the ARP protocol to ask "who has
[Link]". Because it is a broadcast packet, it is sent to a special MAC address
that causes all machines on the network to receive it. Any machine with the
requested IP address will reply with an ARP packet that says "I am [Link]",
and this includes the MAC address which can receive packets for that IP.
Ipconfig:
Ipconfig (sometimes written as IPCONFIG) is a command line tool used to
control the network connections on Windows NT/2000/XP machines.
Ipconfig displays all current TCP/IP network configuration values and refreshes
Dynamic Host Configuration Protocol (DHCP) and Domain Name System (DNS)
settings.

OUTPUT:
PING :
C:\Users\admin>ping [Link]
Pinging [Link] [[Link]] with 32 bytes of data:
Reply from [Link]: bytes=32 time=52ms TTL=115
Reply from [Link]: bytes=32 time=52ms TTL=115
Reply from [Link]: bytes=32 time=53ms TTL=115
Reply from [Link]: bytes=32 time=53ms TTL=115
Ping statistics for [Link]:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 52ms, Maximum = 53ms, Average = 52m
NETSTAT:
C:\Users\admin>netstat
Active Connections
Proto Local Address Foreign Address State
TCP [Link]:49774 ITPC2:49775 ESTABLISHED
TCP [Link]:49775 ITPC2:49774 ESTABLISHED
TCP [Link]:52105 ITPC2:52106 ESTABLISHED
TCP [Link]:52106 ITPC2:52105 ESTABLISHED
TCP [Link]:52118 ITPC2:52119 ESTABLISHED
TCP [Link]:52119 ITPC2:52118 ESTABLISHED
TRACERT:
C:\Users\admin>tracert [Link]
Tracing route to [Link] [[Link]]
over a maximum of 30 hops:
1 <1 ms 3 ms <1 ms [Link]
2 3 ms 3 ms 3 ms [Link]
NSLOOKUP
C:\Users\admin>nslookup [Link]
Server: [Link]
Address: [Link]
Name: [Link]
Addresses: [Link]
[Link]
ARP
C:\Users\admin>ARP –A
Interface: [Link] --- 0x7
Internet Address Physical Address Type
[Link] 00-50-56-e1-80-22 dynamic
[Link] ff-ff-ff-ff-ff-ff static
[Link] 01-00-5e-00-00-16 static
[Link] 01-00-5e-00-00-fb static
[Link] 01-00-5e-00-00-fc static
[Link] 01-00-5e-7f-ff-fa static
[Link] ff-ff-ff-ff-ff-ff static
IPCONFIG
C:\Users\admin>ipconfig
Windows IP Configuration
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::7888:b0c6:ecc6:3ce0%12
IPv4 Address. . . . . . . . . . . : [Link]
Subnet Mask . . . . . . . . . . . : [Link]
Default Gateway . . . . . . . . . : [Link]

RESULT:
Thus the basic network command are implemented.
EX NO:2 GENERATING HAMMING CODE FOR ERROR DETECTION
AND CORRECTION
AIM:
To Generating Hamming code for error detection and correction
ALGORITHM:
STEP 1: Calculate the number of redundant bits (r) using the formula: 2r ≥ m + r +
1
STEP 2: Place the r redundant bits at bit positions that are powers of 2 (1, 2, 4, 8,
16, etc.).
STEP 3: Calculate the parity bits by following the same rule used to generate c1,
c2, c3, c.
STEP 4: If the decimal value of the parity bits is 0, there is no error. Otherwise, the
decimal value indicates the bit position with the error. Correct the message by
flipping the bit at that position.
PROGRAM:
import [Link].*;
class DanHamming
{
public static void main(String arg[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the 7-bit data code");
int d[]=new int[7];
for(int i=0;i<7;i++)
{
d[i]=[Link]();
}
int p[]=new int[4];
p[0]=d[0]^d[1]^d[3]^d[4]^d[6];
p[1]=d[0]^d[2]^d[3]^d[5]^d[6];
p[2]=d[1]^d[2]^d[3];
p[3]=d[4]^d[5]^d[6];
int c[]=new int[11];
[Link]("Complete Code Word is ");
c[0]=p[0];
c[1]=p[1];
c[2]=d[0];
c[3]=p[2];
c[4]=d[1];
c[5]=d[2];
c[6]=d[3];
c[7]=p[3];
c[8]=d[4];
c[9]=d[5];
c[10]=d[6];
for(int i=0; i<11;i++)
{
[Link](c[i]+ " ");
}
[Link]();
[Link]("Enter the Received codeword");
int r[]=new int[11];
for(int i=0;i<11;i++)
{
r[i]=[Link]();
}
int pr[]=new int[4];
int rd[]=new int[7];
pr[0]=r[0];
pr[1]=r[1];
rd[0]=r[2];
pr[2]=r[3];
rd[1]=r[4];
rd[2]=r[5];
rd[3]=r[6];
pr[3]=r[7];
rd[4]=r[8];
rd[5]=r[9];
rd[6]=r[10];
int s[]=new int[4];
s[0]=pr[0]^rd[0]^rd[1]^rd[3]^rd[4]^rd[6];
s[1]=pr[1]^rd[0]^rd[2]^rd[3]^rd[5]^rd[6];
s[2]=pr[2]^rd[1]^rd[2]^rd[3];
s[3]=pr[3]^rd[4]^rd[5]^rd[6];
int dec=(s[0]*1)+(s[1]*2)+(s[2]*4)+(s[3]*8);
if(dec==0)
[Link]("No error");
else
{
[Link]("Error is at "+dec);
if(r[dec-1]==0)
r[dec-1]=1;
else
r[dec-1]=0;
}
[Link]("Corrected code word is : ");
for(int i=0;i<11;i++)
[Link](r[i]+" ");
[Link]();
}
}

SAMPLE OUTPUT:
Enter the 7-bit data code
1110100
Complete Code Word is
10101101100
Enter the Received codeword
10001101100
Error is at 3
Corrected code word is :
10101101100

RESULT:
Thus the program has been implemented to generating hamming code for error
detection and correction using java.
EX NO:3 IMPLEMENTATION OF CRC
AIM:
To implement the CRC-12, CRC-16, CRC-CCITT in data link layer

ALGORITHM:
STEP 1: Append r zero bits to the input frame (r is the degree of the polynomial
G(x)).
STEP 2: Divide the extended frame by the polynomial G(x) using modulo-2
division.
STEP 3: The remainder is the check-summed frame for transmission.
STEP 4: Transmit the data along with the CRC check-sum.
STEP 5: At the receiver side, perform modulo-2 division on the received data
using the same polynomial.
STEP 6: If the remainder is 0, there is no error in transmission. Otherwise, there
was an error during transmission.

PROGRAM:
import [Link].*;
class crc
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Generator:");
String gen = [Link]();
[Link]("Enter Data:");
String data = [Link]();
String code = data;
while([Link]() < ([Link]() + [Link]() - 1))
code = code + "0";
code = data + div(code,gen);
[Link]("The transmitted Code Word is: " + code);
[Link]("Please enter the received Code Word: ");
String rec = [Link]();
if([Link](div(rec,gen)) == 0)
[Link]("The received code word contains no errors.");
else
[Link]("The received code word contains errors.");
}
static String div(String num1,String num2)
{
int pointer = [Link]();
String result = [Link](0, pointer);
String remainder = "";
for(int i = 0; i< [Link](); i++)
{
if([Link](i) == [Link](i))
remainder += "0";
else
remainder += "1";
}
while(pointer < [Link]())
{
if([Link](0) == '0')
{
remainder = [Link](1, [Link]());
remainder = remainder + [Link]([Link](pointer));
pointer++;
}
result = remainder;
remainder = "";
for(int i = 0; i< [Link](); i++)
{
if([Link](i) == [Link](i))
remainder += "0";
else
remainder += "1";
}
}
return [Link](1,[Link]());
}
}
OUTPUT:
Enter Generator:
1010
Enter Data:
10101010
The transmitted Code Word is: 10101010010
Please enter the received Code Word:
10101010010
The received code word contains no errors.

RESULT:
Thus the program has been implemented to generating CRC for error
detection using java.
EXNO:4 IMPLEMENTATION OF STOP AND WAIT PROTOCOL
AIM:
To write a program for implementation of stop and wait protocol through client
and server.
ALGORITHM:
SENDER:
STEP 1: Start the program.
STEP 2: Declare the socket address.
STEP 3: Specify and bind the port address to communicate with the receiver.
STEP 4: If the specified port is already in use, display an error message.
STEP 5: Initialize a variable to store the length of the data.
STEP 6: Display the received data.
STEP 7: Close the socket.

RECEIVER
STEP 1: Shrink the receiver window after storing the message.
STEP 2: Expand the window size upon message reception.
STEP 3: Display the received message.
STEP 4: Shrink the window size again.
STEP 5: Decrease the data within the new window size.
STEP 6: Receive the message.
STEP 7: Stop the program.

PROGRAM
SENDER:
import [Link].*;
import [Link].*;
import [Link];
class stopwaitsender
{
public static void main(String args[]) throws Exception
{
stopwaitsender sws = new stopwaitsender();
[Link]();
}
public void run() throws Exception
{
Scanner sc=new Scanner([Link]);
[Link]("Enter no of frames to be sent:");
int n=[Link]();
Socket myskt=new Socket("localhost",9999);
PrintStream myps=new PrintStream([Link]());
for(int i=0;i<=n;)
{
if(i==n)
{
[Link]("exit");
break;
}
[Link]("Frame no "+i+" is sent");
[Link](i);
BufferedReader bf=new BufferedReader(new InputStreamReader (myskt.
getInputStream ()));
String ack=[Link]();
if(ack!=null)
{
[Link]("Acknowledgement was Received from receiver");
i++;
[Link](4000);
}
else
{
[Link](i);
}
}
}
}
RECEIVER:
import [Link].*;
import [Link].*;
class stopwaitreceiver
{
public static void main(String args[])throws Exception
{
stopwaitreceiver swr = new stopwaitreceiver();
[Link]();
}
public void run() throws Exception
{
String temp="any message",str="exit";
ServerSocket myss=new ServerSocket(9999);
Socket ss_accept=[Link]();
BufferedReader ss_bf=new BufferedReader(new
InputStreamReader(ss_accept.getInputStream()));
PrintStream myps=new PrintStream(ss_accept.getOutputStream());
while([Link](str)!=0)
{
[Link](1000);
temp=ss_bf.readLine();
if([Link](str)==0)
{
break;
}
[Link]("Frame "+temp+" was received");
[Link](500);
[Link]("Received");
}
[Link]("ALL FRAMES WERE RECEIVED SUCCESSFULLY");
}
}

OUTPUT:
SENDER:
[usera37@redhatnewit gunaseelan]$ javac [Link]
[usera37@redhatnewit gunaseelan]$ java stopwaitsender

Enter no of frames to be sent:


5
Frame no0is sent
Acknowledgement was Received from receiver
Frame no1is sent
Acknowledgement was Received from receiver
Frame no2is sent
Acknowledgement was Received from receiver
Frame no3is sent
Acknowledgement was Received from receiver
Frame no4is sent
Acknowledgement was Received from receiver

RECEIVER
[usera37@redhatnewit gunaseelan]$ javac [Link]
[usera37@redhatnewit gunaseelan]$ java stopwaitreceiver
Frame 0 was received
Frame 1 was received
Frame 2 was received
Frame 3 was received
Frame 4 was received
ALL FRAMES WERE RECEIVED SUCCESSFULLY

RESULT
Thus the program has been implemented to stop and wait protocol using
java.
EXNO:5 SLIDING WINDOW
AIM:
To write a program for implementation of sliding window protocol through
client and server.
ALGORITHM:

SENDER:
STEP 1: Start the program.
STEP 2: Declare and bind the socket address with the receiver's port.
STEP 3: Check for port availability; display an error message if the port is already
in use.
STEP 4: Initialize a variable to store the data length.
STEP 5: Display the received data.
STEP 6: Close the socket.

RECEIVER
STEP 1: Shrink the receiver window after storing the message.
STEP 2: Expand the window size upon message reception and display the received
message.
STEP 3: Shrink the window size again and decrease the data within the new
window size.
STEP 4: Receive the message.
STEP 5: Stop the program.

PROGRAM:
CLIENT PROGRAM
import [Link].*;
import [Link].*;
class sliclient
{
public static void main(String aafhbasgj[])throws Exception
{
Socket s=new Socket("localhost",0202);
String opmsg;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
DataInputStream in=new DataInputStream([Link]());
PrintStream dos=new PrintStream([Link]());
int iterations=[Link]([Link]());
String msg=[Link]();
int no=[Link](msg);
int i=0,intr=0,iframe=0;
int framecounter=0;
while(intr<iterations)
{
msg=[Link]();
[Link](msg);
i++;
framecounter++;
if(framecounter%no==0)
{
[Link]("sending ack to server");
opmsg="ACK : FRAME "+iframe+" RECEIVED";
[Link](opmsg);
iframe++;
}
if(i==8)
{
i=0;
intr++;
}
}
[Link]();
}
}
SERVER:
import [Link].*;
import [Link].*;
class slisender
{
public static void main(String aafhbasgj[])throws Exception
{
ServerSocket ss=new ServerSocket(0202);
Socket s=[Link]();
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
DataInputStream in=new DataInputStream([Link]());
String str1;
[Link]("Enter number of iterations");
int it=[Link]([Link]());
int intr=0;
int msg[]=new int[8];
for(int i=0;i<8;i++)
msg[i]=i;

PrintStream p;
int no;
p=new PrintStream([Link]());
[Link](it);
[Link]("Enter window size");
no=[Link]([Link]());
[Link](no);
int i=0;
while(intr<it)
{
[Link](msg[i]);
i++;

if(i==no)
{
str1=[Link]();
[Link](str1);
}
if(i==8)
{
i=0;
intr++;
}
}
[Link]();
[Link]();
}
}
OUTPUT:
SERVER:
Enter number of iterations
4
Enter window size
2
ACK : FRAME 0 RECEIVED
ACK : FRAME 1 RECEIVED
ACK : FRAME 2 RECEIVED
ACK : FRAME 3 RECEIVED
CLIENT:
project@ubuntu:~/lab/sliding$ java sliclient
0
1
sending ack to server
2
3
sending ack to server
4
5
sending ack to server
0
1
sending ack to server
2
3
sending ack to server
4
5
sending ack to server
0
1
sending ack to server
2
3
sending ack to server
4
5
sending ack to server
0
1
sending ack to server
2
3
sending ack to server
4
5

RESULT:
Thus the program has been implemented to sliding window using java.
EX NO: 6 IMPLEMENTATION OF UDP
AIM:
To write a program for implementation of udp through client and server.
ALGORITHM:
SERVER:
STEP 1: Create a server socket and bind it to a port.
STEP 2: Listen for and accept incoming connections from clients.
STEP 3: Send a message to the connected client and read their IP address.
STEP 4: Display the client details.
STEP 5: Repeat steps 2-4 until the server is terminated.
STEP 6: Close the server socket.
CLIENT
STEP 1: Create a client socket and connect it to the server's port number.
STEP 2: Retrieve its own IP address.
STEP 3: Send its address to the server and display the message received from the
server.
STEP 4: Close the client socket.

PROGRAM:
SERVER:
import [Link].*;
import [Link].*;
class UDPServer
{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789,sport=790;
public static void main(String[] a) throws IOException
{
serversocket = new DatagramSocket(sport);
dp = new DatagramPacket(buf,[Link]);
dis = new BufferedReader
(new InputStreamReader([Link]));
ia = [Link]();
[Link]("Server is Running...");
while(true)
{
[Link](dp);
String str = new String([Link](), 0,[Link]());
if([Link]("STOP"))
{
[Link]("Terminated...");
break;
}
[Link]("Client: " + str);
String str1 = new String([Link]());
buf = [Link]();
[Link](new
DatagramPacket(buf,[Link](), ia, cport));
}
}
}
CLIENT:
import [Link].*;
import [Link].*;
class UDPClient
{
public static DatagramSocket clientsocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789, sport = 790;
public static void main(String[] a) throws IOException
{
clientsocket = new DatagramSocket(cport);
dp = new DatagramPacket(buf, [Link]);
dis = new BufferedReader(new
InputStreamReader([Link]));
ia = [Link]();
[Link]("Client is Running... Type 'STOP'to Quit");
while(true)
{
String str = new String([Link]());
buf = [Link]();
if([Link]("STOP")){
[Link]("Terminated...");
[Link](new DatagramPacket(buf,[Link](), ia,sport));
break;
}
[Link](new DatagramPacket(buf,[Link](), ia, sport));
[Link](dp);
String str2 = new String([Link](), 0,[Link]());
[Link]("Server: " + str2);
}
}
}

OUTPUT:
CLIENT
project@ubuntu:~/lab/udp$ javac [Link]
project@ubuntu:~/lab/udp$ java udpclient
Hello message sent.
Hello
SERVER
project@ubuntu:~/lab/udp$ javac [Link]
project@ubuntu:~/lab/udp$ java udpserver
hello

RESULT:
Thus the program has been implemented for udp communication between
server and client using java
EXNO:7 IMPLEMENTATION OF TCP
AIM:
To write a program for implementation of tcp.
ALGORITHM:
SERVER:
STEP 1: Create and bind a server socket to a port.
STEP 2: Listen for and accept incoming connections from clients.
STEP 3: Send a message to the connected client and display their IP address.
STEP 4: Repeat steps 2-3 until the server is terminated.
STEP 5: Close the server socket.
CLIENT
STEP 1: Create a client socket and connect it to the server's port number.
STEP 2: Retrieve its own IP address.
STEP 3: Send its address to the server and display the received message.
STEP 4: Close the client socket.

PROGRAM:
CLIENT
import [Link].*;
import [Link].*;
public class ReverseClient {
public static void main(String[] args) {
if ([Link] < 2) return;
String hostname = args[0];
int port = [Link](args[1]);
try (Socket socket = new Socket(hostname, port)) {
OutputStream output = [Link]();
PrintWriter writer = new PrintWriter(output, true);
Console console = [Link]();
String text;
do {
text = [Link]("Enter text: ");
[Link](text);
InputStream input = [Link]();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String time = [Link]();
[Link](time);
} while (![Link]("bye"));
[Link]();
} catch (UnknownHostException ex) {
[Link]("Server not found: " + [Link]());
} catch (IOException ex) {
[Link]("I/O error: " + [Link]());
}
}
}
SERVER
import [Link].*;
import [Link].*;
public class ReverseServer {
public static void main(String[] args) {
if ([Link] < 1) return;
int port = [Link](args[0]);
try (ServerSocket serverSocket = new ServerSocket(port)) {
[Link]("Server is listening on port " + port);
while (true) {
Socket socket = [Link]();
[Link]("New client connected");
InputStream input = [Link]();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = [Link]();
PrintWriter writer = new PrintWriter(output, true);
String text;
do {
text = [Link]();
[Link]("Server: " +text);
} while (![Link]("bye"));
[Link]();
}
} catch (IOException ex) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}

OUTPUT:
CLIENT
project@ubuntu:~/lab/tcp$ java ReverseClient localhost 8056
Enter text: hello
Server: hello
Enter text: hi
Server: hi
SERVER
project@ubuntu:~/lab/tcp$ java ReverseServer 8056
Server is listening on port 8056
New client connected
Hello
Hi

RESULT
Thus the program has been implemented for tcp communication
between sever and client using java.
EX NO:8 STUDY OF BASIC CONCEPTS OF NETWORK SIMULATOR
(NS2), ITS INSTALLATION AND WORKING ENVIRONMENT.
AIM:
To Study about Basic concepts of Network Simulator (NS2), its installation
and working environment.
I. Basic concepts of Network Simulator (NS2):
● NS2 stands for Network Simulator Version 2. It is an open-source
event-driven simulator designed specifically for research in computer
communication networks.
● Network simulator is a discrete event network simulator.
● A network simulator is software that predicts the behavior of a computer
network. Network simulator is used to understand system behavior
accurately.
● In simulators, the computer network is typically modeled with devices,
links, and applications to analyze the performance.
● Simulators provide support for the most popular technologies and networks.
Use of Network Simulator
● Network simulators provide a cost effective method for network design and
validation for sensor networks
● Facility for addition or modification to exiting network simulator must
enable user to model the network topology to specify the nodes and the links
between
● Those nodes Model the application flow (traffic) between the nodes.
● Provides network performance metrics as output
● Visualization and animation of packet flow.
Features of NS2
1. It is a discrete event simulator for networking research.
2. It provides substantial support to simulate bunch of protocols like TCP, FTP,
UDP, HTTP and DSR.
3. It simulates wired and wireless network.
4. It is primarily Unix based.
5. Uses TCL as its scripting language.
6. Otcl: Object oriented support
7. Tclcl: C++ and otcl linkage
8. Discrete event scheduler

Basic Architecture
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. The C++ and the
OTcl are linked together using TclCL. Ns2 is basically an Object Oriented TCL
(OTcl) script interpreter with network simulation event scheduler, network
component object libraries and network setup (plumbing) module libraries.
Network simulator is used for setting up and running a network simulation and
user writes a simulation program in OTcl script language. The OTcl script is used
to initiate the event scheduler, setup the network topology and tell traffic source
when to start and stop sending packets through event scheduler

II. INSTALLATION
Installing NS2 on windows 7
● NS2 builds and runs under windows using Cygwin.
● Cygwin provides Linux like environment under windows.
● System Requirements: A computer with C++ compiler. Building full NS2
package requires large memory space approximately 250MB
A. Steps to install NS 2 on windows 7
1. Download Cygwin from following link [Link]
2. Run the downloaded [Link] and you will see screen shown below click next.

3. Select option "Install From Internet". If you have already downloaded the
package select "Install from local directory" and click next.
4. Keep the default installation directory as "C:\cygwin" and click next.
5. Keep default local package directory as your download folder and click next.
6. Next screen will ask for your Internet connection type keep it as "Direct
connection" and click next and in next screen choose one site to download the
packages and click next.
7. In next screen Cygwin will allow to select the packages you want to install .
8. Uncheck the option "Hide obsolete packages" then click on "view" button till the
word "category" changes to "Full".
To install NS2 you need to select and install following packages:
gcc
gcc-g++
gnuplot
make
patch
perl
tar
X-startup-scripts
xorg-x11-base
xorg-x11-bin
xorg-x11-devel
xorg-x11-bin-dlls
xorg-x11-bin-lndir
xorg-x11-etc
xorg-x11-fenc
xorg-x11-fnts
xorg-x11-libs-data
xorg-x11-xwin
libxt-devel
libXmu-devel

To select a package search the package name and click on word "skip" this will
change it to version number of the package. Do this for all above packages and
click next to start download and installation .
9. Once installation is complete create desktop icons if you need.
NOTE: If you missed any package while installing Cygwin first time you can
install it by running the [Link] again and selecting the package in step 8.
10. Cygwin installation is complete now you can run Cygwin from desktop and see
its interface.
B. Steps is to install NS2
1. Download NS2 from following link:
[Link]

2. Decompress the file use winrar. Copy the decompressed folder the Cygwin
installation directory under the subdirectory home. It will be
C:\cygwin\home\system_name : where system_name is name of your system in
above Cygwin installation this path will be C:\Cygwin\home\sys27
3. Run Cygwin from desktop and change the directory to folder you copied just
now in step 2 command to change directory:
cd /home/sys27/ns-allinone-2.28
NOTE: please change sys27 to name of your system
4. To start installation type following command: "./install" (WITHOUT
qoutes)
This will began the installation process if any Cygwin package is missing it will be
reported to you if so the run Cygwin [Link] and install the missing package and
start again from step 2.
5. Add following lines to the .bashrc

export NS_HOME=/home/sys27/ns-allinone-2.28
export
PATH=$NS_HOME/nam-1.11:$NS_HOME/tcl8.4.5/unix:$NS_HOME/tk8.4.5/uni
x:$NS_HOME/bin:$PATH
export
LD_LIBRARY_PATH=$NS_HOME/tcl8.4.5/unix:$NS_HOME/tk8.4.5/unix:$NS_
HOME/otcl-1.9:$NS_HOME/lib:$LD_LIBRARY_PATH
export TCL_LIBRARY=$NS_HOME/tcl8.4.5/library

NOTE: replace sys27 with your system name.

Working environment.
Why NS2 uses two languages? (TCL and C++)
NS2 uses OTcl to create and configure a network, and uses C++ to run simulation.
All C++ codes need to be compiled and linked to create an executable file.
Use OTcl
- For configuration, setup, or one time simulation, or
- To run simulation with existing NS2 modules.
This option is preferable for most beginners, since it does not involve complicated
internal mechanism of NS2. Unfortunately, existing NS2 modules are fairly
limited. This option is perhaps not sufficient for most researchers.

Use C++
- When you are dealing with a packet, or – when you need to modify existing NS2
modules.
This option perhaps discourages most of the beginners from using NS2. This book
particularly aims at helping the readers understand the structure of NS2 and feel
more comfortable in modifying NS2 modules.
NETWORK ANIMATOR [NAM]
NAM is a Tcl/TK based animation tool for viewing network simulation traces
and real world packet traces. It supports topology layout, packet level animation,
and various data inspection [Link] has a graphical interface, which can provide
information such as number of packets drops at each link. The network animator
"NAM'' began in 1990 as a simple tool for animating packet trace data. Nam began
at LBL. It has evolved substantially over the past few years.

We can either start NAM with the command

'nam<nam-file>'

where '<nam-file>' is the name of a NAM trace file that was generated by NS or
one can execute it directly out of the Tcl simulation script for visualization of node
movement.

NAM window is showed on the following figure;

Structure of NS2
● NS is an object oriented discrete event simulator
– Simulator maintains list of events and executes one event after another
– Single thread of control: no locking or race conditions

● Back end is C++ event scheduler


– Protocols mostly
– Fast to run, more control
● Front end is OTCL
Creating scenarios, extensions to C++ protocols

Platforms: It can be employed in most unix systems (FreeBSD, Linux, Solaris)


and Windows.

Source code: Most of NS2 code is in C++

Scripting language: It uses TCL as its scripting language OTcl adds object
orientation to TCL.

Protocols implemented in NS2


Transport layer(Traffic Agent) – TCP, UDP
Network layer(Routing agent)
Interface queue – FIFO queue, Drop Tail queue, Priority queue
Logic link contol layer – IEEE 802.2, AR

NS programming Structure
● Create the event scheduler
● Turn on tracing
● Create network topology
● Create transport connections
● Generate traffic
● Insert errors

Creating Event Scheduler


● Create event scheduler: set ns [new simulator]
● Schedule an event: $ns at <time><event>
– event is any legitimate ns/tcl function
$ns at 5.0 "finish"
proc finish {} {
global ns nf
close $nf
exec nam [Link] &
exit 0
}
● Start Scheduler
$ns run

Tracing
● All packet trace
$ns traceall[open [Link] w]
<event><time><from><to><pkt><size>
<flowid><src><dst><seqno><aseqno>
+ 0.51 0 1 cbr 500 —– 0 0.0 1.0 0 2
_ 0.51 0 1 cbr 500 —– 0 0.0 1.0 0 2
R 0.514 0 1 cbr 500 —–0 0.0 1.0 0 0

● Variable trace
set par [open output/[Link] w]
$tcp attach $par
$tcp trace cwnd_
$tcp trace maxseq_
$tcp trace rtt_

Tracing and Animation


● Network Animator
set nf [open [Link] w]
$ns namtraceall
$nf
proc finish {} {
global ns nf
close $nf
exec nam [Link] &
exit 0 }

Creating topology
● Two nodes connected by a link

● Creating nodes
set n0 [$ns node]
set n1 [$ns node]

● Creating link between nodes


$ns <link_type> $n0 $n1 <bandwidth><delay><queue-type>
$ns duplex-link$n0 $n1 1Mb 10ms DropTail

Data Sending

● Create UDP agent


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
● Create CBR traffic source for feeding into UDP agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent$udp0

● Create traffic sink


set null0 [new Agent/Null]
$ns attach-agent$n1 $null0

● Connect two agents


$ns connect $udp0 $null0
● Start and stop of data
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

RESULT:
Thus the basic concepts, its installation procedure and working environment
of NS2 was studied successfully.
EX NO:9 CREATION OF SIMPLE TOPOLOGY
AIM :
To Write a simple program for creation of topology using ns2
ALGORITHM
STEP 1: Define the procedure "finish."
STEP 2: Close all tracefiles, trifle, and nam files.
STEP 3: Create a new simulator and assign it to "simulator."
STEP 4: Create a new file "outer" and set it as "x."
STEP 5: Store all trifle in "traceall" and nam files in "nam."
STEP 6: Connect UDP and Null.
STEP 7: Stop the program.

PROGRAM
set ns [new Simulator]
$ns color 1 blue
$ns color 2 red
$ns rtproto DV
set nf [open [Link] w]
$ns namtrace-all $nf
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam [Link]
exit 0
}
#creating Nodes
for {set i 0} {$i<7} {incr i} {
set n($i) [$ns node]
}
#Creating Links
for {set i 1} {$i<7} {incr i} {
$ns duplex-link $n(0) $n($i) 512Kb 10ms SFQ
}
#Orienting The nodes
$ns duplex-link-op $n(0) $n(1) orient left-up
$ns duplex-link-op $n(0) $n(2) orient right-up
$ns duplex-link-op $n(0) $n(3) orient right
$ns duplex-link-op $n(0) $n(4) orient right-down
$ns duplex-link-op $n(0) $n(5) orient left-down
$ns duplex-link-op $n(0) $n(6) orient left
#TCP_Config
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n(1) $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
#UDP_Config
set udp0 [new Agent/UDP]
$udp0 set class_ 2
$ns attach-agent $n(2) $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0
#CBR Config
set cbr0 [new Application/Traffic/CBR]
$cbr0 set rate_ 256Kb
$cbr0 attach-agent $udp0
#FTP Config
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#Scheduling Events
$ns rtmodel-at 0.5 down $n(0) $n(5)
$ns rtmodel-at 0.9 up $n(0) $n(5)
$ns rtmodel-at 0.7 down $n(0) $n(4)
$ns rtmodel-at 1.2 up $n(0) $n(4)
$ns at 0.1 "$ftp0 start"
$ns at 1.5 "$ftp0 stop"
$ns at 0.2 "$cbr0 start"
$ns at 1.3 "$cbr0 stop"
$ns at 2.0 "finish"
$ns run

OUTPUT:
RESULT:
Thus the ns2 simulation for creation of topology is executed.
EXNO:10 SIMULATION OF DUPLEX NETWORK
AIM:
To Simulate a four Duplex network and apply TCP agent between two nodes
and UDP agents between other two nodes and by changing the parameters,
determine the number of packets sent and dropped by TCP/UDP.
ALGORITHM:
STEP 1: Create a simulator object and open namfile and tracefile.
STEP 2: Define the "Finish" procedure.
STEP 3: Create four nodes (n0, n1, n2, n3, n4) with links and queue sizes.
STEP 4: Establish a TCP connection for FTP and schedule CBR agents.
STEP 5: Detach the Tcp and link agents, then print the packet size.

ALGORITHM FOR CALCULATE NUMBER OF PACKET SEND AND


DROPPED PACKET
STEP 1: Start the program.
STEP 2: Initialize variables for UDP received and dropped packets, and TCP
received and dropped packets.
STEP 3: Assign columns 1 and 2 as "action" and "type".
STEP 4: If action is "r" and type is "cbr", increment the received packet count.
STEP 5: If action is "d" and type is "tcp", increment the received TCP packet
count.

PROGRAM:
#===================================
# Simulation parameters setup
#===================================
set val(stop) 5 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Open the NS trace file
set tracefile [open [Link] w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open [Link] w]
$ns namtrace-all $namfile
#===================================
# Nodes Definition
#===================================
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n1 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50
$ns duplex-link $n2 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 50
$ns duplex-link $n4 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n4 $n3 50

#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
$ns duplex-link-op $n4 $n3 orient left-up
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500

#Setup a UDP connection


set udp2 [new Agent/UDP]
$ns attach-agent $n3 $udp2
set null3 [new Agent/Null]
$ns attach-agent $n4 $null3
$ns connect $udp2 $null3
$udp2 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 2.0 "$ftp0 stop"

#Setup a CBR Application over UDP connection


set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp2
$cbr1 set packetSize_ 1000
$cbr1 set rate_ 1.0Mb
$cbr1 set random_ null
$ns at 2.0 "$cbr1 start"
$ns at 3.0 "$cbr1 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
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

OUTPUT

RESULT
Thus the simulation for duplex network with four node is implemented.
EXNO:11 SIMULATE A WIRED NETWORK AND MEASURE THE
THROUGHPUT,DELAY,PACKET LOSS
AIM
To Simulate a wired network and measure the Throughput,DELAY,PACKET
LOSS
ALGORITHM
STEP 1: Create a simulator object instance and open the nam file and tracefile.
STEP 2: Define the "finish" procedure.
STEP 3: Create four nodes with links and queue size (n2-n3) set to 10, and specify
node positions for display in nam.
STEP 4: Setup TCP and UDP connections between the nodes along with agents
for FTP over TCP and CBR over UDP connections.
STEP 5: Run the simulator.

CALCULATE NO OF PACKET SENT,RECEIVED AND DROPPED


STEP 1: Start the program.
STEP 2: Initialize variables for UDP received and dropped packets, and TCP
received and dropped packets.
STEP 3: Assign columns "action" and "type."
STEP 4: If "action" is "r" and "type" is "cbr," increment the received packet count.
STEP 5: If "action" is "d" and "type" is "tcp," increment the received TCP packet
count.
STEP 6: Stop the program.

CALCULATE THROUGHPUT
STEP 1: start the program
STEP 2: if action $"r" and $"s and if s==0 assign s=1and start
STEP 3:Assign the time
STEP 4:display throughput of flow
PROGRAM
set ns [new Simulator]
$ns color 1 green
$ns color 2 Red

set nf [open [Link] w]


$ns namtrace-all $nf
set nd [open [Link] w]
$ns trace-all $nd

set f0 [open [Link] w]


set f1 [open [Link] w]
set f2 [open [Link] w]
set last_ack 0
proc record {} {
global ns null tcp f0 f1 f2 last_ack
set time 0.5 ;#Set Sampling Time to 0.5 Sec
set a [$null set bytes_]
set b [$null set nlost_]
set c [$tcp set ack_]
set d [$tcp set packetSize_]

set now [$ns now]


puts $f0 "$now [expr $a*8/$time]"
puts $f1 "$now [expr $b/$time]"
if { $c > 0 } {
set e [expr $c - $last_ack]
puts $f2 "$now [expr $e*$d*8/$time]"
set last_ack $c
} else {
puts $f2 "$now 0"
}
$null set bytes_ 0
$null set nlost_ 0
$ns at [expr $now+$time] "record" ;# Schedule Record after $time
interval sec
}

proc finish {} {
global ns nf nd f0 f1 f2
$ns flush-trace
close $nf
close $nd
close $f0
close $f1
close $f2

# Plot Recorded Statistics


exec xgraph [Link] [Link] -geometry 800x400 &
exec xgraph [Link] -geometry 800x400 &

# NAM
exec nam [Link] &
exit 0
}
set s1 [$ns node]
set s2 [$ns node]
set r [$ns node]
set d [$ns node]

$ns duplex-link $s1 $r 2Mb 10ms DropTail


$ns duplex-link $s2 $r 2Mb 10ms DropTail
$ns duplex-link $r $d 1.7Mb 20ms DropTail

#Queue Limit
$ns queue-limit $r $d 10
set tcp [new Agent/TCP/Reno]
$ns attach-agent $s1 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $d $sink
$ns connect $tcp $sink
$tcp set fid_ 1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

set udp [new Agent/UDP]


$ns attach-agent $s2 $udp
set null [new Agent/LossMonitor]
$ns attach-agent $d $null
$ns connect $udp $null
$udp set fid_ 2
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
$ns at 0.0 "record"
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
$ns at 4.5 "$ns detach-agent $s1 $tcp ; $ns detach-agent $d $sink"
$ns at 5.0 "finish"
$ns run

OUTPUT
CBR LOSS:

CBR THROUGHPUT:

RESULT:
Thus the simulation of a wired network and measure the
throughput,delay,packet loss is executed.
EXNO:12 IMPLEMENT LINK STATE ROUTING AND DISTANCE
VECTOR ROUTING
AIM:
To Implement Link State routing and Distance Vector routing measure the
following performance Metrics
ALGORITHM
DISTANCE VECTOR ROUTING:
STEP 1: Create a simulator object instance and open the nam file and tracefile.
STEP 2: Define the "finish" procedure and declare the distance vector algorithm.
STEP 3: Schedule events for CBR and TCP.
STEP 4: Add the agent simulator.
LINK STATE ROUTING:
STEP 1: Create a simulator object instance and open the nam file and tracefile.
STEP 2: Define the "finish" procedure and declare the link state algorithm.
STEP 3: Schedule events for CBR and TCP.
STEP 4: Add the agent simulator.

PROGRAM:
DISTANCE VECTOR:
# Create a new network simulation instance
set ns [new Simulator]

# Open trace files


set nr [open [Link] w]
$ns trace-all $nr
set nf [open [Link] w]
$ns namtrace-all $nf

# Define a procedure to finish the simulation


proc finish {} {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam [Link] &
exit 0
}

# Create nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}

# Create duplex links


for {set i 0} {$i < 5} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail
}

# Additional links
$ns duplex-link $n(0) $n(2) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(3) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(4) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(5) 1Mb 10ms DropTail

# Create UDP agents and CBR traffic sources


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 udp1 [new Agent/UDP]


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

# Create Null agents for sink nodes


set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns attach-agent $n(5) $null0

# Connect UDP agents to Null agents


$ns connect $udp0 $null0
$ns connect $udp1 $null0

# Enable Distance Vector Routing (DV)


$ns rtproto DV

# Define routing changes using rtmodel-at


$ns rtmodel-at 10.0 down $n(2) $n(3)
$ns rtmodel-at 20.0 down $n(1) $n(4)
$ns rtmodel-at 30.0 up $n(2) $n(3)
$ns rtmodel-at 40.0 up $n(1) $n(4)
# Set FID for UDP agents
$udp0 set fid_ 1
$udp1 set fid_ 2

# Set colors for nodes


$ns color 1 Red
$ns color 2 Green

# Schedule CBR traffic start


$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"

# Schedule the finish procedure


$ns at 45 "finish"

# Run the simulation


$ns run
LINK STATE
# Create a new network simulation instance
set ns [new Simulator]

# Open trace files


set nr [open [Link] w]
$ns trace-all $nr
set nf [open [Link] w]
$ns namtrace-all $nf

# Define a procedure to finish the simulation


proc finish {} {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam [Link] &
exit 0
}
# Create nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
# Create duplex links
for {set i 0} {$i < 5} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail
}
# Additional links
$ns duplex-link $n(0) $n(2) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(3) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(4) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(5) 1Mb 10ms DropTail

# Create UDP agents and CBR traffic sources


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 udp1 [new Agent/UDP]


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

# Create Null agents for sink nodes


set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns attach-agent $n(5) $null0

# Connect UDP agents to Null agents


$ns connect $udp0 $null0
$ns connect $udp1 $null0

# Enable Link State Routing (LS)


$ns rtproto LS

# Define routing changes using rtmodel-at


$ns rtmodel-at 10.0 down $n(2) $n(3)
$ns rtmodel-at 20.0 down $n(1) $n(4)
$ns rtmodel-at 30.0 up $n(2) $n(3)
$ns rtmodel-at 40.0 up $n(1) $n(4)

# Set FID for UDP agents


$udp0 set fid_ 1
$udp1 set fid_ 2

# Set colors for nodes


$ns color 1 Red
$ns color 2 Green

# Schedule CBR traffic start


$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"

# Schedule the finish procedure


$ns at 45 "finish"

# Run the simulation


$ns run

OUTPUT:
NS2 : Simulating Distance Vector Routing
Data Transfer in NAM

First Link Fails


Data Transfer through Alternate Path

Second Link Fails & Data Transfer through Alternate Path

THROUGHPUT:
[root@localhost ~]# awk -f [Link] [Link]
Average Throughput[kbps] = 0.00
StartTime=1.00
StopTime=2.00

Delay:
******
[root@localhost ~]# awk -f [Link] [Link]
GeneratedPackets =200000
ReceivedPackets = 150000
Packet Delivery Ratio = 75.00
Packet Loss = 1.5
Average End-to-End Delay= 0.00001
[root@localhost ~]#

LINKSTATE:
NS2 : Simulating Link State Routing
Network Topology in NAM

Data Transfer & Link Failure


Data Transfer through Alternate Path

Second Link Fails & Data Transfer through Alternate Path


THROUGHPUT:
[root@localhost ~]# awk -f [Link] [Link]
Average Throughput[kbps] = 0.00
StartTime=1.00
StopTime=2.00

Delay:
******
[root@localhost ~]# awk -f [Link] [Link]
GeneratedPackets =200000
ReceivedPackets = 150000
Packet Delivery Ratio = 75.00
Packet Loss = 1.5
Average End-to-End Delay= 0.00001

RESULT:
Thus the simulation for distance vector and link state routing is
implemented.
EX NO:13 EXPERIMENT ON PACKET CAPTURE AND NETWORK
TRAFFIC USING WIRE SHARK TOOL.
AIM:
To implement simulation of packet capture and network traffic analysis
using Wire shark Tool.
ABOUT WIRE SHARK TOOL
Wireshark is a free application that allows you to capture and view the data
traveling back and forth on your network, providing the ability to drill down and
read the contents of each packet – filtered to meet your specific needs. It is
commonly utilized to troubleshoot network problems as well as to develop and test
software. This open-source protocol analyzer is widely accepted as the industry
standard, winning its fair share of awards over the years. Wireshark can be
downloaded at no cost from the Wireshark Foundation website for both macOS
and Windows operating systems. Unless you are an advanced user, it is
recommended that you only download the latest stable release. During the setup
process (Windows only) you should choose to also install WinPcap if prompted, as
it includes a library required for live data capture.
What Wireshark Is Used For?
Wireshark has quite an extensive application or use. Here are a few examples of
what people use Wireshark for:
● Network administrators use it to troubleshoot network problems
● Network security engineers use it to examine security problems
● Developers use it to debug protocol implementations
● Others use it to learn network protocol internals
Features at a Glance
● The following are some of the many features Wireshark provides:
Capture live packet data from a network interface.
● Open files containing packet data captured with tcpdump/WinDump,
Wireshark, and a number of other packet capture programs.
● Import packets from text files containing hex dumps of packet data.
● Display packets with very detailed protocol information.
● Save packet data captured.
● Export some or all packets in a number of capture file formats.
● Filter packets on many criteria.
● Search for packets on many criteria.
● Colorize packet display based on filters.
● Create various statistics.

How to Capture Data Packets

When you first launch Wireshark a welcome screen similar to the one shown
above should be visible, containing a list of available network connections on your
current device. In this example, you'll notice that the following connection types
are shown: Bluetooth Network Connection, Ethernet, VirtualBox Host-Only
Network, Wi-Fi. Displayed to the right of each is an EKG-style line graph that
represents live traffic on that respective network.
To begin capturing packets, first select one or more of these networks by clicking
on your choice(s) and using the Shift or Ctrl keys if you'd like to record data from
multiple networks simultaneously. Once a connection type is selected for capturing
purposes, its background will be shaded in either blue or gray. Click
on Capturefrom the main menu, located towards the top of the Wireshark interface.
When the drop-down menu appears, select the Start option.
You can also initiate packet capturing via one of the following shortcuts.
Keyboard: Press Ctrl + E
Mouse: To begin capturing packets from one particular network, simply
double-click on its name
Toolbar: Click on the blue shark fin button, located on the far left-hand side of the
Wireshark toolbar
The live capture process will now begin, with packet details displayed in the
Wireshark window as they are recorded. Perform one of the actions below to stop
capturing.
Keyboard: Press Ctrl + E
Toolbar: Click on the red stop button, located next to the shark fin on the
Wireshark toolbar
OUTPUT:
Viewing and Analyzing Network Traffic

RESULT:
Thus simulation of packet capture and network traffic analysis using Wire
shark Tool is implemented.

You might also like