Thread: NAMED PIPE - 1 record behind

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    NAMED PIPE - 1 record behind

    I have one server, and multiple clients running as seperate tasks, NOT children. When a client initially writes to the named pipe, the server does a line feed. I have no idea where this comes from. It does a line feed, but does not display the buffer. After that, for every client write to the server, the server shows the previous record. In other words, I never see the first write until a second write occurs, and then the server display is always one record behind. I don't understand why the first write stays in the pipe until a second write occurs. Obviously, I need the first write to display when I perform the first write.
    Code:
     /*************************************
    *     Server     Server     Server     Server         *
    *     server_test_1                                          *
     *************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <string.h>
    char *ServerPipe="/tmp/Server.pipe";
    int main(int argc, char *argv[])
    {
         int     Server;
         int     BytesIn;
         char  Buffer[200] = {0};
         int    BufferSize = sizeof(Buffer);
    
         if (access(ServerPipe, F_OK) != 0) {
    
              if (mkfifo(ServerPipe, S_IRWXU) != 0) {
     
                   printf("\n\nCan't create the FIFO\n\n");
                   return(10);
              }
         }
         printf("\n\nServer - Waiting for Client to press Q\n\n");
         Server = open(ServerPipe, O_RDONLY);
         do {
    
              BytesIn=read(Server, Buffer, BufferSize);
              printf("\nBytesIn: %d %d - %s", BytesIn,
                                                              strlen(Buffer),
                                                              Buffer);
              Buffer[1] = '\0';
     
         } while (Buffer[0] != 'Q');
         sleep(1);
         close(Server);
         unlink(ServerPipe);
         printf("\n\nTat's all folken....\n\n");
         return(0);
    }
    
     /***************************************
    *     Client     Client     Client     Client     Client     *
    *                                                                       *
    *     client_test_2                                               *
     ***************************************/
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <termios.h>
    char *ServerPipe="/tmp/Server.pipe";
    
    int main(int argc, char *argv[])
    {
    
         int Server, x = 0;
         char Buffer[200] = {0};
         int  BytesOut;
    
         printf("\n\nClient_Test_1\n\n");
         if (access(ServerPipe, F_OK) != 0) {
    
              printf("\n\nServer not running!!!!\n\n");
              return(10);
         }
         Server=open(ServerPipe, O_WRONLY);
         do {
    
              printf("\nPress 'Q' to quit, 'S' to send...");
              Buffer[0] = getchar();
              if (Buffer[0] == 'S') {
    
                   sprintf(Buffer, "Test record %d from PID: %d", ++x,
                                                                                        getpid());
                   printf("\nSending...");
                   BytesOut = write(Server, Buffer, sizeof(Buffer));
                   printf("BytesOut: %d", BytesOut);
             }
         }
         while (Buffer[0] != 'Q');
         Buffer[1] = '\0';
         write(Server, Buffer, strlen(Buffer));
         close(Server);
         printf("\n\nThat's all folks....\n\n");
         return(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The problem is, that the outputstream is also buffered.
    You can call 'fflush(stdout)' after the printf, or you send an '\n' after the string like:
    Code:
    printf("BytesIn: %d %d - %s\n", BytesIn, strlen(Buffer), Buffer);

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    thank you/n thank you/n thank you/n This was so weird. I was getting all the data, just not as I "expected"... sometimes you have to stand back and get away from it for a bit, and get some assistance from the guru's that are way beyond my abilities. The important thing .... never, ever, ever, give up/n/n/n

    I can not thank you enough. I now can get some sleep and start thinking about sockets.......CR/LF/FLUSH/FLUSH/FLUSH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Named Pipe problem again
    By rahul_c in forum C Programming
    Replies: 4
    Last Post: 02-22-2012, 12:24 PM
  2. Trouble with named pipe...
    By DamienCurr in forum C Programming
    Replies: 5
    Last Post: 03-27-2010, 09:49 PM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Named Pipe Problems.
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2006, 08:35 AM
  5. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 05:54 AM

Tags for this Thread