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? 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);
}
<---- updated server: ---->
// named pipe 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];
fprintf(stderr, "Pipe server running...\n");
// try to open my fifo file. it may already exist from
// an earlier aborted execution
fp = fopen(FIFO_FILE, "r");
// if the fopen failed, the fifo file does not exist
if (NULL == fp)
{
// Create the FIFO file
umask(0);
if(mknod(FIFO_FILE, S_IFIFO|0666, 0))
{
fprintf(stderr, "mknod() failed\n");
return 1;
}
fp = fopen(FIFO_FILE, "r"); // now open the fifo file
}
printf("Receiving...\n");
// while we can read (up to 9) chars from the fifo...
// Note: readbuf[] will be nul-terminated and will
// include any newlines read from the pipe. since
// we are reading so few bytes at once (9) it will
// take several iterations of the 'while loop' to read
// any long lines written to the pipe by clients.
while(NULL != fgets(readbuf, 10, fp))
{
// print the string just read to my stdout
printf("%s", readbuf);
fflush(stdout);
}
fclose(fp); // close the fifo file
remove(FIFO_FILE); // delete the fifo file
fprintf(stderr, "Pipe server terminating.\n");
return(0);
}
<---- updated client: ---->
// named pipe client
#include <stdio.h>
#include <stdlib.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[])
{
FILE *fp;
char str[20];
// try to open an existing fifo file
if((fp = fopen(FIFO_FILE, "w")) == NULL)
{
perror("fopen");
exit(1);
}
// get up to 19 chars from stdin into str[].
// str[] will be nul-terminated and any newlines in
// the input will be written to the pipe (fp).
while( fgets(str, sizeof(str), stdin) != NULL )
{
// write the chars read from stdin to the pipe
fprintf(fp, "%s", str);
fflush(fp); // flush any buffered IO to the pipe
// echo what I wrote to the pipe to my stdout
fprintf(stdout, "Sent: %s\n", str);
fflush(stdout);
}
fclose(fp); // close the pipe
return(0);
}
Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.