named pipe problem on linux

R

richard

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);
}
 
S

Sharad Kala

richard said:
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.

Sorry but this question is off-topic on c.l.c++. Try

Sharad
 
S

Sybren Stuvel

["Followup-To:" header set to comp.os.linux.]
richard enlightened us with:
I have a simple test to pass information from a client to a server
using named pipe.

Using C. But posting to a C++ group. Make up your mind, but at least
don't crosspost.
anything I missed?

Try flushing.

Sybren
 
L

Larry I Smith

richard said:
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
 
R

richard

richard said:
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


thanks, Larry.
 
S

Sybren Stuvel

["Followup-To:" header set to comp.os.linux.]
richard enlightened us with:
thanks, Larry.

Geesh, you quoted 183 lines just to write one! Learn how to properly
snip your post!

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top