08 March, 2014

CharCount

CharCount Receiver

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

struct frame
{
        char c;
        char *info;
}f;

main()
{
        int fd,i,j,file1,no;
    char ch;

        if((fd=open("testfile",O_RDONLY))<0)
        {
                perror("File open failed");
        }

        if((file1=open("output.txt",O_WRONLY))<0)
        {
                perror("Output file open failed");
        }
        for(;;)
        {

                if((i=read(fd,&f.c,1))>0)
                {
                        printf("\nCounter: %d",f.c);
                }

        no=f.c;

        f.info=(char *)malloc(sizeof(char) * no);

        for(j=0;j<no;j++)
        {
                    if((i=read(fd,&ch,1))<=0)
                        break;
           
            f.info[j]=ch;
        }
          printf("\nReceiver msg : %s",f.info);
   
              printf("\n");
          
         if((i=write(file1,f.info,f.c))<=0)
                      break;
                 
               printf("\n");
            free(f.info);
         }
}


CharCount Sender

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

struct frame
{
        char c;
        char *info;
}f;

main()
{
        int fd,i,j,file1;
        int no;
        char ch;

        if((fd=open("testfile",O_WRONLY))<0)
        {
                perror("File open failed");

        }

        if((file1=open("input.txt",O_RDONLY))<0)
        {
                perror("Input file open failed");
        }

        for(;;)
        {
            
                no=rand() % 10;

                f.info=(char *)malloc(sizeof(char)*no);

                f.c=no;

                   if(read(file1,f.info,no)<=0)
                   break;
       

                 if((i=write(fd,&f.c,1))<=0)
                {
                       perror("Message write failed");
                }
       

        for(j=0;j<no;j++)
        {
            ch=f.info[j];
                    if((i=write(fd,&ch,1))<=0)
                    {
                              perror("Message write failed");

                    }
           
        }
                

            //      printf("%s",f.info);

        }

}