1) Framing Techniques (Character Count, Byte Stuffing, Bit Stuffing)
Framing Techniques (Character Count, Byte Stuffing, Bit Stuffing)
Bit Stuffing
(SENDER)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
struct frame1
{
char header1[9];
int cnt;
char data1[20];
char tailer1[9];
};
main()
{
char str[20];
int i=0,cnt=0,j=0,fd;
struct frame1 f;
strcpy(f.header1,"01111110");
strcpy(f.tailer1,"01111110");
f.cnt=0;
if((fd=open("nn",O_WRONLY))<0)
{
perror("Error on open ...");
exit(0);
}
while(1)
{
i=0;
j=0;
cnt=0;
printf("Enter any binary number (1 and 0 only) : ");
scanf(" %s",str);
f.cnt=f.cnt+1;
if(f.cnt>5)
f.cnt=0;
printf("\nActual String is : %s",str);
while(str[i]!='\0')
{
f.data1[j++]=str[i];
if(str[i]=='1')
cnt++;
if(cnt==5)
{
f.data1[j++]='0';
cnt=0;
}
if(str[i]=='0')
cnt=0;
i++;
}
f.data1[j]='\0';
printf("\nAfter Stuffing : %s\n",f.data1);
if((write(fd,&f,sizeof(struct frame1)))<=0)
{
perror("error on write ..");
exit(0);
}
if(f.cnt==0)
break;
}
}
/*
Enter any binary number (1 and 0 only) : 11111011111
Actual String is : 11111011111
After Stuffing : 1111100111110
Enter any binary number (1 and 0 only) : 11111
Actual String is : 11111
After Stuffing : 111110
*/
RECEIVER
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
struct frame1
{
char header1[9];
int cnt;
char data1[20];
char tailer1[9];
};
main()
{
char str[20];
int i=0,cnt=0,j=0,fd;
struct frame1 f;
if((fd=open("nn",O_RDWR))<0)
{
perror("Error on open...");
exit(0);
}
while(1)
{
if((read(fd,&f,sizeof(struct frame1)))<=0)
{
//perror("error on read ..");
exit(0);
}
i=0;
if(f.cnt==0)
break;
j=0;
cnt=0;
while(f.data1[i]!='\0')
{
str[j++]=f.data1[i];
if(f.data1[i]=='1')
cnt++;
if(cnt==5)
{
i+=2;
cnt=0;
}
else
i++;
if(f.data1[i]=='0')
cnt=0;
}
str[j]='\0';
printf("\nFrame No : %d Stuffing data : %s\n",f.cnt,f.data1);
printf("\n original : %s\n",str);
}
}
/*
Frame No : 1 Stuffing data : 1111100111110
original : 11111011111
Frame No : 2 Stuffing data : 111110
original : 11111
*/
Byte Stuffing
SENDER
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct byt
{
char sfg;
char data[50];
char efg;
};
int main()
{
int fd,len1=0,l1,i=0,j=0;
char msg[20];
struct byt b1;
fd=open("bt1",O_WRONLY);
printf("\nEnter the Starting Flag :-> ");
scanf("%c",&b1.sfg);
printf("\nEnter The Message :: ");
scanf("%s",b1.data);
printf("\nEnter the ESCAPE Flag :->");
scanf("%c",&b1.efg);
msg[j]=b1.sfg;
j++;
for(i=0;i<strlen(b1.data);i++)
{
if(b1.data[i]==b1.sfg)
{
msg[j]='$';
j++;
msg[j]=b1.data[i];
j++;
}
else if(b1.data[i]=='$')
{
msg[j]='$';
j++;
msg[j]=b1.data[i];
j++;
}
else
{
msg[j]=b1.data[i];
j++;
}
}
msg[j++]=b1.sfg;
for(i=0;i<j;i++)
{
printf("%c",msg[i]);
}
write(fd,msg,j);
return 0;
}
-:OUTPUT:-
[exam27@localhost exam27]$ cc bstr.c
[exam27@localhost exam27]$ ./a.out bstr
Enter the Starting Flag :-> #
Enter The Message :: bh#dd$dd
Enter the ESCAPE Flag :-> $
#bh$#dd$$dd#
RECIEVER
#include<stdio.h>
#include<fcntl.h>
int main()
{
char rmsg[80],msg[50];
int fd,len=20,i,j=0,l;
mknod("bt1",010666,0);
fd=open("bt1",O_RDONLY);
read(fd,msg,len);
for(i=1;i<len-1;i++)
{
if(msg[i]=='#')
{
rmsg[j]=msg[i];
i++;
j++;
}
else if(msg[i]=='$')
{
i++;
rmsg[j]=msg[i];
j++;
}
else
{
rmsg[j]=msg[i];
j++;
}
}
for(i=0;i<j;i++)
{
printf("%c",rmsg[i]);
}
return 0;
}
-:OUTPUT:-
exam27@localhost exam27]$ ./a.out bstr
bh#dd$dd
CHARECTER COUNT
(SENDER)
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd,len1=0,l1,i=0,j=0;
char msg[20],ms1[25],l[3],smsg[50];
fd=open("fcon1",O_WRONLY);
printf("\nEnter the length :: ");
scanf("%s",l);
printf("\nEnter the Sting");
scanf("%s",msg);
l1=strlen(l);
if(i<=l1)
{
smsg[i]=l[i];
i=i++;
}
smsg[i++]='#';
if(i>l1)
{
for(j=0;j<strlen(msg);j++)
{
smsg[i]=msg[j];
i++;
}
}
printf("\n %s %d",smsg,i);
write(fd,smsg,i);
return 0;
}
-:OUTPUT:-
[divya@linux nt]$ gcc -o data1 cc_s.c
[divya@linux nt]$ ./data1
Enter the length :: 5
Enter the Stingdivya
5#divya 7
Or
[exam27@localhost exam27]$ ./a.out scount
Enter the length :: 5
Enter the Stingbhumi
5#bhumi 7
(RECIVER)
#include<stdio.h>
#include<fcntl.h>
int main()
{
char msg[80],t[10];
int fd,len,i,j,l;
mknod("fcon1",010666,0);
fd=open("fcon1",O_RDONLY);
read(fd,msg,len);
for(i=0;msg[i]!='#';i++)
t[i]=msg[i];
l=atoi(t);
for(j=i+1;j<=l+1;j++)
{
printf("%c",msg[j]);
}
return 0;
}
-:OUTPUT:-
[divya@linux nt]$ gcc -o data2 cc_r.c
[divya@linux nt]$ ./data2
divya
or
[exam27@localhost exam27]$ cc rcount.c
[exam27@localhost exam27]$ ./a.out rcount
bhumi
[exam27@localhost exam27]$
2) Error Detection and Correction Techniques (Single Bit Parity,...
Shared By : Divya S Didwania(Lecturer-LCIT) Show/Hide Program
2) Error Detection and Correction Techniques (Single Bit Parity, Block Parity,
Checksum, CRC Checksum, Hamming Code)
Students may be asked to simulate different noise scenarios like single bit corruption,
multi-bit corruption, etc, and check whether the receiver, using the given error
detection/correction technique, is able to detect/correct the error.
CHECKSUM
Sender Side
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
char data[15][15];
int len;
}senddata;
void bincode(int val,char *str)
{
int i,j;
char temp[10];
int mask = 1;
for(i = 0 ; i < 7 ; i ++ , val >= 1)
{
if(( val & 1) == 1)
{
temp[i] = '1';
}
else
{
temp[i] = '0';
}
}
for(i = 6,j = 0;i>=0;i--,j++)
{
str[j] = temp[i];
}
str[j] = '\0';
return;
}
int main()
{
int fp,i,j,k,val;
char str[10],str1[10][10];
int sum[10],count = 0;
printf("\nEnter The String :: ");
scanf("%s",str);
senddata.len = strlen(str);
fp = open ("chksum",O_WRONLY);
for(i=0;i<strlen(str);i++)
{
val = (int)str[i];
bincode(val,senddata.data[i]);
}
printf("\n\n Sender Side Sum Is :: ");
for(i=6;i>=0;i--)
{
for(j=0;j<7;j++)
{
if(senddata.data[j][i] == '1')
{
count ++ ;
}
}
sum[i] = count % 2;
count = count / 2;
printf("%d",sum[i]);
}
printf("\nAfter Carry Of Sum Is(Final Sum) :: ");
for(i=6;i>=0;i--)
{
if(sum[i] == 1)
{
count ++ ;
}
sum[i] = count % 2;
count = count / 2;
printf("%d",sum[i]);
}
printf("\n Comlement Of Sum :: ");
for(i=0;i<7;i++)
{
if(sum[i] == 1)
{
senddata.data[senddata.len][i] = '0';
}
else
{
senddata.data[senddata.len][i] = '1';
}
printf("%c",senddata.data[senddata.len][i]);
}
senddata.data[senddata.len][i] = '\0';
printf("\n Sending Data Is :: ");
for(i=0;i<senddata.len;i++)
{
printf("\n\t\t %s",senddata.data[i]);
}
write(fp,&senddata,sizeof(struct frame);
return 0;
}
//Receiver Side
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
char data[15][15];
int len;
}readdata;
int main()
{
int fp,i,j,k,val;
char str[10],str1[10][10];
int sum[10],count = 0,flag = 0;
FILE *file;
file = fopen("chk.dat","rb");
if(!file)
{
printf("\n File Is Not Open");
exit(0);
}
fp = open ("chksum",O_RDONLY);
read(fp,&readdata,sizeof(struct frame);
printf("\nRecieved Data :: ");
for(i=0;i<=readdata.len;i++)
{
printf("%s\n" Readdata.data[i]);
}
printf("\n\n Recieverr Side Sum Is :: ");
for(i=6;i>=0;i--)
{
for(j=0;j<7;j++)
{
if(readdata.data[j][i] == '1')
{
count ++ ;
}
}
sum[i] = count % 2;
count = count / 2;
printf("%d",sum[i]);
}
printf("\nAfter Carry Of Sum Is :: ");
for(i=6;i>=0;i--)
{
if(sum[i] == 1)
{
count ++ ;
}
sum[i] = count % 2;
count = count / 2;
printf("%d",sum[i]);
}
printf("\n Comlement Of Sum :: ");
for(i=0;i<7;i++)
{
if(sum[i] == 1)
{
sum[i] = 0;
}
else
{
sum[i] = 1;
}
printf("%d",sum[i]);
if(sum[i] == 0)
{
continue;
}
else
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\n Correct Data ");
}
else
{
printf("\n Currupted Data ");
}
return 0;
}
CRC MECHANISM
//Sender Side
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl>
struct Frame
{
int lendivi,lendata;
char data[15];
char divi[10];
}send;
void crc()
{
int i,j,len;
char str[10];
int flag = 0;
len = send.lendata;
for(i = 0; i < send.lendivi - 1; i ++)
{
send.data[send.lendata++] = '0';
}
for(i=0;i<send.lendivi;i++)
{
str[i] = send.data[i];
}
str[i] = '\0';
for(i=send.lendivi;i<=send.lendata;i++)
{
printf("\n%s",str);
for(j = (send.lendivi - 1); j >= 0; j --)
{
if(flag == 0)
{
if((str[j] == '0' && send.divi[j] == '0' || (str[j] == '1' && send.divi[j] == '1'))
{
str[j] = '0';
}
else
{
str[j] = '1';
}
}
else
{
if(str[j] == '0')
{
str[j] = '0';
}
else
{
str[j] = '1';
}
}
}
flag = 0;
for(j=0;j<(send.lendivi - 1);j++)
{
str[j] = str[j+1];
}
if(str[0] == '0')
{
flag = 1;
}
str[j] = send.data[i];
str[j+1] = '\0';
}
printf("\n Value Of CRC Is :: %s\n",str);
for(i=0;i<send.lendivi;i++)
{
send.data[len++] = str[i];
}
printf("\nOriginal Data With CRC Is :: %s\n",send.data);
return;
}
int main()
{
int i,j,fp;
fp = open("crc",O_WRONLY);
printf("Enter The Data In Binary Form :: \n");
scanf("%s",send.data);
printf("\nEnter The Diviser In Binary Form :: \n");
scanf("%s",send.divi);
send.lendivi = strlen(send.divi);
send.lendata = strlen(send.data);
crc();
write (fp,&send,sizeof(struct Frame));
return 0;
}
//Reciever Side
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl>
struct Frame
{
int lendivi,lendata;
char data[15];
char divi[10];
}readdata;
void crc()
{
int i,j;
char str[10];
int flag = 0;
printf("\n\n Received Data Is :: \n",readdata.data);
for(i = 0; i < readdata.lendivi; i ++)
{
str[i] = readdata.data[i];
}
str[i] = '\0';
for(i=readdata.lendivi;i<=readdata.lendata;i++)
{
printf("\n%s",str);
for(j = (readdata.lendivi - 1); j >= 0; j --)
{
if(flag == 0)
{
if((str[j] == '0' && readdata.divi[j] == '0' || (str[j] == '1' && readdata.divi[j] == '1'))
{
str[j] = '0';
}
else
{
str[j] = '1';
}
}
else
{
if(str[j] == '0')
{
str[j] = '0';
}
else
{
str[j] = '1';
}
}
}
flag = 0;
for(j=0;j<(readdata.lendivi - 1);j++)
{
str[j] = str[j+1];
}
if(str[0] == '0')
{
flag = 1;
}
str[j] = readdata.data[i];
str[j+1] = '\0';
}
printf("\n Value Of CRC Is :: %s\n",str);
flag = 0;
for(i=0;i<(readdata.lendivi - 1);i++)
{
if(str[i] == '0')
{
continue;
}
else
{
flag = 1;
break;
}
}
return flag;
}
int main()
{
int i,j,fp,flag;
fp = open("crc",O_RDONLY);
read(fp,&readdata,sizeof(struct Frame));
flag = crc();
if(flag == 0)
{
printf("\nCorrect Data ...");
}
else
{
printf("\nCurrupted Data ...");
}
return 0;
}
}
Hamming Code For Bust Error Correction
//Sender Side
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
char data[15][15];
int len;
}senddata;
void bincode(int val,char *str)
{
int i;
int mask = 1;
for(i=0;i<7;i++,val >= 1)
{
if((val & 1) == 1)
{
str[i] = '1';
}
else
{
str[i] = '0';
}
}
str[i] = '\0';
return;
}
void hamcode(char *str)
{
int i,j,one,two,three,four;
char str1[15];
str1[15] = '\0';
one = two = three = four = 0;
str1[0] = '0';
str1[1] = '0';
str1[2] = str[6];
str1[3] = '0';
str1[4] = str[5];
str1[5] = str[4];
str1[6] = str[3];
str1[7] = '0';
str1[8] = str[2];
str1[9] = str[1];
str1[10] = str[0];
str1[11] = '\0';
for(i=0;i<12;i++)
{
if((i==0 || i==2 || i==4 || i==6 || i==8 || i==10) && (str1[i] == '1'))
one ++ ;
if((i==1 || i==2 || i==5 || i==6 || i==9 || i==10) && (str1[i] == '1'))
two ++;
if((i==3 || i==4 || i==5 || i==6) && (str1[i] == '1'))
three ++;
if((i==7 || i==8 || i==9 || i==10) && (str1[i] == '1'))
four ++;
}
if(one % 2 != 0)
str1[0]='1';
if(two % 2 != 0)
str1[1]='1';
if(three % 2 != 0)
str1[3]='1';
if(four % 2 != 0)
str1[7]='1';
strcpy(str,str1);
return ;
}
int main()
{
int fp,i,j,val;
char str[10];
printf("\n Enter The String :: ");
scanf("%s",str);
senddata.len = strlen(str);
fp = open("hamcode",O_WRONLY);
for(i=0;i<strlen(str);i++)
{
val = (int)str[i];
bincode(val,senddata.data[i]);
hamcode(senddata.data[i]);
}
write (fp,&senddata,sizeof(struct frame));
printf("\nSending Data Is :: \n");
for(i=0;i<strlen(str);i++)
{
printf("\n\t\t %s",senddata.data[i]);
}
return 0;
}
//Reciever Side
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
char data[15][15];
int len;
}readdata;
void bincode(int val,char *str)
{
int i;
int mask = 1;
for(i=0;i<7;i++,val >= 1)
{
if((val & 1) == 1)
{
str[i] = '1';
}
else
{
str[i] = '0';
}
}
str[i] = '\0';
return;
}
void hamchk(struct frame read)
{
int i,j,one,two,three,four,count;
int r1[10],r2[10],r3[10],r4[10];
char str1[10];
one = two = three = four = 0;
for(j=0;j<read.len;j++)
{
for(i=0;i<11;i++)
{
if((i==0 || i==2 || i==4 || i==6 || i==8 || i==10) && (read.data[j][i] == '1'))
one ++ ;
if((i==1 || i==2 || i==5 || i==6 || i==9 || i==10) && (read.data[j][i] == '1'))
two ++;
if((i==3 || i==4 || i==5 || i==6) && (read.data[j][i] == '1'))
three ++;
if((i==7 || i==8 || i==9 || i==10) && (read.data[j][i] == '1'))
four ++;
}
r1[j] = (one % 2 == 0) ? 0 : 1;
r2[j] = (two % 2 == 0) ? 0 : 1;
r3[j] = (three % 2 == 0) ? 0 : 1;
r4[j] = (four % 2 == 0) ? 0 : 1;
if(r1[j] == 0 && r2[j] == 0 && r3[j] == 0 && r4[j] == 0)
{
printf("\n Valid Data ...");
}
else
{
count = (r1[j]*1) + (r2[j]*2) + (r3[j]*4) + (r4[j]*8);
printf("There Was An Error On %d Bit ..",count-1);
if(read.data[i][count-1] == '0')
read.data[i][count-1] = '1';
else
read.data[i][count-1] = '0';
printf("\n\t Corrected Frame Is :: %s ",read.data[i]);
}
}
}
int main()
{
int fp,i;
fp = open("hamcode",O_RDONLY);
read(fp,&readdata,sizeof(struct frame));
printf("\n Recieved Data :: \n");
for(i=0;i<readdata.len;i++)
{
printf("\t\t%s",readdata.data[i]);
}
hamchk(readdata);
return 0;
}
// Vrc Mechanism
//Reciever Side
#include<fcntl.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct frame
{
char bdata[10][10];
int totblk,blksize;
}readdata;
void convertchar(char string[20])
{
int ii,sum = 0,val;
for(i = 0; i < 7; i ++)
{
if(string[i] == '1')
{
val = 1;
for(j = 0; j < i; j ++)
{
val * = 2;
}
sum + = val;
}
}
printf("%c",sum);
}
void chkparity(struct frame readdata)
{
int i,j,pcount;
printf("\nFinal Received Data Is :: \n");
for(i = 0; i < readdata.totblk; i ++)
{
pcount = 0;
for(j = 0; j < 7; j ++)
{
if(readdata.bdata[i][j] == '1')
{
pcount + = 1;
}
}
if(pcount % 2 == 0)
{
if(readdata.bdata[i][7] == '0')
{
convertchar(readdata.bdata[i]);
}
else
{
printf("\nWrong Data");
break;
}
}
else
{
if(readdata.bdata[i][7] == '1')
{
convertchar(readdata.bdata[i]);
}
else
{
printf("\nWrong Data");
break;
}
}
}
}
int main()
{
int fp,parity,i;
int idata[10];
char chrdata;
char ch,ch1[10];
void chkparity(struct frame);
fp = open("vrcdata"O_RDONLY);
read(fp,&readdata,sizeof(struct frame));
for(i = 0; i < readdata.totblk; i++)
{
printf("\nReceive Data Is :: %s\n",readdata.bdata[i]);
}
chkparity(readdata);
return(0);
}
// Vrc Mechanism
//Sender Side
#include<fcntl.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct frame
{
char bdata[10][10];
int totblk,blksize;
}senddata;
int chkparity(char recdata[10],int bsize)
{
int pcount,icnt;
pcount = 0;
for(icnt = 0 ;icnt < bsize;icnt ++)
{
if(recdata[icnt] == '1')
pcount ++ ;
}
return(pcount % 2);
}
void getbincode(int num,char binstr[10])
{
int icnt;
for(icnt = 0; icnt < 7; icnt ++;num > = 1)
{
if(num % 2 = = 0)
binstr[icnt] = '0';
else
binstr[icnt] = '1';
}
binstr[icnt] = '\0';
printf("\nBinary String Is :: %s",binstr);
}
int main()
{
int fp,pbit,icnt,i;
int idata[10];
FILE *vrcfile;
char chrdata;
char ch,ch1[10];
//Create Data File
vrcfile = fopen("vrcfile.dat","wb");
if(!vrcfile)
{
printf('\nCould Not Create Data FIle");
return(0);
}
printf("\nEnter The String :: ");
scanf("%s",ch1);
for(icnt = 0; icnt < strlen(ch1); icnt ++)
{
fprintf(vrcfile,"%c",ch1[icnt]);
}
fclose(vrcfile);
vrcfile = fopen("vrcfile.dat","rb");
if(!vrcfile)
{
printf("\nInput File Is Not Found ...");
return(0);
}
printf("\nData To Send Is :: \n");
for(icnt = 0; icnt < strlen(ch1); icnt ++)
{
fscan(vrcfile,"%c",&chrdata);
printf("\n %c",chrdata);
idata[icnt] = (int)chrdata;
printf("%d",idata[icnt]);
getbincode(idata[icnt],senddata,bdata[icnt]);
}
printf("\n");
senddata.totblk = strlen(ch1);
senddata.blksize = 8;
for(icnt = 0;icnt < senddata.totblk; icnt ++)
{
pbit = chkparity(senddata.bdata[icnt],7);
if(pbit == 0)
senddata.bdata[int][7] = '0';
else
senddata.bdata[icnt][7] = '1';
senddata.bdata[icnt][8] = '\0';
printf("\nSend Data Is :: %s\n",senddata.bdata[icnt]);
}
fp = open("vrcdata",O_WRONLY);
write(fp,&senddata,sizeof(struct frame));
close(fp);
fclose(vrcfile);
sleep(2);
return(0);
}
3) All Data Link Layer Protocols (Protocols 1, 2, 3, 4, 5, 6 as per Main...
Shared By : Divya S Didwania(Lecturer-LCIT) Show/Hide Program
3) All Data Link Layer Protocols (Protocols 1, 2, 3, 4, 5, 6 as per Main reference Book)
Protocols 1, 2 and 3 may be asked to be implemented completely. Programs based on
protocol 4, 5, and 6 may be asked in a way that a small feature like retransmission timer,
or NACK or sliding window to be implemented. Entire protocol programs must not be
asked for protocol 4, 5 and 6.
//Protocol Header File
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#define MAX_PKT 1024
typedef enum
{
false,true
}boolen;
typedef unsigned int seq_nr;
typedef struct
{
unsigned char data[MAX_PKT];
}packet;
typedef enum
{
data,ack,nak
}frame_kind;
typedef struct
{
frame_kind kind;
seq_nr seq;
seq_nr ack;
packet info;
}frame;
//void wait_for_event(event_type *event);
//{
//}
void from_network_layer(packet *p)
{
printf("\n Enter the string for Protocol1:=>");
scanf("%s",p->data);
}
void to_network_layer(packet *p)
{
printf("\n The message is :=> %s",p->data);
}
void from_physical_layer(frame *fr)
{
int fp;
mknod("P2",010666,0);
fp=open("P2",O_RDONLY);
read(fp,fr,sizeof(frame));
}
void to_physical_layer(frame *frp)
{
int fp;
fp=open("P2",O_WRONLY);
write(fp,frp,sizeof(frame));
}
**********************************************************
:W.A.P. FOR THE PROTOCOL 1.
**********************************************************
-------------------- SENDER SIDE -----------------
#include<stdio.h
#include<fcntl.h>
#include<string.h>
#include "pro1_header.h"
{
frame_arrival
}event_type;
void sender(void)
{
int i;
frame frp;
packet buff;
for(i=0;i<=5;i++)
{
from_network_layer(&buff);
frp.info=buff;
to_physical_layer(&frp);
}
}
int main()
{
sender();
return 0;
}
/*************************Otutput:*************************************************
Enter the string for Protocol1:=>Divya
Enter the string for Protocol1:=>Ankit
Enter the string for Protocol1:=>Ruchita
Enter the string for Protocol1:=>Seema
Enter the string for Protocol1:=>Kajal
Enter the string for Protocol1:=>Bhumika
**********************************************************
W.A.P. FOR THE PROTOCOL 1.
**********************************************************
-------------------- RECEIVER SIDE -----------------
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "pro1_header.h"
typedef enum
{
frame_arrival
}event_type;
void receiver(void)
{
int i,true=1;
frame fr1;
event_type evt;
for(i=0;i<=5;i++)
{
from_physical_layer(&fr1);
to_network_layer(&fr1.info);
}
}
int main()
{
receiver();
return 0;
}*********************** OUTPUT *************************
The message is :=> Divya
The message is :=> Ankit
The message is :=> Ruchita
The message is :=> Seema
The message is :=> Kajal
The message is :=> Bhumika
//Protocol : 2 >> Stop And Wait
// Sender Side
#include<stdio.h>
#include<fcntl.h>
#define PACKETSIZE 10
typedef struct
{
char data[PACKETSIZE];
}Packet;
typedef struct
{
packet info;
int Frameno;
}Frame;
typedef struct
{
int ack;
}ack;
ack a1;
Frame s;
Packet p1;
FILE *fp;
int fd,fd1;
int mordata = 1;
int frameno = 0;
void from_network_layer(Packet *packet)
{
int count = 0;
char ch;
while(count < PACKETSIZE)
{
ch = fgetc(fp);
if(ch == EOF)
{
mordata = 0;
break;
}
packet->data[count ++] = ch;
}
packet->data[count] = '\0';
}
void to_physical_layer(Frame *s)
{
write(fd,s,sizeof(Frame));
}
void wait_for_event(ack *myack)
{
read(fd1,myack,sizeof(ack));
}
main()
{
fp = fopen("data.txt","r");
fd = open("prot2pipe1",O_WRONLY);
fd1 = open("prot2pipe2",O_RDONLY);
while(mordata)
{
from_network_layer(&p1);
s.Frameno = frameno ++;
s.info = p1;
to_physical_layer(&s);
wait_for_event(&a1);
}
s.Frameno = -1;
to_physical_layer(&s);
printf("Data Transmitted Successfully\n");
}
//Protocol : 1 >> Unrestricted Simplex Protocol
// Reciever Side
#include<stdio.h>
#include<fcntl.h>
#define PACKETSIZE 10
typedef struct
{
char data[PACKETSIZE];
}Packet;
typedef struct
{
Packet info;
int Frameno;
}Frame;
typedef struct
{
int ack;
}ack;
Packet p1;
Frame r;
ack a1;
int fd,fd1;
void to_network_layer(Packet *packet)
{
printf("%s",packet->data);
}
void from_physical_layer(Frame *r);
{
read(fd,r,sizeof(Frame));
}
void to_physical_layer(ack *myack)
{
write(fd1,myack,sizeof(ack));
}
main()
{
fd = open("prot2pipe1",O_RDONLY);
fd1 = open("prot2pipe2",O_WRONLY);
while(1)
{
from_physical_layer(&r);
if(s.Frameno == -1)
break;
p1 = r.info;
to_network_layer(&p1);
to_physical_layer(&a1);
}
}
//Protocol : 3 >> Allow Unidirectional Data Flow Over An Unreliable Channel
// Receiver Side
#include<stdio.h>
#include<fcntl.h>
#include<sys/time.h>
#include<sys/ipc.h>
#define PACKETSIZE 10
#define MAXSEQ 1
#define inc(k) if(k < MAXSEQ) k = k + 1;else k = 0
typedef struct
{
char data[PACKETSIZE];
}Packet;
typedef struct
{
packet info;
int Frameno;
int Ack;
}Frame;
Frame s;
Packet buffer;
int fd,fd1;
int frame_expected = 0;
void from_network_layer(Packet *packet)
{
printf("\n%s",packet->data);
}
void to_physical_layer(Frame *s)
{
write(fd1,s,sizeof(Frame));
}
void from_physical_layer(Frame *r)
{
read(fd,r,sizeof(Frame));
}
main()
{
fd = open("prot3pipe1",O_RDONLY);
fd1 = open(prot3pipe2",O_WRONLY);
while(1)
{
from_network_layer(&r);
if(r.Frameno == -1)
break;
if(r.Frameno == frame_expected)
{
buffer = r.info;
to_network_layer(&buffer);
inc(frame_expected);
sleep(10);
}
s.Ack = 1 - 1frame_expected;
to_physical_layer(&s);
}
printf("\nData Recieved Successfully ...\n");
}
//Protocol : 3 >> Allow Unidirectional Data Flow Over An Unreliable Channel
// Sender Side
#include<stdio.h>
#include<fcntl.h>
#include<sys/time.h>
#include<sys/ipc.h>
#define PACKETSIZE 10
#define MAXSEQ 1
#define inc(k) if(k < MAXSEQ) k = k + 1;else k = 0
typedef struct
{
char data[PACKETSIZE];
}Packet;
typedef struct
{
packet info;
int Frameno;
int Ack;
}Frame;
Frame s;
Packet buffer;
FILE *fp;
int fd,fd1;
int mordata = 1;
int next_frame_to_send = 0;
int starttime;
struct timeval tv;
struct timezone tz;
void start_timer()
{
printf("\nTimer Started ");
gettimeofday(&tv,&tz);
starttime = tv.tv_sec;
}
void from_network_layer(Packet *packet)
{
int count = 0;
char ch;
while(count < PACKETSIZE)
{
ch = fgetc(fp);
if(ch == EOF)
{
mordata = 0;
break;
}
packet->data[count ++] = ch;
}
packet->data[count] = '\0';
}
void to_physical_layer(Frame *s)
{
write(fd,s,sizeof(Frame));
}
int wait_for_event(Frame *myfram)
{
int localtime,status;
do
{
sleep(2);
gettimetoday(&tv,&tz);
localtime = tv.tv_sec;
status = read(fd1,myframe,sizeof(Frame));
printf("\nIt %d , Ct = %d,Status = %d\n",localtime,starttime,status);
}while(((localtime - starttime) < 1) && (sstatus < 0));
printf("Out Of While\n")
if(status > 0)
{
return (1);
}
else
{
return (0);
}
}
void stop_timer()
{
printf("\n TImer Ended ");
starttime = 0;
}
main()
{
int event;
fp = fopen("data.txt","r");
fd = open("prot3pipe1",O_WRONLY);
fd1 = open(prot3pipe2",O_RDONLY | IPC_NOWAIT);
from_network_layer(&buffer);
while(mordata)
{
s.Frameno = next_frame_to_send;
s.info = buffer;
to_physical_layer(&s);
printf"Data Send :: %s\n",buffer.data);
start_timer();
event = wait_for_event(&s);
printf("\nEvent :: %d",event);
if(event == 1) //Frame Arrival
{
if(s.Ack == next_frame_to_send)
{
stop_timer();
from_network_layer(&buffer);
inc(next_frame_to_send);
}
}
}
// For Trasmitting Last Packet
s.Frameno = next_frame_to_send;
s.info = buffer;
to_physical_layer(&s);
s.Frameno = -1;
to_physical_layer(&s);
printf("Data Transmitted Successfully\n");
}
4) Cryptography (Without using Java Security/Cryptography Packages)...
Shared By : Divya S Didwania(Lecturer-LCIT) Show/Hide Program
4) Cryptography (Without using Java Security/Cryptography Packages)
Generalized Caesar Cipher, Mono-alphabetic Substitution Cipher, Transposition
(Columnar) cipher, P-Box, S-Box, Product Cipher, One Time Pad.
Above programs in item no. 4 should be implemented in Java. For these programs,
students need to implement the corresponding algorithms themselves in Java.
Communication may be done using sockets. For that Java socket programming is to be
used.
// substitution sender
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd,len1,icnt;
char strdata[10],msg[10]={0},asc[10];
fd=open("substitution",O_WRONLY);
printf("ENTER THE MESSAGE:=>");
scanf("%s",strdata);
len1=strlen(strdata);
for(icnt=0;icnt<len1;icnt++)
{
asc[icnt]=strdata[icnt];
}
for(icnt=0;icnt<len1;icnt++)
{
msg[icnt]=asc[icnt]+4;
}
printf("The Encrypted message is as follows:=> %s",msg);
write(fd,msg,20);
return 0;
}
[exam08@localhost nt1]$ ./a.out subtitution_s.c
ENTER THE MESSAGE:=>divya
The Encrypted message is as follows:=> hmz}e
// substitution receiver
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd,len1,icnt,asc[10];
char msg1[10],ans[10]={0};
mknod("substitution",010666,0);
fd=open("substitution",O_RDONLY);
read(fd,msg1,20);
len1=strlen(msg1);
for(icnt=0;icnt<len1;icnt++)
{
asc[icnt]=msg1[icnt];
}
for(icnt=0;icnt<len1;icnt++)
{
ans[icnt]=asc[icnt]-4;
}
printf("Encrytion data %s",msg1);
printf("\nThe original msg is %s",ans);
}
[exam08@localhost nt1]$ ./a.out subtitution_r.c
Encrytion data hmz}e
The original msg is divya
//Transposition (Columnar)
**********************************************************
MCA SEM-IV
ROLL NO :20
SUB :NT
PROGRAM :W.A.P. FOR THE TRANSPOSITION METHOd.
**********************************************************
-------------------- SENDER SIDE -----------------
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
char up(char);
char trans(char);
int main()
{
char ch,c,str[10],msg[50],enc[10];
int i,len,fd,j=0;
fd=open("f1",O_WRONLY);
printf("Enter the message....:=>");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
c=up(str[i]);
ch=trans(c);
msg[j]=ch;
j++;
}
printf("MESSAGE IS :=> %s",msg);
write(fd,msg,80);
}
char up(char c)
{
int i,asc;
asc=c;
if(asc<123||asc>97)
{
asc=asc-32;
}
c=asc;
return(c);
}
char trans(char ch)
{
char org[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char sub[26]="ZVJEDTIGHCKLOMNRQPSFBUWXYA";
int i;
for(i=0;i<25;i++)
{
if(ch==org[i])
break;
}
return(sub[i]);
}
************************ OUTPUT *********************
Enter the message....:=> ABC
MESSAGE IS :=> ZVJ
*****************************************************
//receiver
**********************************************************
MCA SEM-IV
ROLL NO :20
SUB :NT
PROGRAM :W.A.P. FOR THE TRANSPOSITION METHOd.
**********************************************************
-------------------- RECEIVER SIDE -----------------
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
char sub_de(char);
int main()
{
int fd,len,i,j=0;
char msg[80],str[80],ch;
mknod("f1",010666,0);
fd=open("f1",O_RDONLY);
read(fd,str,80);
len=strlen(str);
for(i=0;i<len;i++)
{
ch=sub_de(str[i]);
msg[j]=ch;
j++;
}
printf("The string is :=> %s",msg);
}
char sub_de(char ch)
{
char org[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char sub[26]= "ZVJEDTIGHCKLOMNRQPSFBUWXYA";
int i;
for(i=0;i<25;i++)
{
if(ch==sub[i])
break;
}
return(org[i]);
}
************************** OUTPUT ************************
The string is :=>ABC
**********************************************************
// Mono-alphabetic Substitution Cipher
//sender
// PROGRAM FOR SUBSTITUTION(MONO ALPHA SENDER).
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
char sym_p[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','\0'};
char sym_c[27]={'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F',
'G','H','J','K','L','Z','X','C','V','B','N','M','\0'};
int fun1(char);
main()
{
char str[100],str1[100];
int fd,i,j,p1;
fd=open("pipe",O_RDWR);
printf("\nEnter The Plaintext : ");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
p1 = fun1(str[i]);
str1[i]=sym_c[p1];
}
printf("\nSender Side");
printf("\nPlaintext -- %s",str);
printf("\nCiphertext -- %s\n\n",str1);
write(fd,&str1,100);
close("pipe");
}
int fun1(char ch)
{
int i;
for(i=0;i<27;i++)
if(ch == sym_p[i])
return i;
}
//receiver
// PROGRAM FOR SUBSTITUTION(MONO ALPHA RECEIVER).
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
char sym_p[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','\0'};
char sym_c[27]={'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F',
'G','H','J','K','L','Z','X','C','V','B','N','M','\0'};
int fun1(char);
main()
{
char str[100],str1[100];
int fd,i,j,p1;
fd=open("pipe",O_RDWR);
read(fd,&str,100);
for(i=0;i<strlen(str);i++)
{
p1 = fun1(str[i]);
str1[i]=sym_p[p1];
}
printf("\nReceiver Side");
printf("\nPlaintext -- %s",str);
printf("\nCiphertext -- %s\n\n",str1);
close("pipe");
}
int fun1(char ch)
{
int i;
for(i=0;i<27;i++)
if(ch == sym_c[i])
return i;
}
/* OUTPUT
cc rec.c
./a.out &
[2] 4085
cc sen.c
./a.out
Enter The Plaintext : attack
Sender Side
Plaintext -- attack
Ciphertext -- QZZQEA
Receiver Side
Plaintext -- QZZQEA
Ciphertext -- attack
*/
// p box
// Write a program to implement P-BOX
// P-BOX Sender
#include<stdio.h>
#include<fcntl.h>
void to_pbox(int a[],int n);
void disp(int a[],int n);
int arr[8];
int pd;
main()
{
int i;
char ch;
if((pd=open("7_pipe",O_RDWR))< 0)
{
printf("Can't open pipe\n");
exit(1);
}
pp:
printf("\n Enter The Data 8-Bit Binary Data:");
for(i=0;i<8;i++)
{
scanf("%d",&arr[i]);
}
to_pbox(arr,8);
printf("\n Want To Send More Frames:");
scanf("\n %c:",&ch);
if (ch == 'y' || ch == 'Y')
goto pp;
}
void to_pbox(int a[],int n)
{
int temp[8];
temp[0]=a[3];
temp[1]=a[6];
temp[2]=a[0];
temp[3]=a[7];
temp[4]=a[1];
temp[5]=a[2];
temp[6]=a[4];
temp[7]=a[5];
disp(temp,8);
write(pd,temp,sizeof(temp));
}
void disp(int a[],int n)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
}
/* OUTPUT
Enter The Data 8-Bit Binary Data:
1
0
0
0
0
0
0
1
0 0 1 1 0 0 0 0
Want To Send More Frames:n
*/
// Write a 'c' program to implement P-BOX
// P-BOX Receiver
#include<stdio.h>
#include<fcntl.h>
void from_pbox();
void disp(int a[],int n);
int arr[8];
int pd;
main()
{
int i;
char ch;
if((pd=open("7_pipe",O_RDWR))< 0)
{
printf("Can't open pipe\n");
exit(1);
}
from_pbox();
}
void from_pbox()
{
int temp[8],a[8];
read(pd,a,sizeof(temp));
temp[0]=a[3];
temp[1]=a[6];
temp[2]=a[0];
temp[3]=a[7];
temp[4]=a[1];
temp[5]=a[2];
temp[6]=a[4];
temp[7]=a[5];
disp(temp,8);
}
void disp(int a[],int n)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
}
// Output
1 0 0 0 0 1 0 0
// Write a program to implement S-BOX
#include <stdio.h>
#include <fcntl.h>
void to_pbox(int a[],int n);
void disp(int a[],int n);
void to_decoder(int a[],int n);
int to_dec(int a[],int n);
void to_bin(int dec);
void to_encoder(int a[],int n);
void to_3bit(int val);
int arr1[3],arr2[8],temp[8],final[3];
void initialize();
int pd;
main()
{
int i;
char ch;
if((pd=open("8_pipe",O_RDWR))< 0)
{
printf("Can't open pipe\n");
exit(1);
}
pp:
printf("\n Enter The Data 3-Bit Binary Data For 3-to-8 Decoder:");
for(i=0;i<3;i++)
scanf("%d",&arr1[i]);
printf("\n The Given Plain_text");
disp(arr1,3);
to_decoder(arr1,3);
to_encoder(temp,8);
printf("\n\n Want To Send More Frames:");
scanf("\n %c:",&ch);
if (ch == 'y' || ch == 'Y')
goto pp;
}
void to_pbox(int a[],int n)
{
temp[0]=a[3];
temp[1]=a[6];
temp[2]=a[0];
temp[3]=a[7];
temp[4]=a[1];
temp[5]=a[2];
temp[6]=a[4];
temp[7]=a[5];
printf("\n\n In P-BOX");
disp(temp,8);
write(pd,temp,sizeof(temp));
}
void disp(int a[],int n)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
}
void to_decoder(int a[],int n)
{
int dec;
dec=to_dec(a,n);
to_bin(dec);
to_pbox(arr2,8);
}
void to_encoder(int a[],int n)
{
int val,i;
for(i=0;i<8;i++)
{
if(a[i]==1)
{
val=i+1;
printf("\n The value of line :: %d",val);
}
}
to_3bit(val);
}
void to_3bit(int val)
{
int i=2;
while(val != 0)
{
final[i]=(val % 2);
val=(val/2);
i--;
}
printf("\n The Converted Cipher-text");
disp(final,3);
}
void initialize()
{
int i;
for(i=0;i<8;i++)
arr2[i]=0;
}
int to_dec(int a[],int n)
{
int i,no,ans=0;
int temp=1,j;
n=n-1;
for(i=n;i>=0;i--)
{
no=a[i];
temp=1;
if(a[i] != 0)
{
for(j=1;j<=i;j++)
temp*=2;
ans+=(no*temp);
;
}
else
continue;
}
printf("\n\n decimal value:%d \n",ans);
return(ans);
}
void to_bin(int dec)
{
int i;
initialize();
dec-=1;
arr2[dec]=1;
printf("\n");
for(i=0;i<8;i++)
printf("\t %d",arr2[i]);
}
/* Output
Enter The Data 3-Bit Binary Data For 3-to-8 Decoder:
1
0
1
The Given Plain_text
1 0 1
decimal value:5
0 0 0 0 1 0 0 0
In P-BOX
0 0 0 0 0 0 1 0
The value of line :: 7
The Converted Cipher-text
1 1 1
Want To Send More Frames:n
*/
//A Program For Seaser Cipher
//receiver Side
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
struct cipher
{
char str1[50];
int key;
}c1;
main()
{
char str[50]={'\0'},fans;
int i,j,fd,key,ch,ans;
fd = open("cipher",O_RDWR);
read(fd,&c1,sizeof(c1));
for(i=0;i<strlen(c1.str);i++)
{
ch = str[i];
if(ch >= 97 && ch <= 122)
{
if(ch >= 97 + c1.key && ch <= 122)
str1[i] = (c1.str[i] - c1.key);
else
str1[i] = (c1.str[i] - 97) + (122 - c1.key + 1);
}
else
{
if(ch >= 65 + c1.key && ch <= 90)
str1[i] = (c1.str[i] - c1.key);
else
str1[i] = (c1.str[i] - 65) + (90 - c1.key + 1);
}
}
str1[i] ='\0';
printf("\n Recieved Data :: %s\n",str1);
close("pipe");
}
//A Program For Seaser Cipher
//Sender Side
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
struct cipher
{
char str1[50];
int key;
}c1;
main()
{
char str[50],fans;
int i,j,fd,key,ch,ans;
fd = open("cipher",O_RDWR);
printf("\nEnter Paintext :: ");
scanf("%s",str);
printf("\nEnter Key :: ");
scanf("%d",&c1.key);
for(i=0;i<strlen(str);i++)
{
ch = str[i];
if(ch >= 97 && ch <= 122)
{
if(ch >= 97 && ch <= 122-c1.key)
c1.str1[i] = str[i] + c1.key;
else
c1.str1[i] = 97 + str[i] - (122 - c1.key) - 1;
}
else
{
if(ch >= 65 && ch <= 90 - c1.key)
c1.str1[i] = str[i] + c1.key;
else
c1.str1[i] = 65 + str[i] - (90 - c1.key) - 1;
}
}
c1.str1[i] = '\0';
write(fd,&c1,sizeof(c1));
printf("\n Sending Data :: %s\n",c1.str1);
close("pipe");
}
Error Detection and Correction Techniques ( Single Bit Parity )
Shared By : Divya S Didwania(Lecturer-LCIT) Show/Hide Program
Error Detection and Correction Techniques
Single Bit Parity
------------Sender-----------------------
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
char data[9];
}f;
int main()
{
int fd,i,x=0,y;
char bits[7];
fd=open("parity",O_WRONLY);
while(1)
{
int j=0;
printf("Enter Bits with 0 and 1 for binayr digit::--- ");
scanf("%s",bits);
y++;
if(y==2)
y=0;
for(i=0;i<strlen(bits);i++)
{
f.data[j++]=bits[i];
if(bits[i]=='1')
x++;
}
if(x%2!=0)
f.data[j++]='1';
elsef
f.data[j++]='0';
f.data[j]='\0';
printf("\n****************************************\n");
printf("Entered String Is::---- %s",bits);
printf("\nString With Parity Bit Is::----- %s\n",f.data);
printf("\n*****************************************\n");
write(fd,&f,sizeof(struct frame));
for(i=0;i<7;i++)
bits[i]= ' ';
if(y==0)
break;
}
return x;
}
"single_s.c" 45L, 1269C written
[divya@linux ~]$ ./d2
Enter Bits with 0 and 1 for binayr digit::--- 0000001
****************************************
Entered String Is::---- 0000001
String With Parity Bit Is::----- 00000011
*****************************************
Enter Bits with 0 and 1 for binayr digit::--- 1111110
****************************************
Entered String Is::---- 1111110
String With Parity Bit Is::----- 11111101
*****************************************
----------Receiver ------------------------
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
struct frame1
{
char data1[9];
}f;
int main()
{
int fd,j=1,i;
char bit[7];
mknod("parity",010666,0);
fd=open("parity",O_RDONLY);
while(1)
{
read(fd,&f,sizeof(struct frame1));
for(i=0;i<7;i++)
{
bit[i]=f.data1[i];
}
printf("\n*******************************\n");
printf("\nRecevied Data Is %s %s",f.data1,bit);
printf("\n*********************************\n");
}
return 0;
}
~
[divya@linux ~]$ gcc -o d1 exam1.c
[divya@linux ~]$ ./d1
*******************************
Recevied Data Is 00000011 0000001
*********************************
Recevied Data Is 11111101 1111110
*********************************
Shared By : Anand Pandya (Lecturer-SSIT)
Shared By :diz ( SVICS - KADI )