0% found this document useful (0 votes)
26 views80 pages

CN&WP Lab Manual

mmmmmmmmmmmmnnnnnnnnnnnnvvvvvvvvvvvvvv

Uploaded by

bgangabhavaniit
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)
26 views80 pages

CN&WP Lab Manual

mmmmmmmmmmmmnnnnnnnnnnnnvvvvvvvvvvvvvv

Uploaded by

bgangabhavaniit
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
You are on page 1/ 80

St.

Marin’s Engineering College CN & WP Lab Manual

1. AIM: Write a program to implement Bit stuffing.

Bit stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5
consecutive 1's.
Source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;

Department of Information Technology Page 1


St.Marin’s Engineering College CN & WP Lab Manual

}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();

OUTPUT for BIT STUFFING:

Enter frame size (Example: 8):12


Enter the frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1
After Bit Stuffing :0101111101001

Department of Information Technology Page 2


St.Marin’s Engineering College CN & WP Lab Manual

2. AIM: Write a Program to implement Character Stuffing

Byte stuffing –
A byte (usually escape character(ESC)), which has a predefined bit pattern is added to the
data section of the frame when there is a character with the same pattern as the flag.
Whenever the receiver encounters the ESC character, it removes from the data section and
treats the next character as data, not a flag.

But the problem arises when the text contains one or more escape characters followed by a
flag. To solve this problem, the escape characters that are part of the text are marked by
another escape character i.e., if the escape character is part of the text, an extra one is added
to show that the second one is part of the text.
Example:

Note – Point-to-Point Protocol (PPP) is a byte-oriented protocol.

Department of Information Technology Page 3


St.Marin’s Engineering College CN & WP Lab Manual

Source code:

#include<stdio.h>
#include<string.h>
main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] == ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
printf("\n After stuffing:%s", fs);
getch();

Department of Information Technology Page 4


St.Marin’s Engineering College CN & WP Lab Manual

}
Output:-
Enter characters to be stuffed: goodday
Enter a character that represents starting delimiter: d
Enter a character that represents ending delimiter: g
After stuffing: dggooddddayg.

Department of Information Technology Page 5


St.Marin’s Engineering College CN & WP Lab Manual

3. AIM: Write a program to implement CRC

Algorithm for Encoding using CRC

a. The communicating parties agrees upon the size of message,M(x) and the
generator polynomial, G(x).
b. If r is the order of G(x),r, bits are appended to the low order end of M(x). This
makes the block size bits, the value of which is xrM(x).
c. The block xrM(x) is divided by G(x) using modulo 2 division.
d. The remainder after division is added to xrM(x) using modulo 2 addition. The
result is the frame to be transmitted, T(x). The encoding procedure makes
exactly divisible by G(x).

Algorithm for Decoding using CRC

e. The receiver divides the incoming data frame T(x) unit by G(x) using modulo
2 division. Mathematically, if E(x) is the error, then modulo 2 division of
[M(x) + E(x)] by G(x) is done.
f. If there is no remainder, then it implies that E(x). The data frame is accepted.
g. A remainder indicates a non-zero value of E(x), or in other words presence of
an error. So the data frame is rejected. The receiver may then send an
erroneous acknowledgment back to the sender for retransmission.

Source code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");

Department of Information Technology Page 6


St.Marin’s Engineering College CN & WP Lab Manual

gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for(i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for(i=0;i<keylen;i++)
temp[i]=input[i];
for(i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for(j=0;j<keylen;j++)
key[j]='0';
else
for(j=0;j<keylen;j++)
key[j]=key1[j];
for(j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for(i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");

Department of Information Technology Page 7


St.Marin’s Engineering College CN & WP Lab Manual

for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for(i=0;i<msglen;i++)
printf("%c",input[i]);
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}

Department of Information Technology Page 8


St.Marin’s Engineering College CN & WP Lab Manual

4. AIM: Write a program for sliding window protocol.

Here you will get sliding window protocol program in C.

In computer networks sliding window protocol is a method to transmit data on a network. Sliding
window protocol is applied on the Data Link Layer of OSI model. At data link layer data is in the
form of frames. In Networking, Window simply means a buffer which has data frames that needs
to be transmitted.

Both sender and receiver agrees on some window size. If window size=w then after sending w
frames sender waits for the acknowledgement (ack) of the first frame.

As soon as sender receives the acknowledgement of a frame it is replaced by the next frames to
be transmitted by the sender. If receiver sends a collective or cumulative acknowledgement to
sender then it understands that more than one frames are properly received, for eg:- if ack of
frame 3 is received it understands that frame 1 and frame 2 are received properly.

Image Source

In sliding window protocol the receiver has to have some memory to compensate any loss in
transmission or if the frames are received unordered.

Department of Information Technology Page 9


St.Marin’s Engineering College CN & WP Lab Manual

Efficiency of Sliding Window Protocol

η = (W*tx)/(tx+2tp)

W = Window Size

tx = Transmission time

tp = Propagation delay

Sliding window works in full duplex mode

It is of two types:-

1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.

2. Go back n: Sender transmits all frames present in the window that occurs after the error bit
including error bit also.

Source code:
#include<stdio.h>

int main()
{
int w,i,f,frames[50];

printf("Enter window size: ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");


scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

Department of Information Technology Page 10


St.Marin’s Engineering College CN & WP Lab Manual

printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

Output

Enter window size: 3

Enter number of frames to transmit: 5

Enter 5 frames: 12 5 89 4 6

With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)

After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver

Department of Information Technology Page 11


St.Marin’s Engineering College CN & WP Lab Manual

12 5 89
Acknowledgement of above frames sent is received by sender

4 6
Acknowledgement of above frames sent is received by sender

Department of Information Technology Page 12


St.Marin’s Engineering College CN & WP Lab Manual

5. AIM: Write a program to implement Go Back N protocol in c

Source code:

#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
output:-
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.

Department of Information Technology Page 13


St.Marin’s Engineering College CN & WP Lab Manual

Frame 2 has been transmitted.


Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.

Please enter the last Acknowledgement received.


2
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.

Please enter the last Acknowledgement received.


8

Department of Information Technology Page 14


St.Marin’s Engineering College CN & WP Lab Manual

6. AIM: Write a C Program to implement Dijkstra’s algorithm.

Given a graph and a source vertex in the graph, find shortest paths from source to all
vertices in the given graph.
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree.
Like Prim’s MST, we generate a SPT (shortest path tree) with given source as root.
We maintain two sets, one set contains vertices included in shortest path tree, other set
includes vertices not yet included in shortest path tree. At every step of the algorithm,
we find a vertex which is in the other set (set of not yet included) and has a minimum
distance from the source.
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from
a single source vertex to all other vertices in the given graph.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in
shortest path tree, i.e., whose minimum distance from source is calculated and
finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance
values as INFINITE. Assign distance value as 0 for the source vertex so that it is
picked first.
3)WhilesptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance
values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of
distance value of u (from source) and weight of edge u-v, is less than the distance
value of v, then update the distance value of v.

Department of Information Technology Page 15


St.Marin’s Engineering College CN & WP Lab Manual

Source code:
#include"stdio.h"
#include"conio.h"
#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[])


{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}

void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("n Enter the number of nodes:");
scanf("%d",&n);
printf("n Enter the cost matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)

Department of Information Technology Page 16


St.Marin’s Engineering College CN & WP Lab Manual

{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("n Shortest path:n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%dn",v,i,dist[i]);
getch();
}

Department of Information Technology Page 17


St.Marin’s Engineering College CN & WP Lab Manual

7. AIM: PROGRAM TO IMPLEMENT BROADCAST ROUTING


ALGORITHM

Source code:

#include<stdio.h>

int a[10][10],n;

main()

int i,j,root;

clrscr();

printf(“Enterno.of nodes:”);

scanf(“%d”,&n);

printf(“Enter adjacent matrix\n”);

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

printf(“Enter connecting of %d–>%d::”,i,j);

scanf(“%d”,&a[i][j]);

printf(“Enter root node:”);

scanf(“%d”,&root);

adj(root);

Department of Information Technology Page 18


St.Marin’s Engineering College CN & WP Lab Manual

adj(int k)

int i,j;

printf(“Adjacent node of root node::\n”);

printf(“%d\n\n”,k);

for(j=1;j<=n;j++)

if(a[k][j]==1 || a[j][k]==1)

printf(“%d\t”,j);

printf(“\n”);

for(i=1;i<=n;i++)

if((a[k][j]==0) && (a[i][k]==0) && (i!=k))

printf(“%d”,i);

Department of Information Technology Page 19


St.Marin’s Engineering College CN & WP Lab Manual

OUTPUT

Enter no.of nodes:5

Enter adjacent matrix

Enter connecting of 1–>1::0

Enter connecting of 1–>2::1

Enter connecting of 1–>3::1

Enter connecting of 1–>4::0

Enter connecting of 1–>5::0

Enter connecting of 2–>1::1

Enter connecting of 2–>2::0

Enter connecting of 2–>3::1

Enter connecting of 2–>4::1

Enter connecting of 2–>5::0

Enter connecting of 3–>1::1

Enter connecting of 3–>2::1

Enter connecting of 3–>3::0

Enter connecting of 3–>4::0

Enter connecting of 3–>5::0

Enter connecting of 4–>1::0

Enter connecting of 4–>2::1

Department of Information Technology Page 20


St.Marin’s Engineering College CN & WP Lab Manual

Enter connecting of 4–>3::0

Enter connecting of 4–>4::0

Enter connecting of 4–>5::1

Enter connecting of 5–>1::0

Enter connecting of 5–>2::0

Enter connecting of 5–>3::0

Enter connecting of 5–>4::1

Enter connecting of 5–>5::0

Enter root node:2

Adjacent node of root node::

134

Department of Information Technology Page 21


St.Marin’s Engineering College CN & WP Lab Manual

8. AIM: C Program / source code for the Distance Vector Routing Algorithm using
Bellman Ford's Algorithm

Distance Vector Routing in this program is implemented using Bellman Ford Algorithm:-
Source code:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct distance from
the node i to k using the cost matrix
//and add the distance from k to node j

Department of Information Technology Page 22


St.Marin’s Engineering College CN & WP Lab Manual

for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}

A sample run of the program works as:-


Enter the number of nodes :
3
Enter the cost matrix :
027
201
710
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router 2
node 1 via 1 Distance 2

Department of Information Technology Page 23


St.Marin’s Engineering College CN & WP Lab Manual

node 2 via 2 Distance 0


node 3 via 3 Distance 1
For router 3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0

Department of Information Technology Page 24


St.Marin’s Engineering College CN & WP Lab Manual

9. AIM: C program to encrypt and decrypt the string using Caesar Cypher
Algorithm.

For encryption and decryption, we have used 3 as a key value.

While encrypting the given string, 3 is added to the ASCII value of the characters. Similarly,
for decrypting the string, 3 is subtracted from the ASCII value of the characters to print an
original string.

Source code:

#include <stdio.h>

int main()
{
int i, x;
char str[100];

printf("\nPlease enter a string:\t");


gets(str);

printf("\nPlease choose following options:\n");


printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);

//using switch case statements


switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value

printf("\nEncrypted string: %s\n", str);


break;
Department of Information Technology Page 25
St.Marin’s Engineering College CN & WP Lab Manual

case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value

printf("\nDecrypted string: %s\n", str);


break;

default:
printf("\nError\n");
}
return 0;
}

Department of Information Technology Page 26


St.Marin’s Engineering College CN & WP Lab Manual

10. AIM: Write a program for congestion control using Leaky bucket algorithm.

In the network layer, before the network can make Quality of service guarantees, it must
know what traffic is being guaranteed. One of the main causes of congestion is that traffic
is often bursty.

To understand this concept first we have to know little about traffic shaping. Traffic
Shaping is a mechanism to control the amount and the rate of the traffic sent to the
network. Approach of congestion management is called Traffic shaping. Traffic shaping
helps to regulate rate of data transmission and reduces congestion.

There are 2 types of traffic shaping algorithms:

1. Leaky Bucket
2. Token Bucket

Suppose we have a bucket in which we are pouring water in a random order but we have to
get water in a fixed rate, for this we will make a hole at the bottom of the bucket. It will
ensure that water coming out is in a some fixed rate, and also if bucket will full we will stop
pouring in it.

The input rate can vary, but the output rate remains constant. Similarly, in networking, a
technique called leaky bucket can smooth out bursty traffic. Bursty chunks are stored in the
bucket and sent out at an average rate.

Department of Information Technology Page 27


St.Marin’s Engineering College CN & WP Lab Manual

In the figure, we assume that the network has committed a bandwidth of 3 Mbps for a host.
The use of the leaky bucket shapes the input traffic to make it conform to this commitment.
In Figure the host sends a burst of data at a rate of 12 Mbps for 2 s, for a total of 24 Mbits of
data. The host is silent for 5 s and then sends data at a rate of 2 Mbps for 3 s, for a total of 6
Mbits of data. In all, the host has sent 30 Mbits of data in 10 s. The leaky bucket smooths the
traffic by sending out data at a rate of 3 Mbps during the same 10 s.

Without the leaky bucket, the beginning burst may have hurt the network by consuming more
bandwidth than is set aside for this host. We can also see that the leaky bucket may prevent
congestion.

A simple leaky bucket algorithm can be implemented using FIFO queue. A FIFO queue holds
the packets. If the traffic consists of fixed-size packets (e.g., cells in ATM networks), the
process removes a fixed number of packets from the queue at each tick of the clock. If the
traffic consists of variable-length packets, the fixed output rate must be based on the number
of bytes or bits.

The following is an algorithm for variable-length packets:

Department of Information Technology Page 28


St.Marin’s Engineering College CN & WP Lab Manual

1. Initialize a counter to n at the tick of the clock.


2. If n is greater than the size of the packet, send the packet and decrement the counter
by the packet size. Repeat this step until n is smaller than the packet size.
3. Reset the counter and go to step 1.

Example – Let n=1000

Packet=
Since n> front of Queue i.e. n>200
Therefore, n=1000-200=800
Packet size of 200 is sent to the network.

Now Again n>front of the queue i.e. n > 400


Therefore, n=800-400=400
Packet size of 400 is sent to the network.

Since n< front of queue


Therefore, the procedure is stop.
Initialize n=1000 on another tick of clock.
This procedure is repeated until all the packets are sent to the network.

Department of Information Technology Page 29


St.Marin’s Engineering College CN & WP Lab Manual

Source code:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

#define NOF_PACKETS 10

intrand(int a)
{
int rn = (random() % 10) % a;
return rn == 0 ? 1 : rn;
}

intmain()
{
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
scanf("%d", &b_size);
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-
PACKET REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else

Department of Information Technology Page 30


St.Marin’s Engineering College CN & WP Lab Manual

{
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rand(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0;
else
op = o_rate, p_sz_rm -= o_rate;
printf("\nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
}
}
}
}
}

Department of Information Technology Page 31


St.Marin’s Engineering College CN & WP Lab Manual

11. AIM: How to write a C Program to Frame sorting technique used in buffers in C
Programming Language?

Source code:

#include<stdio.h>

#include<string.h>

#define FRAM_TXT_SIZ 3

#define MAX_NOF_FRAM 127

char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];

struct frame // structure maintained to hold frames

{ char text[FRAM_TXT_SIZ];

int seq_no;

}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];

int assign_seq_no() //function which splits message

{ int k=0,i,j; //into frames and assigns sequence no

for(i=0; i < strlen(str); k++)

{ fr[k].seq_no = k;

for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)

fr[k].text[j] = str[i++];

printf("\nAfter assigning sequence numbers:\n");

for(i=0; i < k; i++)

printf("%d:%s ",i,fr[i].text);

return k; //k gives no of frames

Department of Information Technology Page 32


St.Marin’s Engineering College CN & WP Lab Manual

void generate(int *random_ary, const int limit) //generate array of random nos

{ int r, i=0, j;

while(i < limit)

{ r = random() % limit;

for(j=0; j < i; j++)

if( random_ary[j] == r )

break;

if( i==j ) random_ary[i++] = r;

}}

void shuffle( const int no_frames ) // function shuffles the frames

int i, k=0, random_ary[no_frames];

generate(random_ary, no_frames);

for(i=0; i < no_frames; i++)

shuf_ary[i] = fr[random_ary[i]];

printf("\n\nAFTER SHUFFLING:\n");

for(i=0; i < no_frames; i++)

printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);

void sort(const int no_frames) // sorts the frames

int i,j,flag=1;

struct frame hold;

for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence

Department of Information Technology Page 33


St.Marin’s Engineering College CN & WP Lab Manual

flag=0;

for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display

if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)

hold = shuf_ary[j];

shuf_ary[j] = shuf_ary[j+1];

shuf_ary[j+1] = hold;

flag=1;

int main()

int no_frames,i;

printf("Enter the message: ");

gets(str);

no_frames = assign_seq_no();

shuffle(no_frames);

sort(no_frames);

printf("\n\nAFTER SORTING\n");

for(i=0;i<no_frames;i++)

printf("%s",shuf_ary[i].text);

printf("\n\n");

Department of Information Technology Page 34


St.Marin’s Engineering College CN & WP Lab Manual

OUTPUT C Program to Frame sorting technique used in buffers.:


Enter the message: Welcome To Acharya Institute of Technology

After assigning sequence numbers:

0:Wel 1:com 2:e T 3:o A 4:cha 5:rya 6: In 7:sti 8:tut 9:e o 10:f T 11:ech 12:nol 13:ogy

AFTER SHUFFLING:

1:com 4:cha 9:e o 5:rya 3:o A 10:f T 2:e T 6: In 11:ech 13:ogy 0:Wel 8:tut 12:nol 7:sti

AFTER SORTING

Welcome To Acharya Institute of Technology

Department of Information Technology Page 35


St.Marin’s Engineering College CN & WP Lab Manual

12.. Wireshark

Wireshark, a network analysis tool formerly known as Ethereal, captures packets in real time
and display them in human-readable format. Wireshark includes filters, color coding, and
other features that let you dig deep into network traffic and inspect individual packets.

i) Startingwireshark:

Two different methods for starting Wireshark are available. These include the Start menu and
the Run command box.

Method 1 - Start Menu:To start Wireshark using the Start menu:

1. Open the Start menu.


2. Select All Programs.
3. Select Wireshark.

Method 2 - Run Command:

To start Wireshark using the Run command box:

1. Open the Start menu or press the Windows key + R.


2. Type Wireshark in the Run command box.
3. Press Enter.

ii) Packet Capture using Wireshark:

The following methods can be used to start capturing packets with Wireshark:

 You can double-click on an interface in the welcome screen.


 You can select an interface in the welcome screen, then select Capture → Start or
click the first toolbar button.
 You can get more detailed information about available interfaces using “The
“Capture Options” Dialog Box” (Capture → Options…).

Department of Information Technology Page 36


St.Marin’s Engineering College CN & WP Lab Manual

 After some time interval we need to stop the capturing .(click stop from capture
menu).

Sample Packet Capturing (snapshot)

iii) Saving the Capture

Department of Information Technology Page 37


St.Marin’s Engineering College CN & WP Lab Manual

1. To save the capture, click File > Save.


2. Name the file, and click Save.
Note: .Pcap and .Pcap-ng are good file types to use for the capture if you plan to use Eye
P.A. to open the capture.

Viewing/Analysis of Packets Captured

Once you have captured some packets or you have opened a previously saved capture file,
you can view the packets that are displayed in the packet list pane (first pane) by simply
clicking on a packet in the packet list pane, which will bring up the selected packet in the
packet list details view and byte view panes.

You can then expand any part of the tree to view detailed information about each protocol in
each packet. Clicking on an item in the tree will highlight the corresponding bytes in the byte
view pane.

Department of Information Technology Page 38


St.Marin’s Engineering College CN & WP Lab Manual

In addition you can view individual packets in a separate window. We can do this by double-
clicking on an item in the packet list or by selecting the packet in which you are interested in
the packet list pane and selecting View → Show Packet in New Window. This allows you to
easily compare two or more packets, even across multiple files.

Department of Information Technology Page 39


St.Marin’s Engineering College CN & WP Lab Manual

iv) Statisticks :

When using Wireshark, we have various types of Statistic tools, starting from the simple tools
for listing end-nodes and conversations, to the more sophisticated tools such as flow and I/O
graphs. Here we are representing two of them.

To start statistics tools, start Wireshark, and choose Statistics from the main menu.

a) Using the statistics to capture file properties

Two steps to be performed:1) Open/Create Packet Capture File.

Department of Information Technology Page 40


St.Marin’s Engineering College CN & WP Lab Manual

2) From the Statistics menu, choose Capture File Properties

Department of Information Technology Page 41


St.Marin’s Engineering College CN & WP Lab Manual

b) Using the statistics to capture conversations

We can use this statistics tools for:

Department of Information Technology Page 42


St.Marin’s Engineering College CN & WP Lab Manual

 On layer 2 (Ethernet): To find and isolate broadcast storms


 On layer 3/layer 4 (TCP/IP): To connect in parallel to the internet router port, and
check who is loading the line to the ISP

V) Filters:

Wireshark has two filtering languages: capture filters and display filters. Capture filters are
used for filtering when capturing packets.

You enter the capture filter into the “Filter” field of the Wireshark “Capture Options” dialog
box, from capture menu.

Display filters are used for filtering which packets are displayed .Display filters allow you to
concentrate on the packets you are interested in while hiding the currently uninteresting ones.
They allow you to only display packets based on:

 Protocol
 The presence of a field
 The values of fields
 A comparison between fields
 … and a lot more!

To only display packets containing a particular protocol, type the protocol name in the
display filter toolbar of the Wireshark window and press enter to apply the filter.

For example if we select tcp protocol in Display filter, the following screen will be displayed.

Department of Information Technology Page 43


St.Marin’s Engineering College CN & WP Lab Manual

To remove the filter, click on the Clear button to the right of the display filter field.
All packets will become visible again.

11. Running nmap scan

Department of Information Technology Page 44


St.Marin’s Engineering College CN & WP Lab Manual

Nmap will scan a target System and report which ports are open and which are closed.

1)Download Nmap scan in Windows

2) Copy and paste it in C:\Program Files\nmap-7.80

3)To run nmap scan,type nmap in command line window.(eg: nmap 172.16.1.34)

Department of Information Technology Page 45


St.Marin’s Engineering College CN & WP Lab Manual

The above screen shot command is used for discovering all live hosts on our network.

Department of Information Technology Page 46


St.Marin’s Engineering College CN & WP Lab Manual

12. OS detection using Nmap

One of Nmap's best-known features is remote OS detection using TCP/IP stack


fingerprinting.

Department of Information Technology Page 47


St.Marin’s Engineering College CN & WP Lab Manual

13. Do the following using NS2 Simulator

i) NS2 Simulator Introduction

Network Simulator (Version 2), widely known as NS2, is simply an event-driven


simulation tool that has proved useful in studying the dynamic nature of communication
networks.

a) 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,DSR etc.
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

b) 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.

Department of Information Technology Page 48


St.Marin’s Engineering College CN & WP Lab Manual

Department of Information Technology Page 49


St.Marin’s Engineering College CN & WP Lab Manual

Flow diagram for running MANET routing protocols in ns-2

Scenario Traffic pattern


Parameters Parameters

Mobility CMU Traffic


Scenario Generator
generator utility
(Bonn Motion) (cbrgen.tcl)

Mobility Tcl Script File


Traffic File
Scenario file (e.g script.tcl)

Network
animator file Ns-2 simulator Output (Dsdv.o)
(.nam)

C/C++
Network compiler
Animator Trace file (.tr)

Analyzer module
Topology (.java file)
graphs Routing protocol
source code (.cc, .h)

Output file (.csv) #include <proto.h>


#include <dsragent.h>
void main()
{
….
}

Graph drawing
software (e.g Excel)
LEGEND

Tools/utilities

Output Performance
Graphs
User Input

c) Trace file format is given below:

Department of Information Technology Page 50


St.Marin’s Engineering College CN & WP Lab Manual

In this format 12 fields and we can explain it as;

1. EVENT OR TYPE IDENTIFIER


+ :a packet enque event
- :a packet deque event
r :a packet reception event
d :a packet drop (e.g., sent to dropHead_) event
c :a packet collision at the MAC level
2. TIME : at which the packet tracing string is created.
3-4. SOURCE AND DESTINATION NODE : source and destination ID's of tracing objects.
5. PACKET NAME : Name of the packet type.
6. PACKET SIZE : Size of packet in bytes.
7. FLAGS : 7 digit flag string.
“-”: disable
1st = “E”: ECN (Explicit Congestion Notification) echo is enabled.
2nd = “P”: the priority in the IP header is enabled.
3rd : Not in use
4th = “A”: Congestion action
5th = “E”: Congestion has occurred.
6th = “F”: The TCP fast start is used.
7th = “N”: Explicit Congestion Notification (ECN) is on.
8. FLOW ID
9-10. SOURCE AND DESTINATION ADDRESS : The format of these two fields is “a.b”,
where “a" is the address and "b" is the port.
11. SEQUENCE NUMBER
12. PACKET UNIQUE ID

Snapshot of trace file(out.tr)

+ 1.04208 2 3 cbr 1000 ------- 0 1.0 3.1 4 6


- 1.04208 2 3 cbr 1000 ------- 0 1.0 3.1 4 6

Department of Information Technology Page 51


St.Marin’s Engineering College CN & WP Lab Manual

r 1.04416 2 3 cbr 1000 ------- 0 1.0 3.1 3 5


+ 1.048 1 2 cbr 1000 ------- 0 1.0 3.1 6 10
- 1.048 1 2 cbr 1000 ------- 0 1.0 3.1 6 10
r 1.05008 1 2 cbr 1000 ------- 0 1.0 3.1 5 7
+ 1.05008 2 3 cbr 1000 ------- 0 1.0 3.1 5 7
- 1.05008 2 3 cbr 1000 ------- 0 1.0 3.1 5 7
r 1.050136 0 2 tcp 1540 ------- 0 0.0 3.0 1 8
+ 1.050136 2 3 tcp 1540 ------- 0 0.0 3.0 1 8

ii ) NS2 Simulation for packet tranmission in wired networks

1)create tcpudp.tcl

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0; # time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open tcpudp.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open tcpudp.nam w]
$ns namtrace-all $namfile

Department of Information Technology Page 52


St.Marin’s Engineering College CN & WP Lab Manual

#===================================
# 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 $n2 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n2 $n1 50
$ns duplex-link $n1 $n0 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n0 50
$ns duplex-link $n4 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n4 $n1 50
$ns duplex-link $n3 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n3 $n1 50

#Give node position (for NAM)


$ns duplex-link-op $n2 $n1 orient right-down
$ns duplex-link-op $n1 $n0 orient right
$ns duplex-link-op $n4 $n1 orient right-up
$ns duplex-link-op $n3 $n1 orient right

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]

Department of Information Technology Page 53


St.Marin’s Engineering College CN & WP Lab Manual

$ns attach-agent $n3 $tcp0


set sink1 [new Agent/TCPSink]
$ns attach-agent $n0 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500

#Setup a UDP connection


set udp2 [new Agent/UDP]
$ns attach-agent $n4 $udp2
set null3 [new Agent/Null]
$ns attach-agent $n0 $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 1.0 "$cbr1 start"
$ns at 2.0 "$cbr1 stop"

Department of Information Technology Page 54


St.Marin’s Engineering College CN & WP Lab Manual

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam tcpudp.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

…………………………………………………………………………………………………
…………..

2)Execute tcpudp.tcl using the following command

$ns tcpudp.tcl

3)It produces tcpudp.nam as output:

Department of Information Technology Page 55


St.Marin’s Engineering College CN & WP Lab Manual

iii)Simulate to find the number of packets dropped in Pcket transmission.

set ns [new Simulator] # Letter S is capital


set nf [open PA1.nam w] # open a nam trace file in write mode
$ns namtrace-all $nf # nf nam filename
set tf [open PA1.tr w] # tf trace filename
$ns trace-all $tf

proc finish { } {
global ns nf tf $ns flush-trace # clears trace file contents
close $nf close $tf exec nam PA1.nam & exit 0
}
set n0 [$ns node] # creates 3 nodes
set n2 [$ns node]

Department of Information Technology Page 56


St.Marin’s Engineering College CN & WP Lab Manual

set n3 [$ns node]

$ns duplex-link $n0 $n2 200Mb 10ms DropTail # establishing links


$ns duplex-link $n2 $n3 1Mb 1000ms DropTail
$ns queue-limit $n0 $n2 10

set udp0 [new Agent/UDP] # attaching transport layer protocols


$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR] # attaching application layer protocols


$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null] # creating sink(destination) node


$ns attach-agent $n3 $null0
$ns connect $udp0 $null0
$ns at 0.1 "$cbr0 start"
$ns at 1.0 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)

#immediately after BEGIN should open braces „{„


BEGIN { c=0;}
{
if($1= ="d")
{ c++;
printf("%s\t%s\n",$5,$11);
}
}
END { printf("The number of packets dropped =%d\n",c); }

Department of Information Technology Page 57


St.Marin’s Engineering College CN & WP Lab Manual

Steps for execution

Open vi editor and type program. Program name should have the extension “ .tcl ”

[root@localhost ~]# vi lab1.tcl


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Open vi editor and type awk program. Program name should have the extension “.awk ”

[root@localhost ~]# vi lab1.awk


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Run the simulation program

[root@localhost~]# ns lab1.tcl
Here “ns” indicates network simulator. We get the topology shown in the snapshot.
Now press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,

[root@localhost~]# awk –f lab1.awk lab1.tr


To see the trace file contents open the file as ,

[root@localhost~]# vi lab1.tr

Department of Information Technology Page 58


St.Marin’s Engineering College CN & WP Lab Manual

Output Topology

Department of Information Technology Page 59


St.Marin’s Engineering College CN & WP Lab Manual

iv) Simulate to find number of packets dropped by tcp/udp

set ns [new Simulator]


set nf [open lab2.nam w]
$ns namtrace-all $nf
set tf [open lab2.tr w]
$ns trace-all $tf

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

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

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


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

set tcp0 [new Agent/TCP] # letters A,T,C,P are capital


$ns attach-agent $n0 $tcp0
set udp1 [new Agent/UDP] # letters A,U,D,P are capital
$ns attach-agent $n1 $udp1
set null0 [new Agent/Null] # letters A and N are capital
$ns attach-agent $n3 $null0
set sink0 [new Agent/TCPSink] # letters A,T,C,P,S are capital
$ns attach-agent $n3 $sink0

Department of Information Technology Page 60


St.Marin’s Engineering College CN & WP Lab Manual

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$ns connect $tcp0 $sink0
$ns connect $udp1 $null0

$ns at 0.1 "$cbr1 start"


$ns at 0.2 "$ftp0 start"
$ns at 0.5 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)

BEGIN{
udp=0;
tcp=0;
}
{
if($1= = “r” && $5 = = “cbr”)
{
udp++;
}
else if($1 = = “r” && $5 = = “tcp”)
{
tcp++;
}
}
END{
printf(“Number of packets sent by TCP = %d\n”, tcp);
printf(“Number of packets sent by UDP=%d\n”,udp);
}

Department of Information Technology Page 61


St.Marin’s Engineering College CN & WP Lab Manual

Steps for execution:


Open vi editor and type program. Program name should have the extension “ .tcl ”

[root@localhost ~]# vi lab2.tcl


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Open vi editor and type awk program. Program name should have the extension “.awk ”

[root@localhost ~]# vi lab2.awk


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Run the simulation program

[root@localhost~]# ns lab2.tcl
oHere “ns” indicates network simulator. We get the topology shown in the snapshot.

oNow press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,

[root@localhost~]# awk –f lab2.awk lab2.tr


To see the trace file contents open the file as ,

[root@localhost~]# vi lab2.tr

Department of Information Technology Page 62


St.Marin’s Engineering College CN & WP Lab Manual

Topology Output
v) Simulate to find number of packets dropped due to congestion

set nf [ open lab4.nam w ]


$ns namtrace-all $nf

Department of Information Technology Page 63


St.Marin’s Engineering College CN & WP Lab Manual

set tf [ open lab4.tr w ]


$ns trace-all $tf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$ns duplex-link $n0 $n4 1005Mb 1ms DropTail
$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p1 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001
set p2 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n1 $p2
set p3 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n2 $p3
$p3 set packetSize_ 30000
$p3 set interval_ 0.00001

set p4 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n3 $p4
set p5 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n5 $p5

$ns queue-limit $n0 $n4 5


$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2
Agent/Ping instproc recv {from rtt} {
$self instvar node_

Department of Information Technology Page 64


St.Marin’s Engineering College CN & WP Lab Manual

puts "node [$node_ id]received answer from $from with round trip time $rtt msec"
}
# please provide space between $node_ and id. No space between $ and from. No space
between and $ and rtt */
$ns connect $p1 $p5
$ns connect $p3 $p4
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab4.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"

Department of Information Technology Page 65


St.Marin’s Engineering College CN & WP Lab Manual

$ns at 2.1 "$p1 send"


$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"
$ns at 1.0 "$p3 send"
$ns at 1.1 "$p3 send"
$ns at 1.2 "$p3 send"
$ns at 1.3 "$p3 send"
$ns at 1.4 "$p3 send"
$ns at 1.5 "$p3 send"
$ns at 1.6 "$p3 send"
$ns at 1.7 "$p3 send"
$ns at 1.8 "$p3 send"
$ns at 1.9 "$p3 send"
$ns at 2.0 "$p3 send"
$ns at 2.1 "$p3 send"
$ns at 2.2 "$p3 send"
$ns at 2.3 "$p3 send"
$ns at 2.4 "$p3 send"
$ns at 2.5 "$p3 send"

Department of Information Technology Page 66


St.Marin’s Engineering College CN & WP Lab Manual

$ns at 2.6 "$p3 send"


$ns at 2.7 "$p3 send"
$ns at 2.8 "$p3 send"
$ns at 2.9 "$p3 send"
$ns at 3.0 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)

BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}

Steps for execution:


1) Open vi editor and type program. Program name should have the extension “ .tcl ”

[root@localhost ~]# vi lab4.tcl


2) Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.

3) Open vi editor and type awk program. Program name should have the extension “.awk ”

[root@localhost ~]# vi lab4.awk

Department of Information Technology Page 67


St.Marin’s Engineering College CN & WP Lab Manual

4) Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.

5) Run the simulation program

[root@localhost~]# ns lab4.tcl
i) Here “ns” indicates network simulator. We get the topology shown in the snapshot.

ii) Now press the play button in the simulation window and the simulation will begins.
6) After simulation is completed run awk file to see the output ,

[root@localhost~]# awk –f lab4.awk lab4.tr


7) To see the trace file contents open the file as ,

[root@localhost~]# vi lab4.tr

Topology

Department of Information Technology Page 68


St.Marin’s Engineering College CN & WP Lab Manual

Department of Information Technology Page 69


St.Marin’s Engineering College CN & WP Lab Manual

Output Screens

vi) Simulate to plot Congestion window for different Source/Destination

set ns [new Simulator]


set tf [open pgm7.tr w]
$ns trace-all $tf
set nf [open pgm7.nam w]
$ns namtrace-all $nf

set n0 [$ns node] $n0 color "magenta" $n0 label "src1"


set n1 [$ns node]
set n2 [$ns node] $n2 color "magenta" $n2 label "src2"
set n3 [$ns node] $n3 color "blue" $n3 label "dest2"
set n4 [$ns node]
set n5 [$ns node] $n5 color "blue" $n5 label "dest1"

$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/ DropTail Mac/802_3 #
should come in single line
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5

$ns connect $tcp0 $sink5


set tcp2 [new Agent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600

Department of Information Technology Page 70


St.Marin’s Engineering College CN & WP Lab Manual

$ftp2 set interval_ 0.001


set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3

set file1 [open file1.tr w]


$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp0 trace cwnd_ # must put underscore ( _ ) after cwnd and no space between them
$tcp2 trace cwnd_
proc finish { }
{
global ns nf tf
$ns flush-trace close
$tf close
$nf exec nam pgm7.nam & exit 0
}

$ns at 0.1 "$ftp0 start"


$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
cwnd:- means congestion window

Department of Information Technology Page 71


St.Marin’s Engineering College CN & WP Lab Manual

BEGIN {
}
{
if($6= ="cwnd_") # don‟t leave space after writing cwnd_
printf("%f\t%f\t\n",$1,$7); # you must put \n in printf
}
END {
}

Steps for execution

Open vi editor and type program. Program name should have the extension “ .tcl ”

[root@localhost ~]# vi lab7.tcl


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Open vi editor and type awk program. Program name should have the extension “.awk ”

[root@localhost ~]# vi lab7.awk


Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Run the simulation program

[root@localhost~]# ns lab7.tcl
After simulation is completed run awk file to see the output ,

[root@localhost~]# awk –f lab7.awk file1.tr >a1


[root@localhost~]# awk –f lab7.awk file2.tr >a2
[root@localhost~]# xgraph a1 a2
Here we are using the congestion window trace files i.e. file1.tr and file2.tr and we are
redirecting the contents of those files to new files say a1 and a2 using output redirection
operator (>).

Department of Information Technology Page 72


St.Marin’s Engineering College CN & WP Lab Manual

To see the trace file contents open the file as ,

[root@localhost~]# vi lab7.tr

Topology

Department of Information Technology Page 73


St.Marin’s Engineering College CN & WP Lab Manual

XGraph Output

vii) Simulate to Determine the Performance with respect to Transmission of Packets in a


wireless network (with 3 nodes)

set ns [new Simulator]


set tf [open lab8.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open lab8.nam w]
$ns namtrace-all-wireless $nf 1000 1000

$ns node-config -adhocRouting DSDV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \

Department of Information Technology Page 74


St.Marin’s Engineering College CN & WP Lab Manual

-prrootype Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "tcp0"
$n1 label "sink1/tcp1"
$n2 label "sink2"
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0

$ns at 0.1 "$n0 setdest 50 50 15"


$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1

Department of Information Technology Page 75


St.Marin’s Engineering College CN & WP Lab Manual

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2
$ns at 5 "$ftp0 start"
$ns at 5 "$ftp1 start"

$ns at 100 "$n1 setdest 550 550 15"


$ns at 190 "$n1 setdest 70 70 15"

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

$ ns at 250 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)

BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}

Department of Information Technology Page 76


St.Marin’s Engineering College CN & WP Lab Manual

{
if($1= ="r"&& $3= ="_1_" && $4= ="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1= ="r" && $3= ="_2_" && $4= ="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n‖, ((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps", ((count2*pack2*8)/(time2*1000000)));
}

Steps for execution

Open vi editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# vi lab8.tcl
Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Open vi editor and type awk program. Program name should have the extension “.awk ”
[root@localhost ~]# vi lab8.awk
Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Run the simulation program
[root@localhost~]# ns lab8.tcl

Department of Information Technology Page 77


St.Marin’s Engineering College CN & WP Lab Manual

Here “ns” indicates network simulator. We get the topology shown in the snapshot.
oNow press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab8.awk lab8.tr

Output:

Node 1 and 2 are communicating

Department of Information Technology Page 78


St.Marin’s Engineering College CN & WP Lab Manual

Node 2 is moving towards node 3

Node 2 is coming back from node 3 towards node1

Department of Information Technology Page 79


St.Marin’s Engineering College CN & WP Lab Manual

Department of Information Technology Page 80

You might also like