postheadericon Character Count - Framing Technics


Sender


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

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

printf("How many Frame you data want to send : ");
scanf("%d",&Frame_Count);
for(i=0;i<Frame_Count;i++)
{
printf("\nEnter Frame [%d] : ",i);
scanf("%s",Frame_Data);
len=strlen(Frame_Data);
targeted_frame[j]=len+1+48; //set Header [header + total char followed by header]
for(k=0;k<len;k++)
{
j++;
targeted_frame[j]=Frame_Data[k];
}
j++;
}
targeted_frame[j]='\0';
printf("\nFRAME TO SEND : %s",targeted_frame);

write(pid,&targeted_frame,sizeof(targeted_frame));
}
 /*2i5love6india*/


Receiver


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

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

while(targeted_frame[i]!='\0')
{
len=targeted_frame[i]-48;
i++;
for(k=0;k<len-1;k++)
{
Frame_Data[k]=targeted_frame[i];
i++;
}
Frame_Data[k]='\0';
printf("\nFrame received :  %s\n",Frame_Data);
}
}
/*
i love india
*/


Code using command line argument



Sender



#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void main(int c, char *argv[])
{
int Frame_Count,j=0,len,i,k,pid;
char Frame_Data[50],targeted_frame[100];

 char myPipe[50];

// concatenation of   >  with pipe name available in command line argument number one
 sprintf(myPipe, "%s%s", ">", argv[1]);

 system("clear");
 system(myPipe);
 pid=open(argv[1],O_WRONLY);

printf("How many Frame you data want to send : ");
scanf("%d",&Frame_Count);
for(i=0;i<Frame_Count;i++)
{
printf("\nEnter Frame [%d] : ",i);
scanf("%s",Frame_Data);
len=strlen(Frame_Data);
targeted_frame[j]=len+1+48; //set Header [header + total char followed by header]
for(k=0;k<len;k++)
{
j++;
targeted_frame[j]=Frame_Data[k];
}
j++;
}
targeted_frame[j]='\0';
printf("\nFRAME TO SEND : %s",targeted_frame);

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


 /*2i5love6india*/

Receiver

#include<fcntl.h>
#include<stdio.h>
#include<string.h>
void main(int c, char *argv[])
{
int no,j=0,len,i=0,k,pid;
char Frame_Data[50],targeted_frame[100];
system("clear");
pid=open(argv[1],O_RDONLY);  
if (pid <= 0)  
{
printf("Pipe does not exist. \n");
exit(1);  
}
else
{
read(pid,&targeted_frame,sizeof(targeted_frame));

while(targeted_frame[i]!='\0')
{
len=targeted_frame[i]-48;
i++;
for(k=0;k<len-1;k++)
{
Frame_Data[k]=targeted_frame[i];
i++;
}
Frame_Data[k]='\0';
printf("\nFrame received :  %s\n",Frame_Data);
}
}
}
/*
i love india

*/


How to run:


Compile sender

gcc sender.c -o sen

Execute sender

./sen


Compile receiver

gcc receiver.c -o rec

Execute receiver

./rec

2 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.