FIFO "connections"

This is a discussion on FIFO "connections" within the Linux Programming forums, part of the Platform Specific Boards category; I've this code and a client that send data on the FIFO. I start the client and the daemon below ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    26

    FIFO "connections"

    I've this code and a client that send data on the FIFO.
    I start the client and the daemon below works perfectly. But when the client stop beacause of while cycle we go to "read function" and now (after the termination of the client) there is no wait for an another client and the read function reads the last structure.
    what's the problem?

    Code:
    #define FIFO_PATH "/home/np2k/Desktop/fifo.txt"
    
    struct data {
      int channel;
      int pid;
    };
    
    int main()
    {
      int fd;
      int fifod;
      printf("daemon> started...\n");
      if (mkfifo(FIFO_PATH,S_IWUSR|S_IRUSR) == -1)
        {
          perror("mkfifo");
          exit(1);
        }
      if ( (fd = open(FIFO_PATH,O_RDONLY)) == -1)
        {
          perror("open");
          exit(1);
        }
      
      while(1)
        {
          struct data getdata;
    
          printf("daemon> waiting for connection...\n"); fflush(NULL);
          if (read(fd,&getdata,sizeof(struct data)) == -1)
            {
              printf("***************************\n");
              perror("read");
              exit(1);
            }
    
          printf("daemon> connection ok...\n");
          printf("daemon> channel: %d, pid: %d\n",getdata.channel,getdata.pid);
          
          char fifo_name[100];
          char buf;
          sprintf(fifo_name, "%d", getdata.pid);
          
          strcat(fifo_name,"OK");
          printf("daemon> fifo name: %s\n",fifo_name);
    
          if (mkfifo(fifo_name,S_IWUSR|S_IRUSR) == -1)
            {
              perror("mkfifo");
              exit(1);
            }
          if ( (fifod = open(fifo_name,O_RDONLY)) == -1)
            {
              perror("open");
              exit(1);
            }
          
          printf("daemon> WAITING....\n"); fflush(NULL);
          read(fifod,&buf,1);
          printf("daemon>>>>>>>>>%c\n",buf); fflush(NULL);
          if (buf == 's')
            {
              printf("daemon> start ok\n");
              close(fifod);
            }
        }
    
      close(fd);
    
      return 0;
    }
    Last edited by np2k; 06-08-2010 at 07:10 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    For starters, why do you create a new fifo everytime instead of resuing the original one "/home/np2k/Desktop/fifo.txt"? And what is fflush(NULL)?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,582
    Quote Originally Posted by itCbitC
    And what is fflush(NULL)?
    Flush all open output streams.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by laserlight View Post
    Flush all open output streams.
    Cool beans!
    I wasn't sure if that was correct but you and the manpage cleared it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. select system call with FIFO fd.
    By vlrk in forum Linux Programming
    Replies: 0
    Last Post: 05-11-2009, 04:27 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  3. working with FIFO files
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 11:35 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. FIFO list
    By Lillian176 in forum C Programming
    Replies: 4
    Last Post: 04-13-2003, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21