CNP LAB Programs
CNP LAB Programs
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:
#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]);
}
Output:
EXPERIMENT 2
Program:
#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);
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
Program:
#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 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("\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));
Output:
(b) Prim’s Algorithm:
#include<stdio.h>
#define infinity 999
void main()
{
int a[10][10],n,i,j,m,source;
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.");
}
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
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
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>
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>
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
CSMA/CA:
CSMA/CD: