0% found this document useful (0 votes)
18 views72 pages

Network Security Record

The document outlines a laboratory manual for a course on Network and Security, detailing experiments related to Data Link Layer framing methods, error detection/correction techniques, and protocols like Go-Back-N and Selective Repeat. Each section includes objectives, resources, theoretical background, algorithms, and sample C programs for practical implementation. The results of the experiments are verified through outputs generated by the provided code.

Uploaded by

Kanchanamala
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)
18 views72 pages

Network Security Record

The document outlines a laboratory manual for a course on Network and Security, detailing experiments related to Data Link Layer framing methods, error detection/correction techniques, and protocols like Go-Back-N and Selective Repeat. Each section includes objectives, resources, theoretical background, algorithms, and sample C programs for practical implementation. The results of the experiments are verified through outputs generated by the provided code.

Uploaded by

Kanchanamala
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/ 72

NETWORK AND SECURITY

LABORATORY
Sub Code: EC3401

BACHELOR OF ENGINEERING
IN ELECTRONICS AND COMMUNICATION

Department of Electronics And Communication Engineering


P.T.Lee Chengalvaraya Naicker

College of Engineering and Technology

Affiliated to Anna University Oovery

Kanchipuram-631502
P.T.Lee Chengalvaraya Naicker College of Engineering and Technology

Kanchipuram-631502

Department of Electronics And Communication Engineering

REGISTER NUMBER

This is to certify that Mr/Mrs _______________________ (Reg No. __________________) of

_________ year B.E- Department of _________ in the _____________________________

laboratory during the academic year 20 __ -20__

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

Staff-in-Charge Head of the Department

Submitted for the Practical Examination held on …………………………………………………........

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

Internal Examiner External Examiner


INDEX
S.No. Date Name of the Experiment Pg No. Marks Sign
Ex: 1. DATE: DATA LINK LAYER FRAMING METHODS

OBJECTIVE:

To implement the following Data Link Layer framing methods

i) Bit stuffing
ii) Character stuffing

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

 Framing is function of Data Link Layer that is used to separate message from source or
sender to destination
 Character Stuffing
o Character stuffing is also known as byte stuffing.
o In byte stuffing, special byte that is basically known as ESC (Escape Character)
that has predefined pattern is generally added to data section of the data stream or frame
when there is message or character that has same pattern as that of flag byte.
 Bit Stuffing
o Bit stuffing is a process of inserting non-information bits into the data to be
transferred.
o It breaks the data sequence and helps in synchronization.
o Most protocols use the 8-bit pattern 01111110 as flag.
o In order to differentiate the message from the flag in case of same sequence, a single
bit is stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the
message, an extra 0 bit is stuffed at the end of the five 1s.

ALGORITHM:
BIT STUFFING

1. Start.

2. Initialize the array for transmitted stream with the special bit pattern 0111 1110 which indicates the
beginning of the frame.

3. Get the bit stream to be transmitted in to the array.

4. Check for five consecutive ones and if they occur, stuff a bit 0.

5. Display the data transmitted as it appears on the data line after appending 0111 1110 at the end.

6. For de−stuffing, copy the transmitted data to another array after detecting the stuffed bits.

7. Display the received bit stream.


8. Stop.

CHARACTER STUFFING
1. Start.
2. Initialize the array for transmitted stream.
3. Get the characters to be transmitted in to the array.
4. Get the character that represents starting and ending delimiter.
5. Display the characters after stuffing.
6. Stop.

PROGRAM:

BIT STUFFING

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int x[50], y[50], n;
int i, j, k, count = 1;

printf("Enter size of a bit string:");


scanf("%d", &n);

printf("Enter the bit string(0's &1's):");


for (i = 0; i < n; i++)
{
scanf("%d", &x[i]);
}
i = 0;
j = 0;
while (i < n)
{
if (x[i] == 1)
{
y[j] = x[i];

//count is less than 5 as 0 is inserted after every 5 consecutive 1's


for (k = i + 1; x[k] == 1 && k < n && count < 5; k++)
{
j++;
y[j] = x[k];
count++;
if (count == 5)
{
j++;
y[j] = 0;
}

i = k;
}
}
else
{
y[j] = x[i];
}

i++;
j++;
}
//Displaying final result
printf("Result of Bit Stuffing:");
for (i = 0; i < j; i++)
{
printf("%d", y[i]);
}
getch();
}

OUTPUT:

Enter the size of a bit string:10

Enter the bit string (0’s & 1’s): 1 0 0 1 1 1 1 1 1 0

Result of Bit Stuffing: 1 0 0 1 1 1 1 1 0 1 0

CHARACTER STUFFING

#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;
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);
}
OUTPUT:

Enter characters to be stuffed: haveagoodday

Enter a character that represents starting delimiter: a

Enter a character that represents ending delimiter: y

After stuffing: ahaaveaagooddaayyy

RESULT:

Thus the Data Link Layer framing methods have been implemented and the output was verified.
EX: 2 DATE: ERROR DETECTION / CORRECTION TECHNIQUES

OBJECTIVE: To implement the following Error Detection / Correction Techniques

(i) CRC (ii) Hamming code

RESOURCE: 1. Pentium – PC

2. C Compiler

THEORY:

 Data-link layer uses some error control mechanism to ensure that frames (data bit streams) are
transmitted with certain level of accuracy. But to understand how errors is controlled, it is essential to
know what types of errors may occur.

 Error control mechanism may involve two possible ways:

Error detection
Error correction

Error Detection
 Longitudinal redundancy check (LRC) is an error-detection method for determining the correctness of
transmitted and stored data. LRC verifies the accuracy of stored and transmitted data using parity bits.

 CRC is a different approach to detect if the received frame contains valid data. This technique involves
binary division of the data bits being sent. The divisor is generated using polynomials. The sender
performs a division operation on the bits being sent and calculates the remainder. Before sending the
actual bits, the sender adds the remainder at the end of the actual bits. Actual data bits plus the
remainder is called a codeword. The sender transmits data bits as codewords.

Error Correction
 Hamming code is used for the set of error-correction codes which may occur when the data is moved
from the sender to the receiver. The hamming method corrects the error by finding the state at which
the error has occurred. The hamming method uses the extra parity bits to allow the identification of a
single-bit error.

ALGORITHM:

CRC:

1. Multiply M(x) by highest power in G(x). i.e. Add so much zeros to M(x).

2. Divide the result by G(x). The remainder = C(x).


3. If: x div y gives remainder c that means: x = n y + c Hence (x-c) = n y (x-c) div y gives
remainder 0 Here (x-c) = (x+c) Hence (x+c) div y gives remainder 0
4. Transmit: T(x) = M(x) + C(x)

5. Receiver end: Receive T(x). Divide by G(x), should have remainder 0.


HAMMING

1. First write the bit positions starting from 1 in a binary form (1, 10, 11,100, etc.).
2. Mark all the bit positions that are powers of two as parity bits (1, 2, 4, 8, 16, 32, 64, etc.).
3. All other bit positions are for the data to be encoded using (3, 5, 6, 7, 9, 10 and 11, etc.).

PROGRAM:

CRC

#include<stdio.h>

#include<string.h>

#define N strlen(gen_poly)

char data[30];

char check_value[30];

char gen_poly[10];

int data_length,i,j;

void XOR()

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

check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');

void receiver()

printf("Enter the received data: ");

scanf("%s", data);

printf("\n-----------------------------\n");

printf("Data received: %s", data);

crc();

for(i=0;(i<N-1) && (check_value[i]!='1');i++);

if(i<N-1)

printf("\nError detected\n\n");

else

printf("\nNo error detected\n\n");

void crc()

{
for(i=0;i<N;i++)

check_value[i]=data[i];

do{

if(check_value[0]=='1')

XOR();

for(j=0;j<N-1;j++)

check_value[j]=check_value[j+1];

check_value[j]=data[i++];

}while(i<=data_length+N-1);

int main()

printf("\nEnter data to be transmitted: ");

scanf("%s",data);

printf("\n Enter the Generating polynomial: ");

scanf("%s",gen_poly);

data_length=strlen(data);

for(i=data_length;i<data_length+N-1;i++)

data[i]='0';

printf("\n----------------------------------------");

printf("\n Data padded with n-1 zeros : %s",data);

printf("\n----------------------------------------");

crc();

printf("\nCRC or Check value is : %s",check_value);

for(i=data_length;i<data_length+N-1;i++)

data[i]=check_value[i-data_length];

printf("\n----------------------------------------");

printf("\n Final data to be sent : %s",data);

printf("\n----------------------------------------\n");

receiver();

return 0;
}
Output:

Enter data to be transmitted: 1001101

Enter the Generating polynomial: 1011

----------------------------------------

Data padded with n-1 zeros : 1001101000

----------------------------------------

CRC or Check value is : 101

----------------------------------------

Final data to be sent : 1001101101

----------------------------------------

Enter the received data: 1001101101

-----------------------------

Data received: 1001101101

No error detected

In the case of error,

Enter the received data: 1001001101

-----------------------------

Data received: 1001001101

Error detected

HAMMING CODES

#include<stdio.h>

void main() {

int data[7],rec[7],i,c1,c2,c3,c;

printf("this works for message of 4bits in size \nenter message bit one by one: ");

scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);

data[6]=data[0]^data[2]^data[4];

data[5]=data[0]^data[1]^data[4];

data[3]=data[0]^data[1]^data[2];
printf("\nthe encoded bits are given below: \n");

for (i=0;i<7;i++) {

printf("%d ",data[i]);

printf("\nenter the received data bits one by one: ");

for (i=0;i<7;i++) {

scanf("%d",&rec[i]);

c1=rec[6]^rec[4]^rec[2]^rec[0];

c2=rec[5]^rec[4]^rec[1]^rec[0];

c3=rec[3]^rec[2]^rec[1]^rec[0];

c=c3*4+c2*2+c1 ;

if(c==0) {

printf("\ncongratulations there is no error ");

} else {

printf("\nerron on the postion: %d\nthe correct message is \n",c);

if(rec[7-c]==0)

rec[7-c]=1; else

rec[7-c]=0;

for (i=0;i<7;i++) {

printf("%d ",rec[i]);

}
OUTPUT:

Enter message bit one by one: 0

The encoded bits are given below:

0101101

Enter the received data bits one by one: 0 1 0 1 1 0 1

Congratulations there is no error

RESULT:

Thus various error detection and correction techniques have been implemented using C
Compiler and the output was verified successfully.
Ex. 3. DATE: Go back-N and Selective Repeat Protocols

OBJECTIVE:

To implement Go back-N and Selective Repeat Protocols using C.

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

Go-Back-N protocol, also called Go-Back-N Automatic Repeat reQuest, is a data link layer protocol
that uses a sliding window method for reliable and sequential delivery of data frames. It is a case of sliding
window protocol having to send window size of N and receiving window size of 1

Go – Back – N ARQ provides for sending multiple frames before receiving the acknowledgment for the
first frame. The frames are sequentially numbered and a finite number of frames. The maximum number of
frames that can be sent depends upon the size of the sending window. If the acknowledgment of a frame is not
received within an agreed upon time period, all frames starting from that frame are retransmitted.

In the selective repeat, the sender sends several frames specified by a window size even without the need
to wait for individual acknowledgement from the receiver as in Go-Back-N ARQ. In selective repeat protocol,
the retransmitted frame is received out of sequence.
In Selective Repeat ARQ only the lost or error frames are retransmitted, whereas correct frames are
received and buffered.

PROGRAM

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<unistd.h>

int n,r;

struct frame

char ack;
int data;

}frm[10];

int sender(void);

void recvack(void);

void resend(void);

void resend1(void);

void goback(void);

void selective(void);

int main()

int c;

do

printf("\n\n1.Selective repeat ARQ\n2.Goback ARQ\n3.exit");

printf("\nEnter your choice:");

scanf("%d",&c);

switch(c)

case 1:selective();

break;

case 2:goback();

break;

case 3:exit(0);

break;

}while(c>=4);

void goback()

sender();

recvack();

resend1();
printf("\n all packets sent successfully\n");

void selective()

sender();

recvack();

resend();

printf("\nAll packets sent successfully");

int sender()

int i;

printf("\nEnter the no. of packets to be sent:");

scanf("%d",&n);

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

printf("\nEnter data for packets[%d]",i);

scanf("%d",&frm[i].data);

frm[i].ack='y';

return 0;

void recvack()

int i;

rand();

r=rand()%n;

frm[r].ack='n';

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

if(frm[i].ack=='n')

printf("\nThe packet number %d is not received\n",r);

}
}

void resend() //SELECTIVE REPEAT

printf("\nresending packet %d",r);

sleep(2);

frm[r].ack='y';

printf("\nThe received packet is %d",frm[r].data);

void resend1() //GO BACK N

int i;

printf("\n resending from packet %d",r);

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

sleep(2);

frm[i].ack='y';

printf("\nReceived data of packet %d is %d",i,frm[i].data);

OUTPUT:

1.Selective repeat ARQ

2.Goback ARQ

3.exit

Enter your choice: 2

Enter the no. of packets to be sent: 6

Enter data for packets[1] aaa

Enter data for packets[2] bbb

Enter data for packets[3] ccc

Enter data for packets[4] ddd

Enter data for packets[5] eee

Enter data for packets[6] fff

The packet number 3 is not received


resending from packet 3

Received data of packet 2 is bbb

Received data of packet 3 is ccc

Received data of packet 4 is ddd

Received data of packet 5 is eee

Received data of packet 6 is fff

all packets sent successfully

1.Selective repeat ARQ

2.Goback ARQ

3.exit

Enter your choice:1

Enter the no. of packets to be sent:5

Enter data for packets[1]aaa

Enter data for packets[2] bbb

Enter data for packets[3] ccc

Enter data for packets[4] ddd

Enter data for packets[5] eee

The packet number 4 is not received

resending packet 4

The received packet is ddd

All packets sent successfully.

RESULT:

Thus Go back-N and Selective Repeat Protocols were implemented using C and the output was
verified.
Ex: 4. DATE: Distance Vector Routing

OBJECTIVE:

Obtain Routing table at each node using distance vector routing algorithm for a given subnet.

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

Distance vector routing algorithm is also called as Bellman-Ford algorithm or Ford Fulkerson
algorithm as this algorithm is used to find the shortest route from one node to another node in the
network.

The Distance Vector routing algorithm (DVR) shares the information of the routing table with
the other routers in the network and keeps the information up-to-date to select an optimal path from
source to destination.

The Bellman-Ford algorithm is defined as:

𝑑𝑥 (𝑦) = 𝑚𝑖𝑛𝑣 {𝑐 (𝑥, 𝑣) + 𝑑𝑣 (𝑦)}

where𝑑𝑥 (𝑦)= The least distance from x to y


𝑐 (𝑥, 𝑣)= Node x's cost from each of its neighbour v
𝑑𝑣 (𝑦)= Distance to each node from initial node
𝑚𝑖𝑛𝑣 = selecting shortest distance

The distance vector routing algorithm works by having each router maintain a routing table,
giving the best-known distance from source to destination and which route is used to get there.

These tables are updated by exchanging the information with the neighbor having a direct link.
Tables contain one entry for each route; this entry contains two-part, the preferred outgoing line use to
reach the destination or an estimate of the time or distance to that destination.

The router exchanges the network topology information periodically with its neighboring node
and updates the information in the routing table.
ALGORITHM:

1. Start
2. Accept the input distance matrix from the user that represents the distance between each node in the
network.
3. Store the distance between nodes in a suitable variable.
4. Calculate the minimum distance between two nodes by iterating.
 If the distance between two nodes is larger than the calculated alternate available path, replace
the existing distance with the calculated distance.
5. Print the shortest path calculated.
6. Stop.

PROGRAM:

#include<stdio.h>

struct node {

unsigned dist[20];

unsigned from[20];

}rt[10];

int main() {

int dmat[20][20],n,i,j,k,count=0;

printf("\nEnter the number of nodes : ");

scanf("%d",&n);

printf("\nEnter the cost matrix :\n");

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

for(j=0;j<n;j++ ) {

scanf("%d",&dmat[i][j]);

dmat[i][i]=0;

rt[i].dist[j]=dmat[i][j];

rt[i].from[j]=j; }

do { count=0;

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

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

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

if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]) {
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<n;i++) {

printf("\nState value for router %d is \n",i+1);

for(j=0;j<n;j++) {

printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);


}

printf("\n");

}}

OUTPUT:

Enter the number of nodes: 4

Enter the cost matrix:

4210

2134

2461

2141

State value for router 1 is

node 1 via 1 Distance0

node 2 via 4 Distance1

node 3 via 3 Distance1

node 4 via 4 Distance0

State value for router 2 is

node 1 via 1 Distance2

node 2 via 2 Distance0

node 3 via 3 Distance3

node 4 via 1 Distance2

State value for router 3 is


node 1 via 1 Distance2

node 2 via 4 Distance2

node 3 via 3 Distance0

node 4 via 4 Distance1

State value for router 4 is

node 1 via 1 Distance2

node 2 via 2 Distance1

node 3 via 1 Distance3

node 4 via 4 Distance0

RESULT:

Thus the Routing table was obtained at each node using distance vector routing algorithm for
the given subnet.
Ex: 5 DATE: Link State Routing Algorithm

OBJECTIVE:

To implement Link State Routing algorithm (Open Shortest Path First) with 5 nodes
(Dijkstra's).

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

In link state routing, each router shares its knowledge of its neighborhood with every other router in the
internet work.

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

ALGORITHM:

 Discover the neighbors and learn their network addresses.


 Measure the delay or cost to each of the neighbours.
 Construct a packet telling all it has just learned – Building link state packets.
 Send this packet to all other routers – Distributing the link state packets.
 Compute the shortest path to every other router.

PROGRAM:

#include <stdio.h>

#include <string.h>

int main()

int count,src_router,i,j,k,w,v,min;

int cost_matrix[100][100],dist[100],last[100];

int flag[100];

printf("\n Enter the no of routers");

scanf("%d",&count);
printf("\n Enter the cost matrix values:");

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

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

printf("\n%d->%d:",i,j);

scanf("%d",&cost_matrix[i][j]);

if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;

printf("\n Enter the source router:");

scanf("%d",&src_router);

for(v=0;v<count;v++)

flag[v]=0;

last[v]=src_router;

dist[v]=cost_matrix[src_router][v];

flag[src_router]=1;

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

min=1000;

for(w=0;w<count;w++)

if(!flag[w])

if(dist[w]<min)

v=w;
min=dist[w];

flag[v]=1;

for(w=0;w<count;w++)

if(!flag[w])

if(min+cost_matrix[v][w]<dist[w])

dist[w]=min+cost_matrix[v][w];

last[w]=v;

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

printf("\n%d==>%d:Path taken:%d",src_router,i,i);

w=i;

while(w!=src_router)

printf("\n<--%d",last[w]);w=last[w];

printf("\n Shortest path cost:%d",dist[i]);

}
OUTPUT:

Enter the no of routers 5

Enter the cost matrix values:

0->0:5

0->1:2

0->2:3

0->3:4

0->4:1

1->0:2

1->1:3

1->2:1

1->3:8

1->4:4

2->0:2

2->1:1

2->2:3

2->3:5

2->4:6

3->0:1

3->1:0

3->2:1

3->3:0

3->4:3

4->0:5

4->1:1

4->2:6

4->3:2

4->4:0

Enter the source router:2

2==>0:Path taken:0

<--2

Shortest path cost:2


2==>1:Path taken:1

<--2

Shortest path cost:1

2==>2: Path taken: 2

Shortest path cost: 3

2==>3: Path taken: 3

<--2

Shortest path cost: 5

2==>4: Path taken: 4

<--0

<--2

Shortest path cost: 3

RESULT:

Thus the Link State Routing algorithm (Open Shortest Path First) with 5 nodes
(Dijkstra's) was implemented and the shortest path was found.
Ex: 6 DATE: DATA ENCRYPTION AND DECRYPTION

OBJECTIVE:

To implement Data encryption and decryption using Data Encryption Standard(DES)


algorithm.

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

DES is a block cipher and encrypts data in blocks of size of 64 bits each, which means 64 bits of plain text
go as the input to DES, which produces 64 bits of ciphertext.

Discarding of every 8th bit of the key produces a 56-bit key from the original 64-bit key.
DES is based on the two fundamental attributes of cryptography: substitution (also called confusion) and
transposition

 In the first step, the 64-bit plain text block is handed over to an initial Permutation (IP) function.
 The initial permutation is performed on plain text.
 Next, the initial permutation (IP) produces two halves of the permuted block; saying Left Plain Text
(LPT) and Right Plain Text (RPT).
 Now each LPT and RPT go through 16 rounds of the encryption process.
 In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on the combined
block
 The result of this process produces 64-bit ciphertext.

ALGORITHM:
ENCRYPTION

Get the text to be encrypted (plain text) and key text.

i) Find the length of the plain text.


ii) For i=1 to length of plain text
iii) Find the binary equivalent of ith character of plain text.
iv) Find the binary equivalent of ith character of key text
v) Find the XOR of above two values.

The resulting value will be the encrypted format (cipher text) of the plain text.

DECRYPTION

Get the text to be decrypted (cipher text) and key text.

Find the length of the cipher text.

For i=1 to length of cipher text

i) Find the binary equivalent of ith character of cipher text.


ii) Find the binary equivalent of ith character of key text
iii) Find the XOR of above two values.

The resulting value will be the decrypted format (original plain text) of the cipher plain text.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
int IP[] =
{
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
int E[] =
{
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
int P[] =
{
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};

int FP[] =
{
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
int S1[4][16] =
{
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
};
int S2[4][16] =
{
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
};
int S3[4][16] =
{
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
};
int S4[4][16] =
{
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
};
int S5[4][16] =
{
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
};
int S6[4][16] =
{
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
};
int S7[4][16]=
{
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
};
int S8[4][16]=
{
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
int PC1[] =
{
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
int PC2[] =
{
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
int SHIFTS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
FILE* out;
int LEFT[17][32], RIGHT[17][32];
int IPtext[64];
int EXPtext[48];
int XORtext[48];
int X[8][6];
int X2[32];
int R[32];
int key56bit[56];
int key48bit[17][48];
int CIPHER[64];
int ENCRYPTED[64];
void expansion_function(int pos, int text)
{
for (int i = 0; i < 48; i++)
{
if (E[i] == pos + 1) {
EXPtext[i] = text;
}
}
}

int initialPermutation(int pos, int text)


{
int i;
for (i = 0; i < 64; i++)
{
if (IP[i] == pos + 1) {
break;
}
}
IPtext[i] = text;
}
int F1(int i)
{
int r, c, b[6];

for (int j = 0; j < 6; j++) {


b[j] = X[i][j];
}
r = b[0] * 2 + b[5];
c = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];

if (i == 0) {
return S1[r][c];
}
else if (i == 1) {
return S2[r][c];
}
else if (i == 2) {
return S3[r][c];
}
else if (i == 3) {
return S4[r][c];
}
else if (i == 4) {
return S5[r][c];
}
else if (i == 5) {
return S6[r][c];
}
else if (i == 6) {
return S7[r][c];
}
else if (i == 7) {
return S8[r][c];
}
}
int XOR(int a, int b) {
return (a ^ b);
}
int ToBits(int value)
{
int k, j, m;
static int i;
if (i % 32 == 0) {
i = 0;
}

for (j = 3; j >= 0; j--)


{
m = 1 << j;
k = value & m;
if (k == 0) {
X2[3 - j + i] = '0' – 48;
}
else {
X2[3 - j + i] = '1' – 48;
}
}
i = i + 4;
}

int SBox(int XORtext[])


{
int k = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 6; j++) {
X[i][j] = XORtext[k++];
}
}
int value;
for (int i = 0; i < 8; i++)
{
value = F1(i);
ToBits(value);
}
}
int PBox(int pos, int text)
{
int i;
for (i = 0; i < 32; i++)
{
if (P[i] == pos + 1) {
break;
}
}
R[i] = text;
}
void cipher(int Round, int mode)
{
for (int i = 0; i < 32; i++) {
expansion_function(i, RIGHT[Round – 1][i]);
}
for (int i = 0; i < 48; i++)
{
if (mode == 0) {
XORtext[i] = XOR(EXPtext[i], key48bit[Round][i]);
}
else {
XORtext[i] = XOR(EXPtext[i], key48bit[17 – Round][i]);
}
}
SBox(XORtext);
for (int i = 0; i < 32; i++) {
PBox(i, X2[i]);
}
for (int i = 0; i < 32; i++) {
RIGHT[Round][i] = XOR(LEFT[Round – 1][i], R[i]);
}
}
void finalPermutation(int pos, int text)
{
int i;
for (i = 0; i < 64; i++)
{
if (FP[i] == pos + 1) {
break;
}
}
ENCRYPTED[i] = text;
}
void convertToBinary(int n)
{
int k, m;
for (int i = 7; i >= 0; i--)
{
m = 1 << i;
k = n & m;

if (k == 0) {
fprintf(out, "0");
}
else {
fprintf(out, "1");
}
}
}
int convertCharToBit(long int n)
{
FILE* inp = fopen("input.txt", "rb");
out = fopen("bits.txt", "wb+");
char ch;
int i = n * 8;
while (i)
{
ch = fgetc(inp);
if (ch == -1) {
break;
}
i--;
convertToBinary(ch);
}
fclose(out);
fclose(inp);
}
void Encryption(long int plain[])
{
out = fopen("cipher.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}
for (int i = 0; i < 32; i++) {
LEFT[0][i] = IPtext[i];
}
for (int i = 32; i < 64; i++) {
RIGHT[0][i – 32] = IPtext[i];
}
for (int k = 1; k < 17; k++)
{
cipher(k, 0);
for (int i = 0; i < 32; i++)
LEFT[k][i] = RIGHT[k – 1][i];
}
for (int i = 0; i < 64; i++)
{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
}
else {
CIPHER[i] = LEFT[16][i – 32];
}
finalPermutation(i, CIPHER[i]);
}
for (int i = 0; i < 64; i++) {
fprintf(out, "%d", ENCRYPTED[i]);
}
fclose(out);
}
void Decryption(long int plain[])
{
out = fopen("decrypted.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}
for (int i = 0; i < 32; i++) {
LEFT[0][i] = IPtext[i];
}
for (int i = 32; i < 64; i++) {
RIGHT[0][i – 32] = IPtext[i];
}
for (int k = 1; k < 17; k++)
{
cipher(k, 1);

for (int i = 0; i < 32; i++) {


LEFT[k][i] = RIGHT[k – 1][i];
}
}
for (int i = 0; i < 64; i++)
{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
} else {
CIPHER[i] = LEFT[16][i – 32];
}
finalPermutation(i, CIPHER[i]);
}
for (int i = 0; i < 64; i++) {
fprintf(out, "%d", ENCRYPTED[i]);
}
fclose(out);
}
void convertToBits(int ch[])
{
int value = 0;
for (int i = 7; i >= 0; i--) {
value += (int)pow(2, i) * ch[7 – i];
}
fprintf(out, "%c", value);
}
int bittochar()
{
out = fopen("result.txt", "ab+");
for (int i = 0; i < 64; i = i + 8) {
convertToBits(&ENCRYPTED[i]);
}
fclose(out);
}
void key56to48(int round, int pos, int text)
{
int i;
for (i = 0; i < 56; i++)
{
if (PC2[i] == pos + 1) {
break;
}
}
key48bit[round][i] = text;
}
int key64to56(int pos, int text)
{
int i;
for (i = 0; i < 56; i++)
{
if (PC1[i] == pos + 1) {
break;
}
}
key56bit[i] = text;
}
void key64to48(unsigned int key[])
{
int k, backup[17][2];
int CD[17][56];
int C[17][28], D[17][28];

for (int i = 0; i < 64; i++) {


key64to56(i, key[i]);
}
for (int i = 0; i < 56; i++)
{
if (i < 28) {
C[0][i] = key56bit[i];
}
else {
D[0][i – 28] = key56bit[i];
}
}
for (int x = 1; x < 17; x++)
{
int shift = SHIFTS[x – 1];
for (int i = 0; i < shift; i++) {
backup[x - 1][i] = C[x – 1][i];
}
for (int i = 0; i < (28 – shift); i++) {
C[x][i] = C[x – 1][i + shift];
}
k = 0;
for (int i = 28 – shift; i < 28; i++) {
C[x][i] = backup[x – 1][k++];
}
for (int i = 0; i < shift; i++) {
backup[x - 1][i] = D[x – 1][i];
}
for (int i = 0; i < (28 – shift); i++) {
D[x][i] = D[x – 1][i + shift];
}
k = 0;
for (int i = 28 – shift; i < 28; i++) {
D[x][i] = backup[x – 1][k++];
}
}
for (int j = 0; j < 17; j++)
{
for (int i = 0; i < 28; i++) {
CD[j][i] = C[j][i];
}
for (int i = 28; i < 56; i++) {
CD[j][i] = D[j][i – 28];
}
}
for (int j = 1; j < 17; j++)
{
for (int i = 0; i < 56; i++) {
key56to48(j, i, CD[j][i]);
}
}
}
void decrypt(long int n)
{
FILE* in = fopen("cipher.txt", "rb");
long int plain[n * 64];
int i = -1;
char ch;
while (!feof(in))
{
ch = getc(in);
plain[++i] = ch – 48;
}
for (int i = 0; i < n; i++)
{
Decryption(plain + i * 64);
bittochar();
}
fclose(in);
}
void encrypt(long int n)
{
FILE* in = fopen("bits.txt", "rb");
long int plain[n * 64];
int i = -1;
char ch;
while (!feof(in))
{
ch = getc(in);
plain[++i] = ch – 48;
}
for (int i = 0; i < n; i++) {
Encryption(plain + 64 * i);
}
fclose(in);
}
void create16Keys()
{
FILE* pt = fopen("key.txt", "rb");
unsigned int key[64];
int i = 0, ch;
while (!feof(pt))
{
ch = getc(pt);
key[i++] = ch – 48;
}
key64to48(key);
fclose(pt);
}
long int findFileSize()
{
FILE* inp = fopen("input.txt", "rb");
long int size;
if (fseek(inp, 0L, SEEK_END)) {
perror("fseek() failed");
}
// size will contain number of chars in the input file.
else {
size = ftell(inp);
}
fclose(inp);
return size;
}
int main()
{
// destroy contents of these files (from previous runs, if any)
out = fopen("result.txt", "wb+");
fclose(out);
out = fopen("decrypted.txt", "wb+");
fclose(out);
out = fopen("cipher.txt", "wb+");
fclose(out);
create16Keys();
long int n = findFileSize() / 8;
convertCharToBit(n);
encrypt(n);
decrypt(n);
return 0;
}
RESULT:

Thus Data encryption and decryption using Data Encryption Standard(DES) algorithm was
implemented.
Ex: 7 DATE: DATA ENCRYPTION AND DECRYPTION

OBJECTIVE:

To implement Data encryption and decryption using RSA (Rivest, Shamir and Adleman)
algorithm.

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works
on two different keys i.e. Public Key and Private Key.
The Public key is used for encryption, and the Private Key is used for decryption. Decryption cannot be
done using a public key. The two keys are linked, but the private key cannot be derived from the public key.
The public key is well known, but the private key is secret and it is known only to the user who owns the key. It
means that everybody can send a message to the user using user's public key. But only the user can decrypt the
message using his private key.

o The data to be sent is encrypted by sender A using the public key of the intended receiver
o B decrypts the received cipher text using its private key, which is known only to B. B replies to A
encrypting its message using A's public key.
o A decrypts the received cipher text using its private key, which is known only to him.

ALGORITHM:
 Ask the user to enter two prime numbers and validate them.
 Store the prime numbers in variables.
 Compute n = pq.
 Compute λ(n) = (p – 1)(q – 1).
 Choose a random number e as a relatively prime number to λ(n) and 1 < e < λ(n).
 Compute d = e-1 mod λ(n).
 Print the public and private keys.
 Ask the user to enter a message and store it in a variable.
 Encrypt the message using the public key.
 Decrypt the message using the private key.
 Print the encrypted and decrypted message.
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &x);
flag = prime(x);
if(flag == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER SECOND PRIME NUMBER\n");
scanf("%d", &y);
flag = prime(y);
if(flag == 0 || x == y)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s",msg);
for(i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = x * y;
t = (x-1) * (y-1);
encryption_key();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i = 0; i < j-1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for(i = 2; i <= j; i++)
{
if(pr % i == 0)
return 0;
}
return 1;
}
//function to generate encryption key
void encryption_key()
{
int k;
k = 0;
for(i = 2; i < t; i++)
{
if(t % i == 0)
continue;
flag = prime(i);
if(flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if(flag > 0)
{
d[k] = flag;
k++;
}
if(k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while(1)
{
k = k + t;
if(k % a == 0)
return(k / a);
}
}
//function to encrypt the message
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while(i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\n\nTHE ENCRYPTED MESSAGE IS\n");
for(i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
//function to decrypt the message
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1)
{
ct = temp[i];
k = 1;
for(j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for(i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
OUTPUT:

ENTER FIRST PRIME NUMBER

ENTER SECOND PRIME NUMBER

11

ENTER MESSAGE OR STRING TO ENCRYPT

Haveaniceday

POSSIBLE VALUES OF e AND d ARE

3 107

7 23

13 37

19 59

23 7

29 149

31 31

37 13

41 121

43 67

47 143

THE ENCRYPTED MESSAGE IS

bsozcMqX0REWfuO2LfIJTw==

THE DECRYPTED MESSAGE IS

Haveaniceday

RESULT:

Thus encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm was
implemented and the output was verified.
Ex: 8 DATE: Client Server Model using FTP

OBJECTIVE:

To implement Client Server Model using FTP protocol.

RESOURCE:

1. Pentium – PC

2. C Compiler

THEORY:

FTP works on a client-server model. The FTP client is a program that runs on the user’s
computer to enable the user to talk to and get files from remote computers. It is a set of commands
that establishes the connection between two hosts, helps to transfer the files, and then closes the
connection.

When an FTP connection is established, there are two types of communication channels are
also established and they are known as command channel and data channel. The command channel
is used to transfer the commands and responses from client to server and server to client.

The FTP client using the URL gives the FTP command along with the FTP server address.
As soon as the server and the client get connected to the network, the user logins using User ID and
password. If the user is not registered with the server, then also he/she can access the files by using
the anonymous login where the password is the client’s email address. The server verifies the user
login and allows the client to access the files. The client transfers the desired files and exits the
connection. The figure below shows the working of FTP.

ALGORITHM:

Client

The client performs the following functions.

1. Start the program


2. Declare the variables and structures required.
3. A socket is created and the connect function is executed.
4. The file is opened.
5. The data from the file is read and sent to the server.
6. The socket is closed.
7. The program is stopped.
Server

The server performs the following functions.

1. Start the program.


2. Declare the variables and structures required.
3. The socket is created using the socket function.
4. The socket is binded to the specific port.
5. Start listening for the connections.
6. Accept the connection from the client.
7. Create a new file.
8. Receives the data from the client.
9. Write the data into the file.
10. The program is stopped.

PROGRAM:

CLIENT:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024
void send_file(FILE *fp, int sockfd){
int n;
char data[SIZE] = {0};
while(fgets(data, SIZE, fp) != NULL) {
if (send(sockfd, data, sizeof(data), 0) == -1) {
perror("[-]Error in sending file.");
exit(1);
}
bzero(data, SIZE);
}
}
int main(){
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd;
struct sockaddr_in server_addr;
FILE *fp;
char *filename = "send.txt";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(e == -1) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Connected to Server.\n");
fp = fopen(filename, "r");
if (fp == NULL) {
perror("[-]Error in reading file.");
exit(1);
}

send_file(fp, sockfd);
printf("[+]File data sent successfully.\n");
printf("[+]Closing the connection.\n");
close(sockfd);
return 0;
}

SERVER:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024

void write_file(int sockfd){


int n;
FILE *fp;
char *filename = "recv.txt";
char buffer[SIZE];

fp = fopen(filename, "w");
while (1) {
n = recv(sockfd, buffer, SIZE, 0);
if (n <= 0){
break;
return;
}
fprintf(fp, "%s", buffer);
bzero(buffer, SIZE);
}
return;
}
int main(){
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd, new_sock;
struct sockaddr_in server_addr, new_addr;
socklen_t addr_size;
char buffer[SIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(e < 0) {
perror("[-]Error in bind");
exit(1);
}
printf("[+]Binding successfull.\n");
if(listen(sockfd, 10) == 0){
printf("[+]Listening....\n");
}else{
perror("[-]Error in listening");
exit(1);
}
addr_size = sizeof(new_addr);
new_sock = accept(sockfd, (struct sockaddr*)&new_addr, &addr_size);
write_file(new_sock);
printf("[+]Data written in the file successfully.\n");
return 0;
}

RESULT:

Thus Client Server model was implemented using FTP protocol.


Ex: 9 DATE: STUDY OF NS2 COMMANDS

OBJECTIVE:
To study the various NS2 commands.

THEORY :

What is NS2 ?

It is a network simulator with a package of tools that simulates behaviour of networks, create
network topologies,Log events that happen under any load, analyse events to understand the network
behaviour.

What is NS ?

It is Object-oriented, discrete event-driven network simulator, Written in C++ and Tcl languages.

Creating Topologies:

Nodes

Set properties like queue length, location Protocols, routing algorithms

Links

Set types of link – Simplex, duplex, wireless, satellite Set bandwidth, latency etc.Done through TCL
Scripts.
NS-2 Generic Script Structure:

1. Create Simulator object

2. [Turn on tracing]

3. Create topology

4. [Setup packet loss, link dynamics]

5. Create routing agents

6. Create application and/or traffic sources

7. Post-processing procedures (i.e. NAM)

8. Start simulation

Step 1: Create Simulator Object

Create event scheduler

set ns [new Simulator]

Step 2: Tracing

Insert immediately after scheduler!


Trace packets on all links

set nf [open out.nam w]

$ns trace-all $nf

$ns namtrace-all $nf

Step 3: Create Network

Nodes
set n0 [$ns node] set n1 [$ns node]

Links and queuing

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


$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>

<queue_type>: DropTail, RED, etc.


Step 4: Post-Processing Procedures

Add a 'finish' procedure that closes the trace file and starts nam.

proc finish {} { global ns nf

$ns flush-traceclose $nf

exec nam out.nam & exit 0

Run Simulation

Schedule Events

$ns at <time> <event>

<event>: any legitimate ns/tcl commands

$ns at 0.5 "$cbr start"

$ns at 4.5 "$cbr stop"

Call ‘finish’

$ns at 5.0 "finish"

Run the simulation

$ns run
NAM Interface:

Nodes:
Color

$node color red

Shape (can’t be changed after sim starts)

$node shape box (circle, box, hexagon)

Links:
Color
$ns duplex-link-op $n0 $n1 color "green"
Label

$ns duplex-link-op $n0 $n1 label “backbone"

Topology Layout:

“Manual” layout: specify everything

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

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

$ns duplex-link-op $n3 $n4 orient 60deg

Creating the Topology:

#create a new simulator object

set ns [new Simulator]

#open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#define a 'finish' procedure proc finish {} { global ns nf

$ns flush-trace

#close the trace file

close $nf

#execute nam on the trace file

exec nam out.nam & exit 0

#create two nodes

set n0 [$ns node] set n1 [$ns node]


#create a duplex link between the nodes

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


#Setup a TCP connection

set tcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

#create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

#create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

#create a Null agent(a traffic sink) and attach it to node n1

set null0 [new Agent/Null]

$ns attach-agent $n1 $null0


#Connect the traffic source to the sink

$ns connect $udp0 $null0

#Schedule events for FTP traffic

$ns at 0.5 "$ftp0 start"

$ns at 4.5 "$ftp0 stop"

#call the finish procedure after 5 secs of simulated time

$ns at 5.0 "finish"

#run the simulation

$ns run

Steps to write a TCL script:

Open ‘CONTEXT’ -> Select file->new file-> write the TCL code ->

Steps to save a TCL script:

Click save -> open program files-> ns-allinone-2.33-> create a new folder -> save withfilename.tcl

RESULT :

Thus the basic ns2 commands are studied.


Ex: 10.A. DATE: NETWORK TOPOLOGY - STAR TOPOLOGY

OBJECTIVE:

To build and simulate a network under star topology.

HARDWARE / SOFTWARE REQUIREMENTS:

Pentium – PC

NS-2

THEORY:

Star networks are one of the most common computer network topologies. In its simplest form, a star
network consists of one central switch, hub or computer, which acts as a conduit to transmit messages. This
consists of a central node, to which all other nodes are connected; this central node provides a common
connection point for all nodes through a hub. In star topology, every node (computer workstation or any other
peripheral) is connected to a central node called a hub or switch. The switch is the server and the peripherals are
the clients. Thus, the hub and leaf nodes, and the transmission lines between them, form a graph with the
topology of a star. If the central node is passive, the originating node must be able to tolerate the reception of
an echo of its own transmission, delayed by the two-way transmission time (i.e. to and from the central node)
plus any delay generated in the central node. An active star network has an active central node that usually has
the means to prevent echo-related problems.

The star topology reduces the damage caused by line failure by connecting all of the systems to a central
node. When applied to a bus-based network, this central hub rebroadcasts all transmissions received from any
peripheral node to all peripheral nodes on the network, sometimes including the originating node. All peripheral
nodes may thus communicate with all others by transmitting to, and receiving from, the central node only. The
failure of a transmission line linking any peripheral node to the central node will result in the isolation of that
peripheral node from all others, but the rest of the systems will be unaffected.

ALGORITHM:

1. Create a simulator object


2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on
tracefile.
4. Create six nodes that forms a network numbered from 0 to 5
5. Create duplex links between the nodes to form a STAR Topology
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP
8. Schedule events and run the program.
PROGRAM:

#Create a simulator object


set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure proc finish {} {
global ns nf
$ns flush-trace
#Close the trace
file close $nf
#Executenam on the trace file
exec nam out.nam &
exit0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Change the shape of center node in a star topology
$n0 shape square
#Create links between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n0 $n5 1Mb 10ms DropTail
#Create a TCP agent and attach it to node n0
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0
# Create a CBR traffic source and attach it to tcp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 finish"
#Run the simulation
$ns run

OUTPUT:

RESULT:

Thus the star topology was simulated and studied.


Ex:10 B DATE: NETWORK TOPOLOGY – BUS TOPOLOGY

OBJECTIVE:

To build and simulate a network under bus topology

HARDWARE / SOFTWARE REQUIREMENTS:

1. Pentium – PC
2. NS-2

THEORY:

Bus topology definition is, this is one of the simplest physical topology used for the network.
This topology is famously used for the Local Area Network. In this topology, all the nodes are
connected through a single cable known as ‘Backbone’. If this Backbone cable is damaged the entire
network breakdowns.

The node that transmits data is known as Host. All the computers connected in the network
will receive all the network traffic. Each node is given equal priority for data transmission. The nodes
use Media Access Technology such as a bus master to share the bus.

ALGORITHM:

1. Create a simulator object


2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on
tracefile.
4. Create six nodes that forms a network numbered from 0 to 5
5. Create duplex links between the nodes to form a STAR Topology
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP
8. Schedule events and run the program.
PROGRAM:

#Create a simulator object

set ns [new Simulator]

#Open the nam trace file


set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam on the trace file
exec nam out.nam &
exit 0
}

#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4 $n5" 0.5Mb 80ms LL Queue/DropTail MAC/Csma/Cd
Channel]

#Create a TCP agent and attach it to node n0


set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink


$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 5
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents


$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation


$ns run

OUTPUT:

RESULT:

Thus the bus topology was simulated and studied.


Ex:10 C DATE: NETWORK TOPOLOGY – RING TOPOLOGY

OBJECTIVE:

To build and simulate a network under ring topology

HARDWARE / SOFTWARE REQUIREMENTS:

1. Pentium – PC
2. NS-2

THEORY:

A ring topology is a network configuration where device connections create a


circular data path. Each networked device is connected to two others, like points on a circle. Together,
devices in a ring topology are called a ring network.

In a ring network, packets of data travel from one device to the next until they reach their
destination. Most ring topologies allow packets to travel only in one direction, called
a unidirectional ring network. Others permit data to move in either direction, called bidirectional.

ALGORITHM:

1. Create a simulator object


2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on
tracefile.
4. Create six nodes that forms a network numbered from 0 to 5
5. Create duplex links between the nodes to form a STAR Topology
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP
8. Schedule events and run the program.
PROGRAM:

#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf


#Define a 'finish'
procedureproc finish {}

global ns nf

$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &exit 0
}

#Create four nodes


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Create links between the nodes


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

#Create a TCP agent and attach it to node n0


set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink


$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents


$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Run the simulation


$ns run

OUTPUT:

RESULT:

Thus the bus topology was simulated and studied.


EX: 11. DATE: CSMA/CA AND CSMA/CD PROTOCOLS

OBJECTIVE:

To implement and perform the operation of CSMA/CD and CSMA/CA using NS2.

HARDWARE / SOFTWARE REQUIREMENTS:

1. Pentium – PC
2. NS-2

THEORY:

Ethernet is a LAN (Local area Network) protocol operating at the MAC (Medium Access Control) layer.
Ethernet has been standardized as per IEEE 802.3. The underlying protocol in Ethernet is known as the CSMA
/ CD – Carrier Sense Multiple Access / Collision Detection. The working of the Ethernet protocol is as explained
below, a node which has data to transmit senses the channel. If the channel is idle then, the data is transmitted.
If the channel is busy then, the station defers transmission until the channel is sensed to be idle and then
immediately transmitted. If more than one node starts data transmission at the same time, the data collides.
This collision is heard by the transmitting nodes which enter into contention phase. The contending nodes
resolve contention using an algorithm called truncated binary exponential back off.

ALGORITHM:

1. Create a simulator object


2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on
tracefile.
4. Create six nodes that forms a network numbered from 0 to 5
5. Create duplex links between the nodes and add Orientation to the nodes for setting a LAN topology
6. Setup TCP Connection between n(0) and n(4)
7. Apply FTP Traffic over TCP
8. Setup UDP Connection between n(1) and n(5)
9. Apply CBR Traffic over UDP.
10. Apply CSMA/CA and CSMA/CD mechanisms and study their performance
11. Schedule events and run the program.

PROGRAM:

CSMA/CA

set ns [new Simulator]


#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files
set file1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace file
set file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam
&exit 0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n1 color red
$n1 shape box
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Ca Channel]
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP
connectionset ftp [new
Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connectionset udp [new Agent/UDP]
$ns attach-agent $n1
$udpset null [new
Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file}
{global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
set wnd [$tcpSource set
window_]puts $file "$now
$cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet
drop\""# PPP
$ns at 125.0 "finish"
$ns run

OUTPUT:

CSMA/CD

set ns [new Simulator]


#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files
set file1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace file
set file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish'
procedureproc finish {} {
global ns file1 file2
$ns flush- trace
close $file1
close $file2
exec nam out.nam
&exit 0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n1 color red
$n1 shape box
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel]
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP connectionset ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP #Setup a UDP connectionset udp [new Agent/UDP]
$ns attach-agent $n1
$udpset null [new Agent/Null]
$ns attach-agent
$n5
$null
$ns connect
$udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file}
{global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
set wnd [$tcpSource set window_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet drop\""
# PPP
$ns at 125.0 "finish"
$ns run

OUTPUT:

RESULT:

Thus CSMA/CD and CSMA/CA was implemented and its operation was performed.
EX: 12. DATE: STOP N WAIT PROTOCOL

OBJECTIVE:

To write a TCL simulation script to generate a simple 2 node network with a stop-n-wait protocol on
lossless and lossy TCP and UDP connections.

HARDWARE / SOFTWARE REQUIREMENTS:

1. Pentium – PC
2. NS-2

THEORY:

In stop-n-wait protocol the sender sends frame 0 and then waits for the acknowledgement before
proceeding for thenext frame. The receiver sends the acknowledgment 1 (ACK1) and so on.

ALGORITHM:

Step 1: Create simulator object

Step 2: Add nodes[set node parameters]

Step 3: Add links [set link parameters]

Step 4: Add agents [set agent parameters]

Step 5: Create connections[set connection parameters]

Step 6: Add traffic [set traffic parameters & insert error mode]

Step 7: Create event schedule [create control loops/ procedures]

Step 8: Run simulation

PROGRAM:

set ns [new Simulator]

set n0 [$ns node]

set n1 [$ns node]

$ns at 0.0 "$n0 label Sender"

$ns at 0.0 "$n1 label Receiver"

set nf [open E5-stop-n-wait-loss.nam w]

$ns namtrace-all $nf

set f [open E5-stop-n-wait-loss.tr w]

$ns trace-all $f

$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail


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

$ns duplex-link-op $n0 $n1 queuePos 0.5

$ns queue-limit $n0 $n1 10 Agent/TCP


set nam_tracevar_ true
set tcp [new Agent/TCP]

$tcp set window_ 1

$tcp set maxcwnd_ 1

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns add-agent-trace $tcp tcp

$ns monitor-agent-trace $tcp

$tcp tracevar cwnd_

$ns at 0.1 "$ftp start"

$ns at 1.3 "$ns queue-limit $n0 $n1 0"

$ns at 1.5 "$ns queue-limit $n0 $n1 10"

$ns at 3.0 "$ns detach-agent $n0 $tcp; $ns detach-agent $n1 $sink"

$ns at 3.5 "finish"

$ns at 0.0 "$ns trace-annotate \"stop and wait with Packet Loss\""

$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""

$ns at 0.11 "$ns trace-annotate \"Send Packet_0\""

$ns at 0.35 "$ns trace-annotate \"Receive Ack_1\""

$ns at 0.56 "$ns trace-annotate \"Send Packet_1\""

$ns at 0.76 "$ns trace-annotate \"Receive Ack_2\""

$ns at 0.99 "$ns trace-annotate \"Send Packet_2\""

$ns at 1.23 "$ns trace-annotate \"Receive Ack_3\""

$ns at 1.43 "$ns trace-annotate \"Lost Packet_3\""

$ns at 1.5 "$ns trace-annotate \"Waiting for Ack_4\""


$ns at 2.43 "$ns trace-annotate \"Send Packet_3 again(cause of timeout)\""

$ns at 2.67 "$ns trace-annotate \"Receive Ack_4\""

$ns at 2.88 "$ns trace-annotate \"Send Packet_4\""

$ns at 3.1 "$ns trace-annotate \"FTP stops\""

proc finish {}

global ns nf

$ns flush-trace close $nf

puts "filtering..." puts "running nam..."

exec nam E5-stop-n-wait-loss.nam & exit 0

$ns run
OUTPUT :

RESULT:

Thus TCL simulation script to generate a simple network with stop-n-wait protocol on lossless and lossy
TCP andUDP connection is done.

You might also like