title: Proper FIFO client-server connection
body: <p>I'm trying to write simple client and server C programs, communicating with each other in separate terminals.</p>
  
  <p>The server has to create a public fifo and wait for the client. Meanwhile the client is creating his own fifo through which the server's response will come. The task of the client is sending the server a name created by the queue and get in return the result of the <code>ls</code> command.</p>
  
  <p>I did search for an answer, for example: <a href="https://stackoverflow.com/questions/8045626/fifo-server-program">fifo-server-program</a>, <a href="https://stackoverflow.com/questions/4113986/example-of-using-named-pipes-in-linux-bash">example-of-using-named-pipes-in-linux-bash</a>, <a href="https://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes">how-to-send-a-simple-string-between-two-programs-using-pipes</a>. I started with the code from the third link and slowly modified it.</p>
  
  <p>What I've got now, is a client taking input from the user, sending it to the server and receiving it back. But it only works once. I have no idea why. The body of main function is below. I will be grateful for any help.</p>
  
  <p><strong>EDIT:</strong>
  I got it working! :D The codes are below, maybe it will help someone.</p>
  
  <p>The server.c code:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char* argv[])
  {
      int fds[2];
      char tab[BUFSIZ];
      int fd, n;
  
      char *myfifo = "/tmp/serwer";
      char *myfifo2 = "/tmp/client";
  
      pipe(fds);
      mkfifo(myfifo,0666);
  
      while(1)
      {
          fds[0]=open(myfifo2,O_RDONLY);
          fds[1]=open(myfifo,O_WRONLY);
  
          read(fds[0],tab,BUFSIZ);
  
          if (strcmp("klient",tab)==0) {
              printf("Od klienta: %s\n",tab);
              fd=open(tab,O_WRONLY);
  
              if(fork()==0)
              {
                  dup2(fds[1],1);
                  close(fds[1]);
                  execlp("ls","ls","-l",NULL);
                  close(fds[0]);
                  close(fds[1]);
              }
              else
              {
                  dup2(fds[0],0);
                  n = read(fds[0],tab,BUFSIZ);
                  write(fd,tab,n);
                  close(fds[0]);
                  close(fds[1]);
              }
          }
          memset(tab, 0, sizeof(tab));
          close(fd);
          close(fds[0]);
          close(fds[1]);
      }
  
      unlink(myfifo);
      return 0;
  }
  </code></pre>
  
  <p>The client.c code:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char* argv[])
  {
      int fds[2];
      char *myfifo = "/tmp/serwer";
      char *myfifo2 = "/tmp/client";
  
      mkfifo(myfifo2,0666);
      fds[0]=open(myfifo,O_RDONLY);
      fds[1]=open(myfifo2,O_WRONLY);
  
      char tab[BUFSIZ];
      memset(tab, 0, sizeof(tab));
  
      write(fds[1],"klient",6);
      perror("Write:"); //Very crude error check
      read(fds[0],tab,sizeof(tab));
      perror("Read:"); // Very crude error check
  
      printf("Odebrano od serwera: %s\n",tab);
  
      close(fds[0]);
      close(fds[1]);
      unlink(myfifo2);
      return 0;
  }
  </code></pre>