I assume you are writing to the FIFO multiple times, by running a program more than once. Your code should keep opening the FIFO and then reading until it hits the End of File. The easiest way to do this is to loop as follows:-
Sorry, I mixed up my client and server in above. The server should be reading from the FIFO and the client should be writing to it.
So each time the FIFO is opened for writing by the "client", the "server" will open the FIFO for reading. The "server" will keep reading from the FIFO until the "client" closes its writing handle. The fgets will then fail, by returning NULL and you should close the FILE handle and try again.
Hope this helps!
Code:
#include <cstdio>#include <cstring>#include <iostream>int main(){ while (true) { FILE *fp = fopen("/tmp/my_named_pipe", "r"); if (fp) { char msgbuf[1024]; while (fgets(msgbuf, sizeof(msgbuf), fp)) { printf("Received %s", msgbuf); } fclose(fp); } } return 0;}
So each time the FIFO is opened for writing by the "client", the "server" will open the FIFO for reading. The "server" will keep reading from the FIFO until the "client" closes its writing handle. The fgets will then fail, by returning NULL and you should close the FILE handle and try again.
Hope this helps!
Statistics: Posted by AndyD — Sun Feb 11, 2024 12:44 pm