0% found this document useful (0 votes)
73 views

CNP LAB Programs

The document describes experiments on data communication techniques including bit stuffing, character stuffing, cyclic redundancy check codes, encryption algorithms, and minimum spanning tree algorithms. It provides C code implementations of: 1) Bit stuffing and character stuffing algorithms to encode data for transmission and reconstruct the original data. 2) Cyclic redundancy check codes to generate and verify error detection codes for transmitted data. 3) Substitution and transposition encryption algorithms to encrypt and decrypt messages. 4) Kruskal's minimum spanning tree algorithm to find minimum cost spanning trees for graphs.

Uploaded by

Muhammed Akram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

CNP LAB Programs

The document describes experiments on data communication techniques including bit stuffing, character stuffing, cyclic redundancy check codes, encryption algorithms, and minimum spanning tree algorithms. It provides C code implementations of: 1) Bit stuffing and character stuffing algorithms to encode data for transmission and reconstruct the original data. 2) Cyclic redundancy check codes to generate and verify error detection codes for transmitted data. 3) Substitution and transposition encryption algorithms to encrypt and decrypt messages. 4) Kruskal's minimum spanning tree algorithm to find minimum cost spanning trees for graphs.

Uploaded by

Muhammed Akram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

EXPERIMENT 1

BIT STUFFING AND CHARACTER STUFFING ALGORITHMS AND CYCLIC REDUNDANCY


CHECK CODES FOR ERROR DETECTION

Objective: (a) To simulate bit stuffing and character stuffing algorithms for HDLC frame
Using C program
(b) To simulate Cyclic Redundancy Check codes for Error detection using C
Program.
Program:

(a) Bit stuffing:

#include <stdio.h>
#include <string.h>
int main ()
{
char flag[9] = "01111110";
int n, count = 0, j = 0, i = 1, bit=0;
printf ("Enter the number of data bits:\n");
scanf ("%d", &n);
char data[n + 1], rdata[n + 1];
printf ("Enter data bits:\n");
scanf ("%s", data);
data[n] = '\0';
char tdata[3 * n];
strcpy (tdata, flag);
while (data[j] != '\0')
{
if (data[j] == '1')
{
if (count == 5)
{
tdata[7 + i] = '0';
count = 0;
bit += 1;
i += 1;
}
else
{
tdata[7 + i] = '1';
j += 1;
i += 1;
count += 1;
}
}
else
{
tdata[7 + i] = '0';
j += 1;
i += 1;
count = 0;
}
}
tdata[8 + n + bit] = '\0';
strcat (tdata, flag);
printf ("Transmitted data:\n");
printf ("%s\n", tdata);
count = 0;
int k = 8, l = 0;
while (tdata[k]!='\0')
{
if (tdata[k] == '0')
{
if (count == 5)
{
count = 0;
k += 1;
}
else
{
rdata[l] = '0';
l += 1;
k += 1;
count = 0;
}
}
else if (tdata[k] == '1')
{
rdata[l] = '1';
count += 1;
l += 1;
k += 1;
}
}
rdata[n] = '\0';
printf("Reconstructed data:\n");
printf("%s", rdata);
return 0;
}

Output:
Character stuffing:

#include <stdio.h>
#include <string.h>
int main() {
char data[20],tdata[40],rdata[20];
char sof[9] = "DLE STX ";
char eof[9] = " DLE ETX";
int i = 0, j = 0;
strcpy(tdata,sof);
j = 8;
printf("Enter the data to be transmitted:\n");
gets(data);
while (data[i]!='\0')
{
if(data[i] == 'D' && data[i+1] == 'L' && data[i+2] == 'E')
{
tdata[j] = 'D';
tdata[j+1] = 'L';
tdata[j+2] = 'E';
tdata[j+3] = ' ';
tdata[j+4] = 'D';
tdata[j+5] = 'L';
tdata[j+6] = 'E';
i += 3;
j += 7;
}
else
{
tdata[j] = data[i];
i += 1;
j += 1;
}
}
tdata[j] = '\0';
strcat(tdata,eof);
printf("Transmitted data:\n");
printf("%s\n",tdata);
j = 8;
i = 0;
while(tdata[j]!='\0')
{
if(tdata[j] == 'D' && tdata[j+1] == 'L' && tdata[j+2] == 'E')
{
rdata[i] = 'D';
rdata[i+1] = 'L';
rdata[i+2] = 'E';
j += 7;
i += 3;
}
else
{
rdata [i] = tdata[j];
i += 1;
j += 1;
}
}
rdata[strlen(data)] = '\0';
printf("Reconstructed data:\n");
printf("%s\n",rdata);
return 0;
}

Output:
(b) CRC Codes for error detection:

#include <stdio.h>
int main()
{
int num;
printf("Enter the number of data bits:\n");
scanf("%d",&num);
int data[num+16],tdata[num+16],rdata[num+16];
printf("Enter the data bits, bitwise:\n");
for (int i=0;i<num+16;i++)
{
if (i<num)
{
scanf("%d",&data[i]);
}
else
{
data[i] = 0;
}
}
int gen[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int rem[17];
for(int j=0;j<17;j++)
{
rem[j] = data[j];
}
for(int i=0;i<num;i++)
{
if(rem[0]==1)
{
for(int k=0;k<17;k++)
{
rem[k] = rem[k]^gen[k];
}
}
else
{
for(int k=0;k<17;k++)
{
rem[k] = rem[k]^0;
}
}
for(int k=0;k<16;k++)
{
rem[k] = rem[k+1];
}
rem[16] = data[17+i];
}
printf("Transmitted codeword:\n");
for(int k=0;k<num;k++)
{
tdata[k] = data[k];
printf("%d",tdata[k]);
}
for(int k=num;k<num+16;k++)
{
tdata[k] = rem[k-num];
printf("%d",tdata[k]);
}

printf("\nEnter the received codeword of length %d bits, bitwise:\n",num+16);


for (int i=0;i<num+16;i++)
{
scanf("%d",&rdata[i]);
}
for(int j=0;j<17;j++)
{
rem[j] = rdata[j];
}
for(int i=0;i<num;i++)
{
if(rem[0]==1)
{
for(int k=0;k<17;k++)
{
rem[k] = rem[k]^gen[k];
}
}
else
{
for(int k=0;k<17;k++)
{
rem[k] = rem[k]^0;
}
}
for(int k=0;k<16;k++)
{
rem[k] = rem[k+1];
}
rem[16] = rdata[17+i];
}
rem[16] = 0;
int flag = 1;
for (int i=0;i<17;i++)
{
if (rem[i]!=0)
{
flag = 0;
}
}
if(flag == 1)
{
printf("The received codeword is error free.\n");
}
else
{
printf("The received codeword is not error free.\n");
}
return 0;
}

Output:
EXPERIMENT 2

ENCRYPTION AND DECRYPTION ALGORITHMS

Objective: To simulate Substitution method, Transposition Method Encryption algorithms


using C Program.

Program:

(a) Substitution method:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int n;
char key[27] = "QWERTYUIOPASDFGHJKLZXCVBNM";
printf("Enter the size of the message in bits:\n");
scanf("%d",&n);
char tdata[n+1],encrypted[n+1];
printf("Enter the data:\n");
scanf("%s",tdata);
for (int i=0; i<n; i++)
{
if(isdigit(tdata[i]))
{
encrypted[i] = '9' - tdata[i] + '0';
continue;
}
if(islower(tdata[i]))
{
tdata[i] = toupper(tdata[i]);
}
encrypted[i] = key[tdata[i]-'A'];
}
encrypted[n] = '\0';
printf("Encrypted data:\n");
printf("%s\n",encrypted);

printf("Enter the size of received message in bits:\n");


int m,index;
char key2[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
scanf("%d",&m);
char rdata[m+1],decrypted[m+1];
printf("Enter the received data:\n");
scanf("%s",rdata);
for(int i=0; i<m; i++)
{
if(isdigit(rdata[i]))
{
decrypted[i] = '9' - rdata[i] + '0';
continue;
}
if(islower(rdata[i]))
{
rdata[i] = toupper(rdata[i]);
}
for (int k=0; k<27; k++)
{
if (rdata[i] == key[k])
{
decrypted[i] = key2[k];
}
}
}
decrypted[m] = '\0';
printf("Decrypted data:\n");
printf("%s",decrypted);
return 0;
}

Output:
(b) Transposition method:

#include<stdio.h>
#include<string.h>
int main()
{
char key[8] = "RAINBOW";
char sorted[8] = "ABINORW";
printf("Enter the length of the message:\n");
int n;
scanf("%d",&n);
char tdata[n+1];
printf("Enter the message:\n");
scanf("%s", tdata);
tdata[n] = '\0';
int x;
if(n%7==0)
{
x = n/7;
}
else
{
x = (n/7)+1;
}
char matrix[x][7];
int y = 0;
for(int i=0; i<x; i++)
{
for(int j=0; j<7; j++)
{
if(y<n)
{
matrix[i][j] = tdata[y];
y+=1;
}
else
{
matrix[i][j] = '*';
}
}
}
printf("Matrix:\n");
for(int i=0; i<x; i++)
{
for(int j=0; j<7; j++)
{
printf("%c",matrix[i][j]);
}
printf("\n");
}
int z = x*7+1;
y = 0;
char encrypted[z];
for(int i=0;i<7;i++)
{
for(int j=0;j<7;j++)
{
if(sorted[i] == key[j])
{
for(int b=0;b<x;b++)
{
encrypted[y] = matrix[b][j];
y+=1;
}
}
}
}
encrypted[z] = '\0';
printf("Encrypted data:\n");
printf("%s\n",encrypted);

int p;
printf("Enter the size of message to be decrypted:\n");
scanf("%d",&p);
char rdata[p+1];
printf("Enter the message to be decrypted:\n");
scanf("%s",rdata);
x = p/7;
y = 0;
char dmatrix[x][7];
for(int i=0;i<7;i++)
{
for(int j=0;j<x;j++)
{
dmatrix[j][i] = rdata[y];
y+=1;
}
}
printf("Matrix:\n");
for(int i=0; i<x; i++)
{
for(int j=0; j<7; j++)
{
printf("%c",dmatrix[i][j]);
}
printf("\n");
}
z = x*7+1;
y = 0;
char decrypted[z];
int dec[7] = {5,0,2,3,1,4,6};
for(int i=0;i<7;i++)
{
for(int j=0;j<7;j++)
{
decrypted[y] = dmatrix[i][dec[j]];
y+=1;
}
}
decrypted[z] = '\0';
printf("Decrypted data:\n");
y = 0;
while(decrypted[y]!='*')
{
printf("%c",decrypted[y]);
y+=1;
}
return 0;
}

Output:
EXPERIMENT 3
MINIMUM SPANNING TREE ALGORITHMS

Objective: To simulate Minimum Spanning Tree algorithms using C Program.

Program:

(a) Kruskal’s Algorithm:

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct node
{
int set;
}node[100];

struct edge
{
int first_node,second_node,selected,distance;
}e[100],temp;

int edge_count=0;

void getdata(int index, int total)


{
int i;
for(i=index;i<total;i++)
{
if(i!=index)
{
printf("\nEnter distance between Vertex %c and %c:",index+65,i+65);
scanf("%d",&e[edge_count].distance);
e[edge_count].first_node=index;
e[edge_count].second_node=i;
++edge_count;
}
}
}

void init(int total)


{
int i;
for(i=0;i<total;i++)
node[i].set=i;
for(i=0;i<edge_count;i++)
e[i].selected=-1;
}

void sort()
{
int i,j;
for(i=0;i<edge_count-1;i++)
for(j=0;j<edge_count-i-1;j++)
if(e[j].distance>e[j+1].distance)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp ;
}
}

void main()
{
int i,total,j,k,m,n,edgeselected=0,nodel,noder;

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


scanf("%d",&total);
printf("\nEnter the distance as -1 if the nodes are not connected.\n");
for(i=0;i<total;i++)
getdata(i,total);
init(total);
sort();

printf("\nThe Sorted order of edges:");


for(i=0;i<edge_count;i++)
if(e[i].distance!=-1)
printf("\nedge: %d first node: %c second node: %c distance: %d",i,e[i].first_node+65,e[i
].second_node+65,e[i].distance);

printf("\n");

for(i=0;i<edge_count;i++)
{
if(e[i].distance!=-1)
break;
}

do
{
e[i].selected=1;
nodel=e[i].first_node;
noder=e[i].second_node;
if(node[nodel].set==node[noder].set)
e[i].selected=-1;
else
{
edgeselected++;
m=node[nodel].set;
k=node[noder].set;
for(n=0;n<total;n++)
{
if(node[n].set==k)
node[n].set=m;
}

}
i++;
}while(edgeselected<(total-1));

printf("\nMinimum Spanning Tree is:");


for(i=0;i<edge_count;++i)
{
if(e[i].selected==1)
printf("\n%c<-------
>%c Distance %d",e[i].first_node+65, e[i].second_node+65, e[i].distance);
}
}

Output:
(b) Prim’s Algorithm:

#include<stdio.h>
#define infinity 999

int prime(int cost[10][10],int source,int n)


{
int i,j,sum=0,visited[10],cmp[10],vertex[10];
int min,u,v;
for(i=1;i<=n;i++)
{
vertex[i]=source;
visited[i]=0;
cmp[i]=cost[source][i];
}
visited[source]=1;
for(i=1;i<=n-1;i++)
{
min=infinity;
for(j=1;j<=n;j++)
if(!visited[j] && cmp[j]<min)
{
min=cmp[j];
u=j;
}
visited[u]=1;
sum=sum+cmp[u];
printf("\n%d<---->%d Sum=%d",vertex[u],u,cmp[u]);
for(v=1;v<=n;v++)
if(!visited[v] && cost[u][v] < cmp[v])
{
cmp[v]=cost[u][v];
vertex[v]=u;
}
}
return sum;
}

void main()
{
int a[10][10],n,i,j,m,source;

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


scanf("%d",&n);
printf("\nEnter the cost matrix(0 self loop & 999 no edge):\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]!=a[j][i]||(a[i][i]!=0))
{
printf("\nInvalid entry\nCost matrix should be symmetrical & the diagonal
elements are zeroes.");
}

printf("\nEnter the source:");


scanf("%d",&source);

m=prime(a,source,n);
printf("\n\nTotal cost=%d",m);
}

Output:
EXPERIMENT 6
TESTING AND VERIFICATION OF NETWORK CONFIGURATIONS USING PACKET TRACER

Objective: To cable a network according to the given network topology and test and verify
configurations using packet tracer by using ping commands.
EXPERIMENT 7
CONFIGURATION OF INTER VLAN NETWORK

Objective: Configuring Traditional Inter-VLAN Routing and test and verify configurations
using packet tracer.
EXPERIMENT 8
IMPLEMENT RSA ALGORITHM USING C

Objective: To write a C program to simulate RSA algorithm

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int gcd(int a,int b){
if(b==0){
return a;
}
else{
return gcd(b,a%b);
}
}
int getValue(char c){
return (int)(c-'A');
}
int pow(int base,int exp,int mod){
long long int b = 1;
for(int i=1;i<=exp;i++){
b = (b * base)%mod;
}
b = b%mod;
return (int)b;
}
void getLetter(int c){
c = c % 26;
c = c + 'A';
printf("%c",c);
}
int main()
{
// manual
int p = 11;
int q = 17;
// User Input
/*printf("Enter p and q: ");
scanf("%d,%d"&p,&q);
printf("\n");*/
int n = p*q;
int totient = (p-1)*(q-1);
int e = 0;
printf("p: %d q: %d\n",p,q);
printf("n: %d\n",n);
printf("Totient: %d\n",totient);
// manual
for(int i=2;i<totient;i++){
if(gcd(i,totient)==1){
e = i;
break;
}
}
// User Input
/*printf("Enter e: ");
scanf("%d",&e);
printf("\n");*/
printf("Public Key e: %d\n",e);
int k=1;
while((k*totient+1)%e!=0){
k++;
}
int d = (k*totient+1)/e;
printf("Private Key d: %d\n",d);
char message[] = "ATTACK";
// User Input
/*printf("Enter Message: ");
scanf("%s",message);*/
printf("Plaintext Message: %s\n",message);
int len = strlen(message);
int cipherText[len];
for(int i=0;i<len;i++){
int m = getValue(message[i]);
int c = pow(m,e,n);
cipherText[i] = c;
}
printf("\nEncrypted Message: ");
for(int i=0;i<len;i++){
getLetter(cipherText[i]);
}
printf("\n");
int plainText[len];
for(int i=0;i<len;i++){
int c = cipherText[i];
int m = pow(c,d,n);
plainText[i] = m;
}
printf("\nDecrypted Message: ");
for(int i=0;i<len;i++){
getLetter(plainText[i]);
}
printf("\n");
}

Output:
EXPERIMENT 9

CONFIGURATION AND TESTING OF GIVEN NETWORK USING PACKET TRACER

Objective: To configure and test a given network using Packet Tracer.


EXPERIMENT 5

IMPLEMENT STOP AND WAIT PROTOCOL USING SOCKET PROGRAMMING CONCEPT IN C


PROGRAM.

Objective: Implement stop and wait protocol using socket programming in C.

Program:

Server side:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)


{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])


{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
fprintf(stderr,"You have'nt provided port Number, please enter port number\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("Server : error at port opening");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("Server : Error at binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("Server : Error while accepting");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("Server : ERROR reading from socket");
printf("MY message is : %s\n",buffer);
n = write(newsockfd,"I Have Recieved your message",30);
if (n < 0) error("Server : Error while writing to server");
close(newsockfd);
close(sockfd);
return 0;
}

Client side:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg)


{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])


{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s Enter your hostname & port number \n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("Client : Error While opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"Client : Error, host not found\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("Client : Error while connecting to server");
printf("Please enter your message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("Client : Error while writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error(" Client : Error while reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}

Output:
EXPERIMENT 4
ANALYSIS AND COMPARISON OF CSMA/CD AND CSMA/CA USING QualNet

Objective: To analyze CSMA/CD and CSMA/CA protocols with respect to following


parameters and compare them:
(i) Number of transmitting nodes vs Collision count
(ii) Number of transmitting nodes vs Mean delay
(iii) Number of retransmissions
(iv) Number of frames dropped

CSMA/CA:
CSMA/CD:

You might also like