Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Monday 18 June 2012

Protocol - 3 (Simplex Protocol For Noisy Channel)


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
typedef struct
{
int seqno,ackno;
char data[50];
}frame;
int getAck()
{
int pid,ack=0;
pid=open("pipe2",O_RDONLY);
if(pid>0)
{
read(pid,&ack,sizeof(ack));
unlink("pipe2");
}
close(pid);
return ack;
}
int start_timer(int ack)
{
int i;
printf("\ntimer starts");
for(i=0;i<10;i++)
{
printf("\n%d",i+1);
sleep(1);
if(ack==getAck())
{
return 0;
}
}
return 1;
}
void from_network_layer(char buffer[])
{
printf("Enter data : ");
scanf("%s",buffer);
}
void to_physical_layer(frame *f)
{
int pid;
system(">pipe1");
pid=open("pipe1",O_WRONLY);
if(pid>0)
{
write(pid,f,sizeof(frame));
}
close(pid);
}
void main()
{
int n,i;
char buffer[50];
frame f;
printf("Enter how many data want to send : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
from_network_layer(buffer);
strcpy(f.data,buffer);
f.seqno=i;
do
{
printf("\n\t...data send...\n");
to_physical_layer(&f);
}while(start_timer(i));
}
}


[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
typedef struct
{
int seqno,ackno;
char data[50];
}frame;
int getAck()
{
int pid,ack=0;
pid=open("pipe2",O_RDONLY);
if(pid>0)
{
read(pid,&ack,sizeof(ack));
unlink("pipe2");
}
close(pid);
return ack;
}
int start_timer(int ack)
{
int i;
printf("\ntimer starts");
for(i=0;i<10;i++)
{
printf("\n%d",i+1);
sleep(1);
if(ack==getAck())
{
return 0;
}
}
return 1;
}
void to_network_layer(char buffer[])
{
printf("\nYour data : %s",buffer);
}
void from_physical_layer(frame *f)
{
int pid;
pid=open("pipe1",O_RDONLY);
printf("\nfile operation : %d\n",pid);
if(pid>0)
{
read(pid,f,sizeof(frame));
unlink("pipe1");
}
close(pid);
}
void send_ack(int seqno)
{
int pid;
system(">pipe2");
pid=open("pipe2",O_WRONLY);
if(pid>0)
{
write(pid,&seqno,sizeof(seqno));
}
close(pid);
}
void main()
{
int n,i;
char buffer[50];
frame f;
from_physical_layer(&f);
strcpy(buffer,f.data);
to_network_layer(buffer);
send_ack(f.seqno);
}

Simple Socket Programming.

[ SENDER SIDE ]


import java.io.*;
import java.net.*;
public class sender
{
public static void main(String args[]) throws Exception
{
String msg="HELLO WORLD";
Socket soc=new Socket("localhost",3030);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
dos.writeUTF(msg);
}
}


[ RECEIVER SIDE ]


import java.io.*;
import java.net.*;
public class receiver
{
public static void main(String args[]) throws Exception
{
String msg;
ServerSocket ss=new ServerSocket(3030);
Socket soc=ss.accept();
DataInputStream dis=new DataInputStream(soc.getInputStream());
msg=dis.readUTF();
System.out.println("Data received is : "+msg);
}
}

P-Box.

[ SENDER SIDE ]

import java.io.*;
import java.net.*;
public class sender
{
public static void main(String args[])throws Exception
{
int i,pos;
String input="",output="",key="241503";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Socket soc=new Socket("localhost",3030);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
System.out.print("Enter Data To Send [length = 6] : ");
input=br.readLine();
for(i=0;i<input.length();i++)
{
pos=Integer.parseInt(key.charAt(i)+"");
output=output+input.charAt(pos);
}
System.out.println("Encrypted Data is "+output);
dos.writeUTF(output);
}
}

[ RECEIVER SIDE ]


import java.io.*;
import java.net.*;
public class receiver
{
public static void main(String args[])throws Exception
{
int i,pos;
String input="",output="",key="241503";
ServerSocket ss=new ServerSocket(3030);
Socket soc=ss.accept();
DataInputStream dis=new DataInputStream(soc.getInputStream());
input=dis.readUTF();
for(i=0;i<input.length();i++)
{
pos=key.indexOf(""+i);
output=output+input.charAt(pos);
}
System.out.println("Encrypted Data is "+output);
}
}

Mono-alphabetic substitution cipher.

[ SENDER SIDE ]


import java.io.*;
import java.net.*;
public class sender
{
public static void main(String args[])throws Exception
{
char ch,ch1;
int i,pos;
String input="",output="";
String plaintext="abcdefghijklmnopqrstuvwxyz";
String ciphertext="QWERTYUIOPASDFGHJKLZXCVBNM";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Socket soc=new Socket("localhost",3030);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());

System.out.print("Enter Data To Send : ");
input=br.readLine(); //attack
for(i=0;i<input.length();i++)
{
ch=input.charAt(i);
pos=plaintext.indexOf(ch);
ch1=ciphertext.charAt(pos);
output=output+ch1;
}
System.out.println("ENCRYPTED DATA IS "+output);
dos.writeUTF(output);
}
}


[ RECEIVER SIDE ]


import java.io.*;
import java.net.*;
public class receiver
{
public static void main(String args[])throws Exception
{
char ch,ch1;
int i,pos;
String input="",output="";
String plaintext="abcdefghijklmnopqrstuvwxyz";
String ciphertext="QWERTYUIOPASDFGHJKLZXCVBNM";
ServerSocket ss=new ServerSocket(3030);
Socket soc=ss.accept();
DataInputStream dis=new DataInputStream(soc.getInputStream());
input=dis.readUTF();
for(i=0;i<input.length();i++)
{
ch=input.charAt(i);
pos=ciphertext.indexOf(ch);
ch1=plaintext.charAt(pos);
output=output+ch1;
}
System.out.println("Decrypted Message : "+output);
}
}

Generalized Caesar Cipher.

[ SENDER SIDE ]


import java.io.*;
import java.net.*;
public class sender
{
public static void main(String args[]) throws Exception
{
int i=0;
int ch;
String input="",output="";
Socket soc=new Socket("localhost",3030);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Data To Send : ");
input=br.readLine();
for(i=0;i<input.length();i++)
{
ch=(int)input.charAt(i);
ch=ch+3;
if(ch>122 || ch>90 && ch<97)
ch=ch-26;
output=output+(char)ch;
}
System.out.println(output);
dos.writeUTF(output);
}
}


[ RECEIVER SIDE ]


import java.io.*;
import java.net.*;
public class receiver
{
public static void main(String args[]) throws Exception
{
int ch,i;
String input="",output="";
ServerSocket ss=new ServerSocket(3030);
Socket soc=ss.accept();
DataInputStream dis=new DataInputStream(soc.getInputStream());
input=dis.readUTF();
for(i=0;i<input.length();i++)
{
ch=(int)input.charAt(i);
ch=ch-3;
if(ch<65 || ch>90 && ch<97)
ch=ch+26;
output=output+(char)ch;
}
System.out.println("DECRYPTED MESSAGE : "+output);
}
}

Tuesday 12 June 2012

FON ( FUNDAMENTALS OF NETWORKING )


**How to use Command line argument in gcc compiler

1. Framing Techniques

  1. Character Count. [SHOW]   [ READ ]
  2. Byte Stuffing. [SHOW]   [ READ ]
  3. Bit Stuffing. [SHOW]   [ READ ]
2. Error Detection and Correction Techniques
  1. Single Bit Parity. [ SHOW ]
  2. Block Parity. [ SHOW ]
  3. Checksum. [ SHOW ]
  4. CRC Checksum. [ SHOW ]
  5. LRC(Longitudinal Redundancy Check) [ SHOW ]
  6. Hamming Code. 
3. All Data Link Layer Protocols
  1. Protocol - 1 (Unrestricted Simplex Protocol) [ SHOW ]
  2. Protocol - 2 (Simplex Stop And Wait Protocol) [ SHOW ]
  3. Protocol - 3 (Simplex Protocol For Noisy Channel) [ SHOW ]
  4. Protocol - 4 (A 1-bit Sliding Window Protocol)
  5. Protocol - 5 (Go-Back N Protocol)
  6. Protocol - 6 (Selective Repeat Protocol)
4. Cryptography (Without Using java security/cryptography package).
  1. Simple Socket Programming. [ SHOW ]
  2. Generalized Caesar Cipher. [ SHOW ]
  3. Mono-alphabetic substitution cipher. [ SHOW ]
  4. Transposition(Columnar) cipher.
  5. P-Box.  [ SHOW ]
  6. S-Box.
  7. Product Cipher.
  8. One Time Pad.
5. Cryptography (suing java security/cryptography packages)
  1. DES (with ECB,CBM,CFM modes).
  2. 3DES (with ECB,CBC,CFM modes).
  3. AES(with ECB,CBC,CFM modes).
  4. Stream Cipher implementation of DES,3DES and AES.

CRC CHECKSUM


[ SENDER SIDE ]
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int power(int no,int raise)
{
int ans=1,i;
for(i=0;i<raise;i++)
{
ans*=no;
}
return ans;
}
int toNum(char str[])
{
int i,sum=0,k=0;
for(i=strlen(str)-1;i>=0;i--)
{
if(str[i]=='1')
sum+=power(2,k);
k++;
}
return sum;
}
void xor(char no1[],char no2[],char temp[])
{
int i;
for(i=0;i<strlen(no1);i++)
{
if(no1[i] == no2[i])
temp[i]='0';
else
temp[i]='1';
}
}
void removeextra(char temp[],int len)
{
int pos,j=0,i;
char temp2[50];
strcpy(temp2,temp);
pos=strlen(temp2)-len;
for(i=pos;temp2[i]!='\0';i++)
temp[j++]=temp2[i];
temp[j]='\0';
}
void divide(char d1[],char str[],char temp[])
{
int i;
char zero[50];
for(i=0;i<strlen(d1);i++)
zero[i]='0';

if(toNum(str)>=toNum(d1))
xor(str,d1,temp);
else
xor(str,zero,temp);

removeextra(temp,strlen(d1)-1); //removing
}
void performdivision(char d1[],char d2[],char rem[])
{
char temp[50],str[50];
int lend1,lend2,i;
lend1=strlen(d1);
lend2=strlen(d2);

for(i=0;i<lend1;i++)
str[i]=d2[i];
str[i]='\0';

for(i=lend1;i<lend2;i++)
{
divide(d1,str,temp);
strcpy(str,temp);
str[strlen(str)]=d2[i];
str[strlen(str)+1]='\0';
temp[0]='\0';
}
strcpy(rem,str);
}
void add(char str1[],char str2[],char ans[])
{
int i,carry=0,sum;
for(i=strlen(str1)-1;i>=0;i--)
{
sum=carry+ (str1[i]-48) + (str2[i]-48);
if(sum>=2)
carry=1;
else
carry=0;
ans[i]=sum%2 + 48;
}
ans[strlen(str1)]='\0';
}
int tobinary(int num,int pos)
{
int code=1;
code=code << (pos-1);
if( code & num )
return 1;
else
return 0;
}
void main()
{
int lend1,lend2,i,k=0,num;
int pid;
char d1[48],d2[48],rem[48],ans[48];
system("clear");
system(">pipe");
pid=open("pipe",O_WRONLY);
printf("Enter divisor (both side must be 1): ");
scanf("%s",d1);
lend1=strlen(d1);
start:
printf("Enter divident (length must be >= %d) : ",lend1);
scanf("%s",d2);
if(strlen(d2)<lend1)
{
printf("\n...check size...\n");
goto start;
}

lend2=strlen(d2);
for(i=lend2; i<lend1+lend2-1; i++)
d2[i]='0';
d2[i]='\0';

printf("\nDIVISOR : %s",d1);
printf("\nDIVIDENT : %s",d2);
performdivision(d1,d2,rem);
printf("\nremainder : %s",rem);
num=toNum(rem);
for(i=strlen(d2);i>0;i--)
{
rem[k++]=tobinary(num,i) + 48;
}
rem[k]='\0';
add(d2,rem,ans);
printf("\nDATA TO SEND : %s ",ans);
write(pid,&d1,sizeof(d1));
write(pid,&ans,sizeof(ans));
}

[ RECEIVER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int power(int no,int raise)
{
int ans=1,i;
for(i=0;i<raise;i++)
{
ans*=no;
}
return ans;
}
int toNum(char str[])
{
int i,sum=0,k=0;
for(i=strlen(str)-1;i>=0;i--)
{
if(str[i]=='1')
sum+=power(2,k);
k++;
}
return sum;
}
void xor(char no1[],char no2[],char temp[])
{
int i;
for(i=0;i<strlen(no1);i++)
{
if(no1[i] == no2[i])
temp[i]='0';
else
temp[i]='1';
}
}
void removeextra(char temp[],int len)
{
int pos,j=0,i;
char temp2[50];
strcpy(temp2,temp);
pos=strlen(temp2)-len;
for(i=pos;temp2[i]!='\0';i++)
temp[j++]=temp2[i];
temp[j]='\0';
}
void divide(char d1[],char str[],char temp[])
{
int i;
char zero[50];
for(i=0;i<strlen(d1);i++)
zero[i]='0';

if(toNum(str)>=toNum(d1))
xor(str,d1,temp);
else
xor(str,zero,temp);

removeextra(temp,strlen(d1)-1); //removing
}
void performdivision(char d1[],char d2[],char rem[])
{
char temp[50],str[50];
int lend1,lend2,i;
lend1=strlen(d1);
lend2=strlen(d2);

for(i=0;i<lend1;i++)
str[i]=d2[i];
str[i]='\0';

for(i=lend1;i<lend2;i++)
{
divide(d1,str,temp);
strcpy(str,temp);
str[strlen(str)]=d2[i];
str[strlen(str)+1]='\0';
temp[0]='\0';
}
strcpy(rem,str);
}
void main()
{
int pid,lend1,lend2,i,k=0,num,flag=1;
char d1[50],d2[50],rem[50],ans[50];
pid=open("pipe",O_RDONLY);
read(pid,&d1,sizeof(d1));
read(pid,&d2,sizeof(d2));
printf("\nDIVISOR : %s",d1);
printf("\nDIVIDENT : %s",d2);
performdivision(d1,d2,rem);
printf("\nremainder : %s",rem);
for(i=0;i<rem[i]!='\0';i++)
{
if(rem[i]=='1')
{
flag=0;
break;
}
}
if(flag==1)
printf("\nDATA ARRIVED CORRENCTLY");
else
printf("DATA ARRIVED INCORRECTELY");
printf("\nDATA TO SEND : %s ",ans);
}

Simplex Stop-AND-Wait Protocol - Protocol 2


[ HEADER.H ]
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
struct frame
{
int ack,sno ;
char data[50] ;
};

void from_network_layer(char buffer[])
{
printf("\n ENTER DATA : ");
scanf("%s",buffer);
}
void to_physical_layer(struct frame *f)
{
int pid1;
system(">pipe1") ;
pid1=open("pipe1",O_WRONLY);
if(pid1>0)
{
write(pid1,f,sizeof(struct frame));
}
close(pid1);
}
int getack()
{
int pid2,ack=0 ;
pid2=open("pipe2",O_RDONLY);
if(pid2>0)
{
read(pid2,&ack,sizeof(ack));
}
return ack ;
}

void to_network_layer(char buffer[])
{
printf("\n %s ",buffer);
}
void from_physical_layer(struct frame *f)
{
int pid1;
pid1=open("pipe1",O_RDONLY);
if(pid1>0)
{
read(pid1,f,sizeof(struct frame));
unlink("pipe1");
}
close(pid1);
}

void sendack(int ack)
{
system(">pipe2");
int pid ;
pid=open("pipe2",O_WRONLY);
write(pid,&ack,sizeof(ack));
close(pid);
}


[ SENDER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "header.h"
void main()


{
int i,datalen ;
char buffer[50] ;
struct frame f ;
printf("\n ENTER HOW MANY DATA : ");
scanf ("%d",&datalen);
for(i=1;i<=datalen;i++)
{
from_network_layer(buffer) ;
strcpy(f.data,buffer);
f.sno=i ;
to_physical_layer(&f);
while(getack()!=i)
{
sleep(2);
}
}
}

[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "header.h"


void main()
{
int i=0 ;
char buffer[50] ;
struct frame f ;
from_physical_layer(&f) ;
strcpy(buffer,f.data) ;
to_network_layer(buffer);
sendack(f.sno);
}


Unrestricted Simplex Protocol- Protocol 1

[ HEADER.H ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
typedef struct 
{
int seqno;
int ackno;
char data[50];
}frame;
void from_network_layer(char buffer[])
{
printf("Enter Data : ");
scanf("%s",buffer);
}
void to_physical_layer(int pid1,frame *f)
{
write(pid1,f,sizeof(frame));
}
void from_physical_layer(int pid1,frame *f)
{
read(pid1,f,sizeof(frame));
}
void to_network_layer(char buffer[])
{
printf("\n%s",buffer);
}



[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "header.h"
void main()
{
int pid1,i,no;
char buffer[50];
frame f;
system(">pipe1");
pid1=open("pipe1",O_WRONLY);
printf("Enter NUMBER OF DATA : ");
scanf("%d",&no);
write(pid1,&no,sizeof(no));
for(i=0;i<no;i++)
{
from_network_layer(buffer);
strcpy(f.data,buffer);
to_physical_layer(pid1,&f);
}
close(pid1);
}

[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "header.h"
void main()
{
int pid1,no,i;
char buffer[50];
frame f;
pid1=open("pipe1",O_RDONLY);
read(pid1,&no,sizeof(no));
printf("DATA RECEIVED : %d",no);
printf("\nDATA");
for(i=0;i<no;i++)
{
from_physical_layer(pid1,&f);
strcpy(buffer,f.data);
to_network_layer(buffer);
}
close(pid1);
unlink("pipe1");
}


SINGLE BIT PARITY


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getbinary(int num,int pos)
{
int code=1;
code=code<<(pos-1);
if(code & num)
return 1; // 1 and 1   is 1
else
return 0;
}
int getparity(char ans[])
{
int i=0,counter=0;
for(i=0;i<14;i++)
{
if(ans[i]=='1') //no of 1 is even then 0 (even parity)
counter++;
}
if(counter % 2 == 0 )
return 0;
else
return 1;
}
void generateError(char ans[])
{
int exitflag=1,pos;
do
{
printf("Enter Position : ");
scanf("%d",&pos);
if(pos>=0 && pos<14)
{
if(ans[pos]=='1')
ans[pos]='0';
else
ans[pos]='1';
}
else
{
printf(" \t>>>Position is out of range<<<\n");
}
printf("Want to Generate More Error (1 / 0) : ");
scanf("%d",&exitflag);
}while(exitflag==1);
}
void main()
{
int num,i,k=0,flag,pid;
char ans[16];
pid=open("pipe",O_WRONLY);
printf("Enter number : ");
scanf("%d",&num);
for(i=14;i>0;i--)
{
ans[k++]=getbinary(num,i) + 48;
}
ans[k++]=getparity(ans) + 48;
ans[k++]='\0';
printf("\n======================================================");
printf("\nData to Be Send With Parity : %s",ans);
printf("\n======================================================\n");
printf("\nWant to Generate Error (1 / 0) : ");
scanf("%d",&flag);
if(flag==1)
{
generateError(ans);
printf("\n======================================================");
printf("\nData to Be Send With Parity and with Error : %s",ans);
printf("\n======================================================\n");
}
write(pid,&ans,sizeof(ans));
close(pid);
}

[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getparity(char ans[])
{
int i=0,counter=0;
for(i=0;i<14;i++)
{
if(ans[i]=='1') //no of 1 is even then 0 (even parity)
counter++;
}
if(counter % 2 == 0 )
return 0;
else
return 1;
}
void main()
{
int pid,i=0;
char ans[16];
pid=open("pipe",O_RDONLY);
read(pid,&ans,sizeof(ans));
if(ans[14]== (getparity(ans)+48) )
printf("...........NO ERROR.............");
else
printf("...........ERROR DETECTED..........");
}


CHECKSUM


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getbinary(int num,int pos)
{
int code=1;
code=code<<(pos-1);
if(code & num)
return 1; // 1 and 1   is 1
else
return 0;
}
void generateError(char ans[])
{
int exitflag=1,pos;
do
{
printf("Enter Position : ");
scanf("%d",&pos);
if(pos>=0 && pos<24)
{
if(ans[pos]=='1')
ans[pos]='0';
else
ans[pos]='1';
}
else
{
printf(" \t>>>Position is out of range<<<\n");
}
printf("Want to Generate More Error (1 / 0) : ");
scanf("%d",&exitflag);
}while(exitflag==1);
}
void divide(char block1[], char block2[], char ans[])
{
int i=0,j=0,k=0;
for(i=0;i<14;i++)
{
if(i<7)
block1[j++]=ans[i];
else
block2[k++]=ans[i];
}
}
int getbit(char block[],int pos)
{
return block[pos]-48;
}
void add(char block1[],char block2[],char code[])
{
int i=0,carry=0,sum;
char carryblock[8]="0000001";
for(i=6;i>=0;i--)
{
sum=carry + getbit(block1,i) + getbit(block2,i);
code[i]=sum%2 + 48;
if(sum>=2)
carry=1;
else
carry=0;
}
if(carry==1)
add(code,carryblock,code);
code[7]='\0';
}

void inverse(char code[])
{
int i=0;
for(i=0;i<7;i++)
{
if(code[i]=='0')
code[i]='1';
else
code[i]='0';
}
}
void main()
{
int num,i,flag,pid,j=0,k=0;
char block1[50],ans[50],block2[50],code[50];
pid=open("pipe",O_WRONLY);
printf("Enter number : ");
scanf("%d",&num);
for(i=7;i>0;i--)
{
block1[j++]=getbinary(num,i+7) + 48;
block2[k++]=getbinary(num,i) + 48;
}
block1[j++]='\0';
block2[k++]='\0';

add(block1,block2,code);
code[7]='\0';
printf("\nblock 1  : %s",block1);
printf("\nblock 2  : %s",block2);
printf("\n===================");
printf("\ncode(add): %s",code);
printf("\n===================\n\n");


inverse(code);

strcat(block1,block2); //concate block1 and block2 in block1
strcat(block1,code);


printf("\n======================================================");
printf("\nData to Be Send With Code : %s",block1);
printf("\n======================================================\n");


printf("\nWant to Generate Error (1 / 0) : ");
scanf("%d",&flag);
if(flag==1)
{
generateError(block1);
printf("\n======================================================");
printf("\nData to Be Send With Parity and with Error : %s",block1);
printf("\n======================================================\n");
}
write(pid,&block1,sizeof(block1));
close(pid);
}

[ RECEIVER SIDE ]


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

int getbit(char block[],int pos)
{
return block[pos]-48;
}
void add(char block1[],char block2[],char code[])
{
int i=0,carry=0,sum;
char carryblock[8]="0000001";
for(i=6;i>=0;i--)
{
sum=carry + getbit(block1,i) + getbit(block2,i);
code[i]=sum%2 + 48;
if(sum>=2)
carry=1;
else
carry=0;
}
if(carry==1)
add(code,carryblock,code);
code[7]='\0';
}
void inverse(char code[])
{
int i=0;
for(i=0;i<7;i++)
{
if(code[i]=='0')
code[i]='1';
else
code[i]='0';
}
}
void main()
{
int pid,i=0,flag=1;
char ans[50],block1[50],block2[50],code[50],temp[50],result[50];
pid=open("pipe",O_RDONLY);
read(pid,&ans,sizeof(ans));
while(ans[i]!='\0')
{
block1[i]=ans[i];
block2[i]=ans[i+7];
code[i]=ans[i+14];
if(i>=7)
break;
i++;
}
block1[i]='\0';
block2[i]='\0';
code[i]='\0';


printf("\nblock 1  : %s",block1);
printf("\nblock 2  : %s",block2);
printf("\ncode     : %s",code);
printf("\n===================\n");

add(block1,block2,temp);
add(temp,code,result);
inverse(result);
for(i=0;i<7;i++)
{
if(result[i]!='0')
flag=0;
}
if(flag==1)
printf("\n..............DATA IS CORRECT..............\n");
else
printf("\n..............DATA IS INCORRECT..............\n");
}


BLOCK PARITY


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getbinary(int num,int pos)
{
int code=1;
code=code<<(pos-1);
if(code & num)
return 1; // 1 and 1   is 1
else
return 0;
}
int getparity(char block[])
{
int i=0,counter=0;
for(i=0;i<7;i++)
{
if(block[i]=='1') //no of 1 is even then 0 (even parity)
counter++;
}
if(counter % 2 == 0 )
return 0;
else
return 1;
}
void generateError(char ans[])
{
int exitflag=1,pos;
do
{
printf("Enter Position : ");
scanf("%d",&pos);
if(pos>=0 && pos<24)
{
if(ans[pos]=='1')
ans[pos]='0';
else
ans[pos]='1';
}
else
{
printf(" \t>>>Position is out of range<<<\n");
}
printf("Want to Generate More Error (1 / 0) : ");
scanf("%d",&exitflag);
}while(exitflag==1);
}
void xor(char block1[],char block2[],char code[])
{
int i=0;
for(i=0;i<8;i++)
{
if(block1[i]==block2[i])
code[i]='0';
else
code[i]='1';
}
code[i]='\0';
}
void main()
{
int num,i,flag,pid,j=0,k=0;
char block1[50],ans[50],block2[50],code[50];
pid=open("pipe",O_WRONLY);
printf("Enter number : ");
scanf("%d",&num);
for(i=7;i>0;i--)
{
block1[j++]=getbinary(num,i+7) + 48;
block2[k++]=getbinary(num,i) + 48;
}
block1[j++]=getparity(block1) + 48;
block2[k++]=getparity(block2) + 48;
block1[j++]='\0';
block2[k++]='\0';

xor(block1,block2,code);

printf("\nblock 1  : %s",block1);
printf("\nblock 2  : %s",block2);
printf("\n===================");
printf("\ncode(XOR): %s",code);
printf("\n===================\n\n");


strcat(block1,block2); //concate block1 and block2 in block1
strcat(block1,code);


printf("\n======================================================");
printf("\nData to Be Send With Parity : %s",block1);
printf("\n======================================================\n");


printf("\nWant to Generate Error (1 / 0) : ");
scanf("%d",&flag);
if(flag==1)
{
generateError(block1);
printf("\n======================================================");
printf("\nData to Be Send With Parity and with Error : %s",block1);
printf("\n======================================================\n");
}
write(pid,&block1,sizeof(block1));
close(pid);
}

[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int xor(char block1[],char block2[],char code[])
{
int i=0;
for(i=0;i<8;i++)
{
if( !( block1[i]==block2[i] && code[i]=='0' || block1[i]!=block2[i] && code[i]=='1' ) ) //if wrong then send 0
return 0;
}
return 1; //if correct then send 1
}
void main()
{
int pid,i=0;
char ans[50],block1[50],block2[50],code[50];
pid=open("pipe",O_RDONLY);
read(pid,&ans,sizeof(ans));
while(ans[i]!='\0')
{
block1[i]=ans[i];
block2[i]=ans[i+8];
code[i]=ans[i+16];
if(i>=8)
break;
i++;
}
block1[i]='\0';
block2[i]='\0';
code[i]='\0';
printf("\nblock 1  : %s",block1);
printf("\nblock 2  : %s",block2);
printf("\n===================");
printf("\ncode(XOR): %s",code);
printf("\n===================\n");
if(xor(block1,block2,code)==1)
printf("\n..............DATA IS CORRECT..............\n");
else
printf("\n..............DATA IS INCORRECT..............\n");
}


CHARACTER COUNT


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void main()
{
int no,j=0,len,i,k,pid;
char data[50],frame[100];
system("clear");

system(">pipe");
pid=open("pipe",O_WRONLY);

printf("Enter how many data want to enter: ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nEnter data : ");
scanf("%s",data);
len=strlen(data);
frame[j]=len+1+48;
for(k=0;k<len;k++)
{
j++;
frame[j]=data[k];
}
j++;
}
frame[j]='\0';
printf("\nFRAME TO SEND : %s",frame);

write(pid,&frame,sizeof(frame));
}

[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void main()
{
int no,j=0,len,i=0,k,pid;
char data[50],frame[100];
system("clear");

pid=open("pipe",O_RDONLY);
read(pid,&frame,sizeof(frame));

while(frame[i]!='\0')
{
len=frame[i]-48;
i++;
for(k=0;k<len-1;k++)
{
data[k]=frame[i];
i++;
}
data[k]='\0';
printf("\ndata %s",data);
}
}