Networks Lab Manual
Networks Lab Manual
YEAR - II SEMESTER – IV
REGULATION 2021
TABLE OF CONTENT
CYCLE-I
Implement the Data Link Layer framing
1 methods
CYCLE-II
Aim:
To write a C program to implement data link layer framing methods using bit stuffing
and charact er stuffing
APPARATUS REQUIRED:
1. PC
2. Compiler C
THEORY:
Dat a link layer is responsible for somet hing called Framing, which is t he divisio n
of stream of bit s from t he network layer int o manageable unit s (called frames ).
Each frame consist s of t he sender’s address and a dest inat ion address. The
dest inat ion address defines where t he packet is to go and t he sender’s address
helps t he recipient acknowledge t he receipt . Frames could be of fixed size or
var iable size. In fixed-size framing, t here is no need for defining t he boundaries
of t he frames as t he size it self can be used to define t he end of t he frame and t he
beginning of t he next frame. But, in variable -size framing, we need a way to
define t he end of t he frame and t he beginning of t he next frame. To separate one
frame from t he next , an 8-bit (or 1-byt e) flag is added at t he beginning and t he
end of a frame. But t he problem wit h t hat is, any patt ern used for t he flag could
also be part of t he informat ion. So, t here ar e two ways to overcome t his problem:
Using Byt e st uffing (or character stuffing)
Using Bit st uffing
PROGRAM:
#include<stdio.h>
#include<string.h>
main()
{
int i,j,count=0,nl;
char str[100];
printf("enter the bit string:");
gets(str);
for(i=0;i<strlen(str);i++)
{
count=0;
for(j=i;j<=(i+5);j++)
{
if(str[j]=='1')
{
count++;
}
SJCE/ECE/EC3401\N&S LAB
}
if(count==6)
{
nl=strlen(str)+2;
for(;nl>=(i+5);nl--)
{
str[nl]=str[nl-1];
}
str[i+5]='0';
i=i+7;
}
}
puts(str);
}
OUTPUT:
BIT STUFFING
ENTER THE BIT STRING: 000111111001
000111110001
CHARACTER STUFFING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
clrscr();
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the character\n");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
SJCE/ECE/EC3401\N&S LAB
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);
getch();
}
SJCE/ECE/EC3401\N&S LAB
INPUT:
enter string:
asdlefgh
enter position: 8
invalid position, enter again: 3
enter the character: k
OUTPUT:
frame after stuffing:
dlestx as dle k dle dle dlefgh dleetx
Result:
Thus the C program for Data Link Layer framing methods of bit stuffing
and character stuffing was implemented and executed successfully.
SJCE/ECE/EC3401\N&S LAB
EXP NO:2
Implementation of Error Detection / Correction Techniques i) LRC, (ii)
CRC (iii) Hamming code
AIM:
To write a C program to implement Error Detection / Correction Techniques
i) LRC (ii) CRC (iii) Hamming code
APPARATUS REQUIRED:
1. PC
2. Compiler C
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.
THEORY:
Longitudinal Redundancy Check (LRC) is the error detection method which
is used by upper layers to detect error in data. The other name for LRC is 2-D
parity check. In this method, data which the user want to send is organized into
tables of rows and columns. To detect an error, a redundant bit is added to the
whole block after addition this block is transmitted to receiver side. This redundant
bit is used by receiver to detect error. If there is no error, receiver accepts the data
and discards the redundant row of bits.
A cyclic redundancy check (CRC) is an error-detecting code commonly used
in digital networks and storage devices to detect accidental changes to digital data.
Blocks of data entering these systems get a short check value attached, based on
the remainder of a polynomial division of their contents. On retrieval, the
calculation is repeated and, in the event the check values do not match, corrective
action can be taken against data corruption.
Hamming code is an error correction system that can detect and correct
errors when data is stored or transmitted. It requires adding additional parity bits
with the data. It is commonly used in error correction code (ECC) RAM.
PROGRAM:
CRC CODE:
#include<stdio.h>
SJCE/ECE/EC3401\N&S LAB
int a[100],b[100],i,j,len,k,count=0;
//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};
int main()
{
void div();
system("clear");
printf("\nEnter the length of Data Frame :");
scanf("%d",&len);
printf("\nEnter the Message :");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
//Append r(16) degree Zeros to Msg bits
for(i=0;i<16;i++)
a[len++]=0;
//Xr.M(x) (ie. Msg+16 Zeros)
for(i=0;i<len;i++)
b[i]=a[i];
//No of times to be divided ie. Msg Length
k=len-16;
div();
for(i=0;i<len;i++)
b[i]=b[i]^a[i]; //MOD 2 Subtraction
printf("\nData to be transmitted : ");
for(i=0;i<len;i++)
printf("%2d",b[i]);
printf("\n\nEnter the Received Data : ");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
div();
for(i=0;i<len;i++)
if(a[i]!=0)
{
printf("\nERROR in Recived Data");
return 0;
}
printf("\nData Recived is ERROR FREE");
}
void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
SJCE/ECE/EC3401\N&S LAB
}
count=0;
}
}
OUTPUT:
Enter the length of Data Frame :4
Enter the Message :1 0 1 1
Data to be transmitted : 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the Reveived Data : 1 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1
ERROR in Recived Data
Reminder is : 0000000100000000
HAMMING CODE:
#include<stdio.h>
#include<conio.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];
for (i=0;i<7;i++) {
printf("%d ",data[i]);
for (i=0;i<7;i++) {
scanf("%d",&rec[i]);
SJCE/ECE/EC3401\N&S LAB
}
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) {
} else {
if(rec[7-c]==0)
rec[7-c]=1; else
rec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d ",rec[i]);
getch();
SJCE/ECE/EC3401\N&S LAB
OUTPUT:
SJCE/ECE/EC3401\N&S LAB
Result:
Thus the C program for Error Detection / Correction Techniques using
LRC, CRC and Hamming code was implemented and executed successfully.
SJCE/ECE/EC3401\N&S LAB
EXP NO:3
Implementation of Stop and Wait and Sliding Window Protocols
AIM:
To write a C program to implement Stop and Wait & Sliding Window Protocols
APPARATUS REQUIRED:
1. PC
2. Compiler C
THEORY:
The Stop-and-Wait protocol uses both flow and error control. In this protocol, the sender
sends one frame at a time and waits for an acknowledgment before sending the next one. To
detect corrupted frames, we need to add a CRC to each data frame. When a frame arrives at the
receiver site, it is checked. If its CRC is incorrect, the frame is corrupted and silently discarded.
The silence of the receiver is a signal for the sender that a frame was either corrupted or lost.
Every time the sender sends a frame, it starts a timer. If an acknowledgment arrives before the
timer expires, the timer is stopped and the sender sends the next frame (if it has one to send). If
the timer expires, the sender resends the previous frame, assuming that the frame was either lost
or corrupted. This means that the sender needs to keep a copy of the frame until its
acknowledgment arrives. When the corresponding acknowledgment arrives, the sender discards
the copy and sends the next frame if it is ready. Only one frame and one acknowledgment can
be in the channels at any time. The advantages are Limited buffer size, sooner Errors detection
and prevents one station occupying medium for long periods.
PROGRAM:
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
SJCE/ECE/EC3401\N&S LAB
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("\n Acknowledgement of above frames sent is received by sender\n");
return 0;
}
OUTPUT
2467
3467
4467
With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)
After sending 2 frames at each stage sender waits for acknowledgement sent by the receiver
1467 2467
SJCE/ECE/EC3401\N&S LAB
Acknowledgement of above frames sent is received by sender
3467 4467
RESULT:
Thus the “Sliding window” and “Stop and Wait” protocol programmed using C is
implemented successfully.
SJCE/ECE/EC3401\N&S LAB
EXP NO:4
Implementation of Go back-N and Selective Repeat Protocols
AIM:
To write a C program to implement Go back-N and Selective Repeat Protocols
APPARATUS REQUIRED:
1. PC
2. Compiler C
PRINCIPLE:
SJCE/ECE/EC3401\N&S LAB
j1=win;
for(i=0;i<frame;i++)
{
a[i]=rand();
}
k=1;
while(i1<frame)
{
if((frame-i1)<win)
j1=frame-i1;
printf("\n\ntransmit the window no %d\n\n",k);
c1=send(i1,i1+j1);
ack=receive(i1,i1+j1,c1);
if (ack!=0)
{
printf("\n\n1.Selective window\n");
printf("2.Go back N\n");
scanf("%d",&ack);
switch(ack)
{
case 1:
printf("\n\n\t Selective window \t\nEnter the faulty frame no\n");
scanf("%d",&i2);
printf("\n\n Retransmit the frame %d \n",i2);
send(i2,i2+1);
break;
case 2:
printf("\n\n\t Go back n\t\n\n");
printf("\nRetransmit the frames from %d to %d\n",i1,i1+j1);
send(i1,i1+j1);
break;
}
}
i1=i1+win;
k++;
}
}
int send(c,d)
{
int t1;
for(i=c;i<d;i++)
{
b[i]=a[i];
printf("frame %d is sent\n",i);
}
s=checsum(&a[c]);
SJCE/ECE/EC3401\N&S LAB
return(s); }
int receive(c,d,c2)
int c2;
{
r=checsum(&b[c]);
if(c2==r)
{
return(0);
}
else
return(1);
}
frame 0 is sent
frame 1 is sent
frame 2 is sent
frame 3 is sent
frame 4 is sent
frame 5 is sent
frame 6 is sent
frame 7 is sent
frame 8 is sent
frame 9 is sent
SJCE/ECE/EC3401\N&S LAB
frame 10 is sent
frame 11 is sent
frame 12 is sent
frame 13 is sent
frame 14 is sent
frame 15 is sent
frame 16 is sent
frame 17 is sent
frame 18 is sent
frame 19 is sent
frame 20 is sent
frame 21 is sent
frame 22 is sent
frame 23 is sent
frame 24 is sent
1.Selective window
2.Go back N
RESULT:
Thus the working of selective repeat and Go Back n were implemented and
understood in C language.
SJCE/ECE/EC3401\N&S LAB
EXP NO:5
Implementation of Distance Vector Routing algorithm (Routing
Information Protocol) (Bellman-Ford).
AIM:
To write a C program to implement of Distance Vector Routing algorithm
APPARATUS REQUIRED:
1. PC
2. Compiler C
PRINCIPLE:
It is under dynamic routing algorithm. This algorithm operates by having
each route maintains a table giving the least known distance to reach destination
and include line in used to get these. These are updated by changing information
with neighbor. This is called “Bell mann ford algorithm” and “fod fick”
algorithm.
DISTANCE VECTOR ROUTING
FIRST PROGRAM
/*Distance Vector Routing in this program is implemented using Bellman Ford Algorithm:-
*/
#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
{
SJCE/ECE/EC3401\N&S LAB
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
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
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
*/ SECOND PROGRAM
SJCE/ECE/EC3401\N&S LAB
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int 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("\n\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\n");
}
Output :
Enter the number of nodes : 4
SJCE/ECE/EC3401\N&S LAB
Enter the cost matrix:
0 3 5 99
3 0 99 1
5402
99 1 2 0
State value for router 1 is
node 1 via 1 Distance0
node 2 via 2 Distance3
node 3 via 3 Distance5
node 4 via 2 Distance4
State value for router 2 is
node 1 via 1 Distance3
node 2 via 2 Distance0
node 3 via 4 Distance3
node 4 via 4 Distance1
State value for router 3 is
node 1 via 1 Distance5
node 2 via 4 Distance3
node 3 via 3 Distance0
node 4 via 4 Distance2
State value for router 4 is
node 1 via 2 Distance4
node 2 via 2 Distance1
node 3 via 3 Distance2
node 4 via 4 Distance0
RESULT:
Thus the distance vector routing algorithm was implemented and the output was verified.
SJCE/ECE/EC3401\N&S LAB
EXP NO:6
Implementation of Link State Routing algorithm (Open Shortest
Path First) with 5 nodes
AIM:
To write a C program to implement Link State Routing algorithm
APPARATUS REQUIRED:
1. PC
2. Compiler C
PRINCIPLE:
Link state routing works on the following principle.
1. Discover the neighbor and keep their network address.
2. Measure the delay or cost to each of its neighbor.
3. Construct a packet telling all it has just learned.
4. Send the packet to all routers.
5. Compute the shortest path to every router.
LINK STATE ROUTING:
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
SJCE/ECE/EC3401\N&S LAB
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through nextnode
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
SJCE/ECE/EC3401\N&S LAB
printf("<-%d",j);
}
while(j!=startnode);
}
}
Dijkstra’s Algorithm
1. Create cost matrix C[ ][ ] from adjacency matrix adj[ ][ ]. C[i][j] is the cost of going from
vertex i to vertex j. If there is no edge between vertices i and j then C[i][j] is infinity.
2. Array visited[ ] is initialized to zero.
for(i=0;i<n;i++)
visited[i]=0;
1. If the vertex 0 is the source vertex then visited[0] is marked as 1.
2. Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-1 from the
source vertex 0.
for(i=1;i<n;i++)
distance[i]=cost[0][i];
3. Initially, distance of source vertex is taken as 0. i.e. distance[0]=0;
4. for(i=1;i<n;i++) a. Choose a vertex w, such that distance[w] is minimum and visited[w] is 0.
Mark visited[w] as 1.
b. Recalculate the shortest distance of remaining vertices from the source.
c. Only, the vertices not marked as 1 in array visited[ ] should be considered for recalculation of
distance. i.e. for each vertex v
if(visited[v]==0)
distance[v]=min(distance[v],
distance[w]+cost[w][v]) [w]+cost[w][v])
RESULT:
Thus the link state algorithm was implemented and the output was verified.
SJCE/ECE/EC3401\N&S LAB
EXP NO:7
Data encryption and decryption using Data Encryption Standard
algorithm.
AIM:
To write a C program to implement Data encryption and decryption using Data
Encryption Standard algorithm.
APPARATUS REQUIRED:
1. PC
2. Compiler C
PRINCIPLE:
CRYPTOGRAPHY (secret writing) is the process which uses the encryption
and decryption algorithm. An encryption algorithm transforms the plain-text into
cipher text (unknown format) and decryption algorithm transforms the cipher text
back into plain-text. The sender uses an encryption algorithm to send the
information in the unknown format and the receiver uses a decryption algorithm
to retrieve the original information.
ALGORITHM:
CLIENT:
1. Start the program
2. Initialize the socket for connection establishment
3. Write the message
4. Convert the message to hex code format
5. Display the message to the server
6. Close all objects
7. Stop
SERVER:
1. Start the program
2. Initialize the server socket
3. Display waiting for connection
4. Display hex code received from client
5. Initialize a new string builder
6. Transform the hex code into the string format using the string builder
7. Display the decrypted message
8. Close all objects
9. Stop
SJCE/ECE/EC3401\N&S LAB
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;
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;
}
OUTPUT:
ENCRYPTION:
SJCE/ECE/EC3401\N&S LAB
DECRYPTION:
RESULT:
Thus the process of Encryption and Decryption is implemented.
SJCE/ECE/EC3401\N&S LAB
EXP NO:8
Data encryption and decryption using RSA (Rivest, Shamir and
Adleman) algorithm.
AIM:
To write a C program to implement Data encryption and decryption using RSA
(Rivest, Shamir and Adleman) algorithm.
APPARATUS REQUIRED:
1. PC
2. Compiler C
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
void main()
{
int a, b, i, j, t, x, n, k = 0, flag = 0, prime[100];
char m[20], pp[20];
float p[20], c[20];
double e, d;
clrscr();
for(i = 0; i < 50; i++)
{
flag = 0;
for(j = 2; j < i / 2; j++)
if(i % j == 0)
{
flag = 1;
break;
}
if(flag == 0)
prime[k++] = i;
}
a = prime[k - 1];
b = prime[k - 2];
n = a * b;
t = (a - 1) * (b - 1);
e=(double)prime[2];
d=1 / (float)e;
printf("\nKey of encryption is:%lf", d);
printf("\nEnter the text:");
scanf("%s", &m);
x = strlen(m);
SJCE/ECE/EC3401\N&S LAB
printf("\nSource------------destination");
printf("\nChar\tnumeric\tcipher\t\tnumeric\t\tchar\n");
for(i = 0; i < x; i++)
{
printf("%c", m[i]);
printf("\t%d", m[i] - 97);
c[i] = pow(m[i] - 97, (float)e);
c[i] = fmod(c[i], (float)n);
printf("\t%f", c[i]);
p[i] = pow(c[i], (float)d);
p[i] = fmod(p[i], (float)n);
printf("\t%f", p[i]);
pp[i] = p[i] + 97;
printf("\t%c\n", pp[i]);
}
getch();
}
OUTPUT:
Source------------destination
Char numeric cipher numeric char
g 6 36.000000 6.000000 g
e 4 16.000000 4.000000 e
e 4 16.000000 4.000000 e
t 19 361.000000 19.000000 t
h 7 49.000000 7.000000 h
a 0 0.000000 0.000000 a
n 13 169.000000 13.000000 n
j 9 81.000000 9.000000 j
a 0 0.000000 0.000000 a
l 11 121.000000 11.000000 l
i 8 64.000000 8.000000 i
RESULT:
Thus the process of Encryption and Decryption using RSA (Rivest, Shamir and Adleman)
algorithm is implemented.
SJCE/ECE/EC3401\N&S LAB
EXP NO:9
Implement Client Server model using FTP protocol.
AIM:
To write a C program to implement Client Server model using FTP protocol.
APPARATUS REQUIRED:
1. PC
2. Compiler C
ALGORITHM:
SERVER:
STEP1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Bind the IP address and Port number
STEP 6: Listen and accept the client’s request for the connection
STEP 7: Establish the connection with the client
STEP 8: Close the socket
STEP 9: Stop
CLIENT:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Call the connect() function
STEP 6: Close the socket
STEP 7: Stop
PROGRAM:
SERVER:
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
SJCE/ECE/EC3401\N&S LAB
#define MAX 60
int i, j, tem;
char buff[4096], t;
FILE *f1;
int main(int afg, char *argv)
{
int sockfd, newsockfd, clength;
struct sockaddr_in serv_addr,cli_addr;
char t[MAX], str[MAX];
strcpy(t,"exit");
sockfd=socket(AF_INET, SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nBinded");
bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("\nListening...");
listen(sockfd, 5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*) &cli_addr,&clength);
close(sockfd);
read(newsockfd, &str, MAX);
printf("\nClient message\n File Name : %s\n", str);
f1=fopen(str, "r");
while(fgets(buff, 4096, f1)!=NULL)
{
write(newsockfd, buff,MAX);
printf("\n");
}
fclose(f1);
printf("\nFile Transferred\n");
return 0;
}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
SJCE/ECE/EC3401\N&S LAB
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
int main(int arg,char*argv[])
{
int sockfd,n;
struct sockaddr_in serv_addr;
struct hostent*server;
char send[MAX],recvline[MAX],s[MAX],name[MAX];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nEnter the source file name : \n");
scanf("%s",send);
write(sockfd,send,MAX);
while((n=read(sockfd,recvline,MAX))!=0)
{
printf("%s",recvline);
}
close(sockfd);
return 0;
}
SJCE/ECE/EC3401\N&S LAB
OUTPUT:
SERVER:
CLIENT:
SJCE/ECE/EC3401\N&S LAB
RESULT:
Thus the client server model using FTP protocol is implemented.
SJCE/ECE/EC3401\N&S LAB
EXP NO:10 Implement and realize the Network Topology - Star, Bus and Ring
using NS2
AIM:
To build and simulate a network under different topologies like
Star, bus & ring and observe the performance parameters.
REQUIREMENTS:
Operating System: Windows NT/2000/XP or
LINUX Programming Tool: Network
Simulator (NS2)
THEORY:
Network topology refers to the physical or logical layout of a network. It defines the
way different nodes are placed and interconnected with each other. Alternately,
network topology may describe how the data is transferred between these nodes.
There are two types of network topologies: physical and logical. Physical topology
emphasizes the physical layout of the connected devices and nodes, while the logical
topology focuses on the pattern of data transfer between network nodes.
The physical and logical network topologies of a network do not necessarily have to
be identical. However, both physical and network topologies can be categorized into
five basic models:
Bus Topology: All the devices/nodes are connected sequentially to the same
backbone or transmission line. This is a simple, low-cost topology, but its single
point of failure presents a risk.
Star Topology: All the nodes in the network are connected to a central device like a
hub or switch via cables. Failure of individual nodes or cables does not necessarily
create downtime in the network but the failure of a central device can. This topology
is the most preferred and popular model.
Ring Topology: All network devices are connected sequentially to a backbone as in
bus topology except that the backbone ends at the starting node, forming a ring. Ring
topology shares many of bus topology's disadvantages so its use is limited to
networks that demand high throughput.
PROCEDURE:
1. Open a terminal and type the TCL file in the vim editor with the
command “vifilename.tcl” and save it.
2. Run the saved file with the command “ns filename.tcl”.
3. If any errors, edit them in the vim editor and rerun it again.
4. The output is displayed in the nam console.
SJCE/ECE/EC3401\N&S LAB
STAR TOPOLOGY
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n0 shape square
$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
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
BUS TOPOLOGY
#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
SJCE/ECE/EC3401\N&S LAB
$ns flush-trace
#Close the trace file
close $nf
#Executenam 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]
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
RING TOPOLOGY
#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 {} {
SJCE/ECE/EC3401\N&S LAB
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam 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
SJCE/ECE/EC3401\N&S LAB
RING TOPOLOGY
RESULT:
Thus the network topologies like star, bus and ring have been implemented
using network simulator.
SJCE/ECE/EC3401\N&S LAB
EXP NO:11 Implement and perform the operation of CSMA/CD and CSMA/CA
using NS2
AIM:
To implement and perform the operation of CSMA/CD and CSMA/CA using NS2
REQUIREMENTS:
Operating System: Windows NT/2000/XP or LINUX
Programming Tool: Network Simulator (NS2)
THEORY:
Carrier Sense Multiple Access (CSMA) is a network protocol that listens to or
senses network signals on the carrier/medium before transmitting any data. CSMA is
implemented in Ethernet networks with more than one computer or network device
attached to it. CSMA is part of the Media Access Control (MAC) protocol. CSMA works
on the principle that only one device can transmit signals on the network, otherwise a
collision will occur resulting in the loss of data packets or frames. CSMA works when a
device needs to initiate or transfer data over the network. Before transferring, each
CSMA must check or listen to the network for any other transmissions that may be in
progress. If it senses a transmission, the device will wait for it to end.
Once the transmission is completed, the waiting device can transmit its
data/signals. However, if multiple devices access it simultaneously and a collision occurs,
they both have to wait for a specific time before reinitiating the transmission process.
CSMA protocol was developed to overcome the problem found in ALOHA i.e. to
minimize the chances of collision, so as to improve the performance. CSMA protocol is
based on the principle of 'carrier sense'.
The station senses the carrier or channel before transmitting a frame. It means the
station checks the state of channel, whether it is idle or busy. Even though devices
attempt to sense whether the network is in use, there is a good chance that two stations
will attempt to access it at the same time. On large networks, the transmission time
between one end of the cable and another is enough that one station may access the cable
even though another has already just accessed it. The chances of collision still exist
because of propagation delay. The frame transmitted by one station takes some time to
reach other stations. In the meantime, other stations may sense the channel to be idle and
transmit their frames. This results in the collision.
ALGORITHM:
Step 1 : Start.
Step 2 : Create a simulator object.
Step 3 : Configure the simulator to use Link state routing.
Step 4 : Open the nam trace file.
Step 5 : Open the output file.
Step 6 : Define the finish procedure.
Step 7 : Close the trace file.
SJCE/ECE/EC3401\N&S LAB
Step 8 : Create the required nodes.
Step 10 : Create links between the nodes.
Step 11 : Create a UDP agent to attach the node.
Step 12 : Create a CBR traffic router and attach.
Step 13 : Create a Null agent to the traffic sink.
Step 14 : Connect the traffic source to the sink.
Step 15 : Schedule the events for CBR agent.
Step 16 : Stop.
PROGRAM:
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
$n5 color red
$n5 shape box
SJCE/ECE/EC3401\N&S LAB
#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_ 80
$tcp set packetSize_ 5
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $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_ 100
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 2.0 "$ftp start"
$ns at 3.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
$ns at 5.0 "finish"
$ns run
SJCE/ECE/EC3401\N&S LAB
OUTPUT:
RESULT:
Thus the operation of CSMA/CD and CSMA/CA using NS2 was implemented
successfully.
SJCE/ECE/EC3401\N&S LAB