client /server pipes


 
Thread Tools Search this Thread
Top Forums Programming client /server pipes
# 1  
Old 08-23-2008
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:

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..
# 2  
Old 08-23-2008
Wow! That's some ugly code you got there. Here's the working version:
Code:
#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!
# 3  
Old 08-24-2008
thanks it helped a lot!!Smilie
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

Code:
//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[size];
   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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sftp script for dev server to client server

hi, i am new to unix, cuold u send some sftp acripts to send files to dev server to clint server, (1 Reply)
Discussion started by: Koti.annam
1 Replies

2. Programming

Trouble with pipes in chat client on linux

I'm writing a simple chat client in C++ on linux to connect to a win32 chat server on my computer also written in C++. I'm confident that the server works but the chat client is giving me some trouble. I'm forking the chat client and have one process dealing with incoming messages and another... (14 Replies)
Discussion started by: vindy
14 Replies

3. UNIX for Dummies Questions & Answers

Client server C

Hello, Please, is there on unix.com the source code of a client C and server C: as shown in following figure: File:InternetSocketBasicDiagram zhtw.png - Wikipedia, the free encyclopedia Thank you so much for help (1 Reply)
Discussion started by: chercheur857
1 Replies

4. Programming

Client Server C

Hello, Please I would create a client and a server in C that communicate frequently. The client sends "hello message" to the server, the server waits a few minutes and sends a "hello message" to the client, the client sends again "hello server ".. etc up to 15 minutes Can you guide me... (3 Replies)
Discussion started by: chercheur857
3 Replies

5. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

6. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

7. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

8. Shell Programming and Scripting

Client-server

Hi, I have installed the vmware server on my debian os and has several clients connected to it. Is there any script that enable the server to restart the client automatically?? Can anyone help. Thanks in advance (3 Replies)
Discussion started by: kanexxx
3 Replies

9. UNIX for Dummies Questions & Answers

client/server

Hi, yes i belong to that duummies group of people so here is the question that i need someone to explain it to me and posiblly to answere it to me in a plain english. This is the question: Describe the concept of “client-server” software. Discuss what each side of the equation... (2 Replies)
Discussion started by: bole
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question