Unix Programming - named pipe problem on linux

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2004 > named pipe problem on linux





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author named pipe problem on linux
richard

2004-11-01, 5:53 pm

I have a simple test to pass information from a client to a server
using named pipe. what I really want is: when I type a line on the client,
the server will output the line immediately. but to my surprise, I always
have to terminate the client to get the server in action, i.e. prints out
what I typed.

anything I missed? I am compiling using gcc without any option.

thanks,
---RICH

---------------------
server
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);

while(1)
{
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 10, fp);
printf("Received string: %s\n", readbuf);
fclose(fp);
}

return(0);
}

-------
client
-------
#include <stdio.h>
#include <stdlib.h>

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}

while( scanf("%s", str) != EOF )
{ fputs(str, fp);
fputs(str, stdout);
}

fclose(fp);
return(0);
}


DINH Viet Hoa

2004-11-01, 5:53 pm

richard wrote :

> I have a simple test to pass information from a client to a server
> using named pipe. what I really want is: when I type a line on the client,
> the server will output the line immediately. but to my surprise, I always
> have to terminate the client to get the server in action, i.e. prints out
> what I typed.
>
> anything I missed?


the fact that 'FILE *' is generally buffered.

--
DINH V. Hoa,

"s/^\(\(\\.\|[^\[]\|\[\(\^.\|[^^]\)[^]]*\]\)*\)\(\\[^\*[]\)/\1\\\4/"
-- Stéphane CHAZELAS

Wayne C. Morris

2004-11-01, 5:53 pm

In article <pan.2004.11.01.16.51.34.41781@sbcglobal.net>,
"richard" <byang1@sbcglobal.net> wrote:

> I have a simple test to pass information from a client to a server using
> named pipe. what I really want is: when I type a line on the client, the
> server will output the line immediately. but to my surprise, I always
> have to terminate the client to get the server in action, i.e. prints
> out what I typed.
>
> anything I missed?


man fflush
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com