0% found this document useful (0 votes)
16 views16 pages

MCom (IS) - DCN LAB Programs

The program implements the cyclic redundancy check (CRC) algorithm to detect errors in transmitted data. It defines variables to store the data, check value, and generator polynomial. It performs an XOR operation between the check value and generator polynomial bits at each step. The receiver function gets the received data and calls the CRC function to calculate the check value and compare it to the transmitted check value to detect errors.
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)
16 views16 pages

MCom (IS) - DCN LAB Programs

The program implements the cyclic redundancy check (CRC) algorithm to detect errors in transmitted data. It defines variables to store the data, check value, and generator polynomial. It performs an XOR operation between the check value and generator polynomial bits at each step. The receiver function gets the received data and calls the CRC function to calculate the check value and compare it to the transmitted check value to detect errors.
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/ 16

1.

Program to identify the category of the IP address for the given IP


address
#include <string.h>
#include <stdlib.h>
/* Function : extractIpAddress
Arguments :
1) sourceString - String pointer that contains ip address
2) ipAddress - Target variable short type array pointer that will store ip
address octets
*/
void extractIpAddress(unsigned char *sourceString,short *ipAddress)
{
unsigned short len=0;
unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];
len=strlen(sourceString);
for(i=0;i<len;i++)
{
if(sourceString[i]!='.')
{
buf[cnt++] =sourceString[i];
}
if(sourceString[i]=='.' || i==len-1)
{
buf[cnt]='\0';
cnt=0;
oct[cnt1++]=atoi(buf);
}
}
ipAddress[0]=oct[0];
ipAddress[1]=oct[1];
ipAddress[2]=oct[2];
ipAddress[3]=oct[3];
}
int main()
{
unsigned char ip[20]={0};
short ipAddress[4];
int i;
printf("\nEnter IP Address (xxx.xxx.xxx.xxx format): ");
scanf("%s",ip);
extractIpAddress(ip,&ipAddress[0]);
printf("\nIp Address:%03d.%03d.%03d.%03d\n",ipAddress[0],
ipAddress[1],ipAddress[2],ipAddress[3]);
if(ipAddress[0]>=0 &&ipAddress[0]<=127)
printf("Class A Ip Address.\n");
if(ipAddress[0]>127 &&ipAddress[0]<191)
printf("Class B Ip Address.\n");
if(ipAddress[0]>191 &&ipAddress[0]<224)
printf("Class C Ip Address.\n");
if(ipAddress[0]>224 &&ipAddress[0]<=239)
printf("Class D Ip Address.\n");
if(ipAddress[0]>239)
printf("Class E Ip Address.\n");
return 0;
}

Output:
Enter IP Address (xxx.xxx.xxx.xxx format): 125.12.14.15

Ip Address:125.012.014.015
Class A Ip Address.

2. Program to implement sliding window protocol


#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the
following manner (assuming no corruption offrames)\n\n");
printf("After sending %d frames at each stage sender waits for
acknowledgement sent by the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}

if(f%w!=0)

printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

Output:

Enter window size: 3

Enter number of frames to transmit: 6

Enter 6 frames: 1 100 30 800 50 1000

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

After sending 3 frames at each stage sender waits for acknowledgement sent
by the receiver
1 100 30
Acknowledgement of above frames sent is received by sender

800 50 1000
Acknowledgement of above frames sent is received by sender

3. Program for Socket pair system call usage in IPC


#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define DATA1 "Hello child process..."
#define DATA2 "Hi parent process..."
/*This program creates a pair of connected sockets then forks and
communicates over
them. This is very similar to communication with pipes, however,
socketpairs are
two-way communications objects. Therefore, I can send messages in both
directions.
*/
int main()
{
int sockets[2], child;
char buf[BUFSIZ];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0)
{
perror("opening stream socket pair");
exit(1);
}
/* Note: Execution order of parent/child is not guaranteed! Hence, order of
data
being sent/read is entirely undefined. Do not rely on any order, even if you
repeatedly observe it to follow
what you perceive as a pattern. */
if ((child = fork()) == -1)
{
perror("fork");
}
else if(child)
{
/* This is the parent. */
close(sockets[0]);
printf("Parent pid(%d) sending: %s\n", getpid(), DATA1);
if (write(sockets[1], DATA1, sizeof(DATA1)) < 0)
{
perror("writing stream message"); }
if (read(sockets[1], buf, BUFSIZ) < 0)
{ perror("reading stream message");
}
printf("Parent pid(%d) reading: %s\n", getpid(), buf);
close(sockets[1]);
}
else
/* This is the child. */
close(sockets[1]);
printf("Child pid(%d) sending: %s\n", getpid(), DATA2);
if (write(sockets[0], DATA2, sizeof(DATA2)) < 0)
{
perror("writing stream message");
}
if (read(sockets[0], buf, BUFSIZ) < 0)
{
perror("reading stream message");
}
printf("Child pid(%d)reading: %s\n", getpid(), buf);
close(sockets[0]);
}
return 0;
}
Output:
4. Program to implement Route tracing program
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char ip1[25],ip2[25],ip3[25],ip4[25],ip5[25];
char destn[25];
FILE *fp;
printf("\nTraceroute: ");
scanf("%s",destn);
fp=fopen("path.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s\t\t%s\t\t\t%s\t\t%s\t\t%s\n",ip1,ip2,ip3,ip4,ip5);
if((strcmp(destn,ip4)==0)||(strcmp(destn,ip5)==0))
{
printf("\nTracing route to %s over a maximum of 30 hops",ip4);
printf("\n1] %s \n2] %s \n3] %s [ %s ]\n",ip2,ip3,ip4,ip5);
printf("\nTrace complete\n");
exit(0);
}
}
return 0;
}
Output:

5. Program to implement any shortest path routing Algorithm


#include<stdio.h>
#include<string.h>
#include<math.h>
#define INF 99
#define N 6
int dijsktra(int cost[][N], int source, int target);
int main()
{
int cost[N][N],i,j,w,ch,co;
int source, target,x,y;
printf("\n\t\t\t The Shortest Path Algorithm ( DIJKSTRA'S ALGORITHM
)\n\n");
for(i=1;i< N;i++)
for(j=1;j< N;j++)
cost[i][j] = INF;
for(x=1;x< N;x++)
{
for(y=x+1;y< N;y++)
{
printf("Enter the weight of the path between nodes %d and %d:",x,y);
scanf("%d",&w);
cost [x][y] = cost[y][x] = w;
}
printf("\n");
}
printf("Enter the source: ");
scanf("%d", &source);
printf("\nEnter the target: ");
scanf("%d", &target);
co = dijsktra(cost,source,target);
printf("\nThe Shortest Path: %d\n",co);
}
int dijsktra(int cost[][N],int source,int target)
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
char path[N],temp;
int length, count=0;
for(i=1;i< N;i++)
{
dist[i] = INF;
prev[i] = -1;
}
start = source;
selected[start]=1;
dist[start] = 0;
while(selected[target] ==0)
{
min = INF;
m = 0;
for(i=1;i< N;i++)
{
d = dist[start] +cost[start][i];
if(d< dist[i]&&selected[i]==0){
dist[i] = d;
prev[i] = start;
}
if(min>dist[i] && selected[i]==0){
min = dist[i];
m = i;
}
}// end for
start = m;
selected[start] = 1;
}// end while
start = target;
j = 0;
while(start != -1)
{
path[j++] = start+65;
start = prev[start];
}
path[j]='\0';
length = strlen(path) - 1;
for(count = 0; count < length; count++, length--)
{
temp = path[count];
path[count] = path[length];
path[length] = temp;
}
printf("\npath: %s", path);
return dist[target];
}
Output:
The Shortest Path Algorithm ( DIJKSTRA'S ALGORITHM )

Enter the weight of the path between nodes 1 and 2:12


Enter the weight of the path between nodes 1 and 3:10
Enter the weight of the path between nodes 1 and 4:25
Enter the weight of the path between nodes 1 and 5:22

Enter the weight of the path between nodes 2 and 3:12


Enter the weight of the path between nodes 2 and 4:02
Enter the weight of the path between nodes 2 and 5:04

Enter the weight of the path between nodes 3 and 4:16


Enter the weight of the path between nodes 3 and 5:18

Enter the weight of the path between nodes 4 and 5:35


Enter the source: 1
Enter the target: 5
path: BCF
The Shortest Path: 16

6. Program to implement Distance Vector Routing 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;
//initialise the distance equal to cost matrix
rt[i].dist[j]=costmat[i][j];
rt[i].from[j]=j;
}
}
do{
count=0;
/* We choose arbitary vertex k and we calculate the direct distance from the
node
i to k using the cost matrixand add the distance from k to node j */
for(i=0;i<nodes;i++)
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");
}

Output:
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 2 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 2 Distance 3


node 2 via 2 Distance 1
node 3 via 3 Distance 0

7. Program to implement Cyclic Redundancy Check algorithm


// Include headers
#include<stdio.h>
#include<string.h>
// length of the generator polynomial
#define N strlen(gen_poly)
// data to be transmitted and received
char data[28];
// CRC value
char check_value[28];
// generator polynomial
char gen_poly[10];
// variables
int data_length,i,j;
// function that performs XOR operation
void XOR(){
// if both bits are the same, the output is 0
// if the bits are different the output is 1
for(j = 1;j < N; j++)
check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');

}
// Function to check for errors on the receiver side
void receiver(){
// get the received data
printf("Enter the received data: ");
scanf("%s", data);
printf("\n-----------------------------\n");
printf("Data received: %s", data);
// Cyclic Redundancy Check
crc();
// Check if the remainder is zero to find the error
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(){
// initializing check_value
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
// check if the first bit is 1 and calls XOR function
if(check_value[0]=='1')
XOR();
// Move the bits by 1 position for the next computation
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
// appending a bit from data
check_value[j]=data[i++];
}while(i<=data_length+N-1);
// loop until the data ends
}

int main()
{
// get the data to be transmitted
printf("\nEnter data to be transmitted: ");
scanf("%s",data);
printf("\n Enter the Generating polynomial: ");
// get the generator polynomial
scanf("%s",gen_poly);
// find the length of data
data_length=strlen(data);
// appending n-1 zeros to the data
for(i=data_length;i<data_length+N-1;i++)
data[i]='0';
printf("\n----------------------------------------");
// print the data with padded zeros
printf("\n Data padded with n-1 zeros : %s",data);
printf("\n----------------------------------------");
// Cyclic Redundancy Check
crc();
// print the computed check value
printf("\nCRC or Check value is : %s",check_value);
// Append data with check_value(CRC)
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length];
printf("\n----------------------------------------");
// printing the final data to be sent
printf("\n Final data to be sent : %s",data);
printf("\n----------------------------------------\n");
// Calling the receiver function to check errors
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
8. Program to encrypt and decrypt a string using Caeser cipher
#include <stdio.h>

// Function to encrypt the string using a Caesar cipher


void encrypt(char *message, int shift) {
while (*message != '\0') {
if (*message >= 'a' && *message <= 'z') {
*message = (*message - 'a' + shift) % 26 + 'a';
} else if (*message >= 'A' && *message <= 'Z') {
*message = (*message - 'A' + shift) % 26 + 'A';
}
message++;
}
}

// Function to decrypt the string using a Caesar cipher


void decrypt(char *message, int shift) {
while (*message != '\0') {
if (*message >= 'a' && *message <= 'z') {
*message = (*message - 'a' - shift + 26) % 26 + 'a';
} else if (*message >= 'A' && *message <= 'Z') {
*message = (*message - 'A' - shift + 26) % 26 + 'A';
}
message++;
}
}

int main() {
char message[100];
int shift;

printf("Enter a string to encrypt: ");


fgets(message, sizeof(message), stdin);

printf("Enter the shift value for encryption: ");


scanf("%d", &shift);

// Encrypt the entered message


encrypt(message, shift);
printf("Encrypted message: %s\n", message);

// Decrypt the encrypted message


decrypt(message, shift);
printf("Decrypted message: %s\n", message);

return 0;
}

Output:
Enter a string to encrypt: good morning
Enter the shift value for encryption: 3
Encrypted message: jrrg pruqlqj

Decrypted message: good morning

You might also like