postheadericon Protocol-2 stop-and-wait protocol

header file


/*
Protocol-2 (stop-and-wait) ,
one-directional flow of data from sender to receiver,
error free channel, The receiver has only a finite buffer capacity and a finite
processing speed, protocol must prevent the sender from flooding
*/

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

//define Frame structure
typedef struct
{
int seqno;
int ackno;
char data[100];
}frame;


//services used @ sender side
void from_network_layer(char buffer[],int sno)
{
printf("Enter Frame [%d] : ",sno);
scanf("%s",buffer);
}

void to_physical_layer(frame *f)
{
int pid1;
system(">pipe1");
pid1=open("pipe1",O_WRONLY);
write(pid1,f,sizeof(frame));
close(pid1);
}

void wait_for_event(int seq_no)
{
int pid2,no;
while(1)
{
pid2=open("pipe2",O_RDONLY);
read(pid2,&no,sizeof(no));
 
if(no==seq_no)
break;

}
close(pid2);
unlink("pipe2");
}

//services used @ reciving side
void from_physical_layer(frame *f)
{
int pid1;
pid1=open("pipe1",O_RDONLY);
read(pid1,f,sizeof(frame));
close(pid1);
unlink("pipe1");
}

void to_network_layer(char buffer[])
{
printf("\nFrame  : %s",buffer);
}

void send_ack(int seqno)
{
int pid2;
system(">pipe2");
pid2=open("pipe2",O_WRONLY);
write(pid2,&seqno,sizeof(seqno));
close(pid2);
}


Sender




#include "header.h"

void main()
{
int i,no,pids;
char buffer[100];
frame f;

printf("How many frame you want to send? : ");
scanf("%d",&no);


for(i=0;i<no;i++)
{
from_network_layer(buffer,i);/*  get some data from network layer to send next layer */
strcpy(f.data,buffer);/*  copy received data into frame's pay load for transmission */
f.seqno=i;
to_physical_layer(&f); /*  send our frame on physical layer */
wait_for_event(i); /* block sender until sender received acknowledment from receciver*/
}
}

postheadericon Protocol 1 utopia

header file


/*
   Protocol 1 (utopia)
   provides for data transmission in one direction only, from sender to receiver.
   The communication channel is assumed to be error free,
   and the receiver is assumed to be able to process all the input infinitely fast.
   Consequently, the sender just sits in a loop pumping data out onto the line as
   fast as it can.

*/


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

//define the frame structure
typedef struct
{
int seqno;
int ackno;
char data[100];
}frame;

//services used @ sender side
void from_network_layer(char buffer[],int sno)
{
printf("Enter Frame [%d] : ",sno);
scanf("%s",buffer);
}

void to_physical_layer(int pid,frame *f)
{
write(pid,f,sizeof(frame));
}


//services used @ reciving side

void from_physical_layer(int pid,frame *f)
{
read(pid,f,sizeof(frame));
}

void to_network_layer(char buffer[],int sno)
{
printf("\nFrame [%d] : %s",sno,buffer);
}

postheadericon Cyclic Redundancy Check - CRC


Sender / CRC Genretor



//CRC-Cyclic Redundancy Check Code

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
void main()
{
int i,j,divisor_len,dividend_len,pid;
char dividend[100],dataword[100], divisor[30],temp[30],quotient[100],remainder[30],divisor1[30];

system("clear");

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

printf("Enter dividend: ");
scanf("%s",dataword);
strcpy(dividend,dataword);

printf("Enter divisor : ");
scanf("%s",divisor);
write(pid,&divisor,sizeof(divisor));
//calculate length of data  
divisor_len=strlen(divisor);

dividend_len=strlen(dividend);

//create backup of original divisor
strcpy(divisor1,divisor);

//left shift rawbits in dividend with divisor_len-1
for(i=0;i<divisor_len-1;i++)
{
dividend[dividend_len+i]='0';
}

//copy original dividend(without padding bits) into temp
for(i=0;i<divisor_len;i++)
{
temp[i]=dividend[i];
}

for(i=0;i<dividend_len;i++)
{
quotient[i]=temp[0];
//select quitient bit ZERO if first bit of current dividend is ZERO otherwise ONE
if(quotient[i]=='0')
{
//if ZERO selected Divisor became all ZERO bits
for(j=0;j<divisor_len;j++)
{
divisor[j]='0';
}
    }
else
{
//if ONE is selected divide with Original divisor
for(j=0;j<divisor_len;j++)
{
divisor[j]=divisor1[j];
}
}
for(j=divisor_len-1;j>0;j--)
{
//do the process of XOR and get Reminder
    if(temp[j]==divisor[j])
{
//temp[j]==divisor[j] : Reminder
// 0 == 0 : 0
// 1 == 1 : 1
    remainder[j-1]='0';
}
    else
{
//temp[j]==divisor[j] : Reminder
// 0 == 1 : 1
// 1 == 0 : 1

    remainder[j-1]='1';
}
}
remainder[divisor_len-1]=dividend[i+divisor_len];
strcpy(temp,remainder);
}
for(i=0;i<divisor_len-1;i++)
{
remainder[i]=temp[i];
}
remainder[i]='\0';

system("clear");
printf("\n[%d] - Dataword : %s",strlen(dataword),dataword);
printf("\n[%d] - Divisor  : %s",strlen(divisor1),divisor1);
printf("\n[%d] - CRC      : %s",strlen(remainder),remainder);
strcat(dataword,remainder);
printf("\n[%d] - Codeword : %s\n",strlen(dataword),dataword);
write(pid,&dataword,sizeof(dataword));
close(pid);

}


postheadericon Block Parity Check - Error Detection and Correction Techniques



Sender / Block Parity Generator 


//block parity sender
#include<stdio.h>
#include<string.h>
#include<fcntl.h>

// one bit for NULL char and
// second for Parity bit

#define Frame_Size 32 // size of frame in bit

#define ROW 4+1 //segments
#define COL 8+2 // m bits in a segment [ +2 for '*' and '\0' ]


int getBit(int frameData,int pos)
{
//get binary equavalient of our Frame Data by setting on bit by bit

int code=1; // take base of binary 1 [ 00000001 ]

code=code<<(pos-1); // do the left shift to find  postion

if(frameData & code)  // if both bits are 1 then set bit otherwise return ZERO
return 1;
else
return 0;

}


void blockParity(char myframe[ROW][COL])
{
int i=0,j=0,ROW_count=0,COL_count=0;

//[ Horizontal parity ]
for(i=0;i<ROW;i++)//ROW's Parity Check
{
ROW_count=0;
for(j=0;j<COL-2;j++) //we r using -2 because we want to exclude last two bit that is * and NULL
{
if(myframe[i][j] == '1')//count ROW Parity
{
ROW_count++;
}
}
if((ROW_count % 2) == 0) //set ROW parity bit
myframe[i][j] = '0' ;
else
myframe[i][j] = '1';
}

//[ Virtical parity ]
for(i=0;i<COL-2;i++)//COL's Parity
{
COL_count=0;
for(j=0;j<ROW;j++)
{
if(myframe[j][i] == '1')//count COL Parity
{
COL_count++;
}

}
if((COL_count % 2) == 0) //set COL parity bit
myframe[ROW-1][i] = '0' ;
else
myframe[ROW-1][i] = '1';

}

}

postheadericon Checksum - Error Detection and Correction Techniques


checksum.h


//checksum header file
#include<stdio.h>
#include<string.h>
# define segment_len 8
# define no_segments 4
# define frameSize 32
int getBit(int frameData,int pos)
{
int code=1; //base
code=code<<(pos-1);

if(frameData & code)
return 1;
else
return 0;
}

void sum(char segment1[],char segment2[])
{
int i,j;
//int segment_len=strlen(segment1);
int ans=0,carry=0;
char ans_segment[9]=" ";
// segment_len--;//because starting from zero

for(i=segment_len-1;i>=0;i--)
{
ans=0;
ans=( (int)segment1[i]-48 ) + ( (int)segment2[i]-48 ) + carry; //sum of bits on each position of two segments

if(ans>=2)//carry genrated
{
carry=1;
if(ans==2)
ans=0; // 1 + 1 = 10 ans
else
ans=1;//  1 + 1 + 1(carry) = 11 ans
}
else
{
carry=0;
}
ans_segment[i]=ans+48;

}
ans_segment[segment_len+1]='\0';
strcpy(segment1,ans_segment);

if(carry==0)
return;
else
sum(segment1,"00000001"); //recursive call of function if carry genrated
}


void complement(char segment[])
{
int i,len=strlen(segment);
for(i=0;i<len;i++)
{
if(segment[i]=='1')
segment[i]='0';
else
segment[i]='1';
}
segment[i]='\0';
}


postheadericon GTU Mobile Computing - Android Result 2012-13


Hurray...!

By the grace of God we secured 100% result in the subject of Mobile Computing (Android), continuously second time in Gujarat Technological Exam [Sem-4] in year 2012-13.

I would like to extend thanks to all my students for their sincere efforts.


Bipin Ruapdiya

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.