client /server pipes

here is the concept:

the client reads a pathname from the standard input and writes it to pipe1.The server reads this pathname from the pipe1 and tries to open the file for reading.If the server can open the file ,the server responds by reading the file and writting it to pipe2;otherwise the server writes an error message to the same pipe.The client then reads from the pipe2 and writes it receives to the standard output

here is my code:

#include <stdio.h>
#include <string.h>
#define maxsize 1000

char buffer [maxsize];

void client(int readfd,int writefd)
{
   puts("give pathname:");
   gets(buffer);

   write(writefd,buffer,strlen(buffer));
   //sleep(10);
   while((read(readfd,buffer,strlen(buffer)))>0);
     printf("%s/n",buffer);

}

void server(int readfd,int writefd)
{
   FILE * fp;
   char line[100];
   //sleep(10);
   read(readfd,buffer,strlen(buffer));
   fp=fopen(buffer,"r");
   if(fp==NULL)
   {
       strcpy(buffer,"cannot oepn file");
       write(writefd,buffer,strlen(buffer));
   }
   else
     while(fgets(line,100,fp)!=NULL)
       write(writefd,line,100);
}

int main()
{
    int pipe1[2],pipe2[2];
    int childpid;
    int status;
    pipe(pipe1);
    pipe(pipe2);
    childpid=fork();
    if(childpid>0)
    {
        close(pipe1[0]);
        close(pipe2[1]);
        client(pipe2[0],pipe1[1]);
        wait(&status);
        exit(0);
    }
    else
    {
        close(pipe1[1]);
        close(pipe2[0]);
        server(pipe1[0],pipe2[1]);
        exit(0);
    }

}

Actually ,I understand that there might be some problems with race conditions that's why i put 2 sleep commands but still doesn't work..
generally i want to sychronize the read and write commands of the client and server but I don't know how...
if someone has the time to see the code and give me some advice,it will really be appreciated.....thanks in advance..

Wow! That's some ugly code you got there. Here's the working version:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 1000

char buffer [maxsize];
void client(int readfd,int writefd)
{
   printf ("give pathname: ");
   fflush (stdout);
   fgets (buffer, sizeof (buffer), stdin);

   write(writefd,buffer, sizeof (buffer));
   while (read(readfd,buffer,sizeof(buffer)) > 0)
        printf("%s",buffer);
}

void server(int readfd,int writefd)
{
   FILE * fp;
   char line[1000];

   read(readfd,buffer,sizeof(buffer));
   if (strchr (buffer, '\n'))
        *strchr(buffer, '\n') = '\0';
   fp=fopen(buffer,"r");
   if(fp==NULL)
   {
       strcpy(buffer,"cannot open file");
       write(writefd,buffer,strlen(buffer));
   }
   else
     while(fgets(line,sizeof(line),fp)!=NULL)
       write(writefd,line,sizeof(line));
}

int main()
{
    int pipe1[2],pipe2[2];
    int childpid;
    int status;
    pipe(pipe1);
    pipe(pipe2);
    childpid=fork();
    if(childpid>0)
    {
        close(pipe1[0]);
        close(pipe2[1]);
        client(pipe2[0],pipe1[1]);
        wait(&status);
        exit(0);
    }
    else
    {
        close(pipe1[1]);
        close(pipe2[0]);
        server(pipe1[0],pipe2[1]);
        exit(0);
    }

}

There were several things wrong. Diff to find mistakes.

Hope it helps!

thanks it helped a lot!!:slight_smile:
Actually,I have another prob right now...
i am trying to make the command cat <filename> | wc -l

well,there seems to be a problem in the reading of the last line from the buffer

//implementation of cat <filename> | wc -l

#include <stdio.h>
#include <string.h>
#define size 500
int main(int argc,char * argv[])
{

   int pipe1[2];
   int childpid;
   FILE * fp;
   char buffer;
   int status;
   int lines;
   if(argc!=2)
   {
     printf("usage : mycat <file name>\n");
     exit(1);
   }
   pipe(pipe1);
   childpid=fork();
   if(childpid>0)
   {
      close(pipe1[0]);
      fp=fopen(argv[1],"r");
      if(fp==NULL)
      {
         printf("Cannot open file");
         exit(1);
      }
     while(fgets(buffer,sizeof(buffer),fp)!=NULL)
       write(pipe1[1],buffer,strlen(buffer));

      fclose(fp);
      wait(&status);
      close(pipe1[1]);
      exit(0);
   }
   else
   {
      close(pipe1[1]);
      lines=0;
       while (read(pipe1[0],buffer,sizeof(buffer)) > 0)
           lines++;
      printf("%d",lines);
      close(pipe1[0]);
      exit(0);
   }
}

take a look if you can...thx in advance...