title: executing commands via sockets with popen()
body: <p>Can anybody give me a hand trying to implement the following server and client?:</p>
  
  <p>The server:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
      int sock = socket(AF_INET, SOCK_STREAM, 0);
  
      struct sockaddr_in serv_addr = { 0 };
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(1234);
      bind(sock, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr));
  
      listen(sock, 128);
  
      struct sockaddr_in cli_addr = { 0 };
      socklen_t cli_addrlen = sizeof(cli_addr);
      int acc_sock = accept(sock, (struct sockaddr *)&amp;cli_addr, &amp;cli_addrlen);
      printf("[+] Connected \n");
      char buf[1024];
      ssize_t nread;
      memset(buf, 0, sizeof(buf));
      int a;
      while (1) {
          nread = read(0, buf, 1024);
          write(acc_sock, buf, nread);
          memset(buf, 0, sizeof(buf));
  
          while ((read(acc_sock, buf, 1024)) != 0) {
              printf("%s", buf);
              memset(buf, 0, sizeof(buf));
          }
      }
  }
  </code></pre>
  
  <p>All the servers does is scanning a command from <code>stdin</code> and send it to the client via sockets. Then scans the client response and prints it out to <code>stdout</code>.</p>
  
  <p>The client:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, const char *argv[]) {
      int sock = socket(AF_INET, SOCK_STREAM, 0);
      struct sockaddr_in serv = { 0 };
      char buf[1024];
      char command[1024];
      memset(buf, 0, sizeof(buf));
      memset(command, 0, sizeof(command));
      int nread;
      FILE *in;
      extern FILE *popen();
  
      serv.sin_family = AF_INET;
      serv.sin_port = htons(atoi(argv[1]));
      serv.sin_addr.s_addr = inet_addr("127.0.0.1");
      int a;  
      connect(sock, (struct sockaddr*)&amp;serv, sizeof(serv));
      while (1) {
          nread = read(sock, buf, 1024);
          in = popen(buf, "r");
          while ((fgets(command, sizeof(command), in)) != NULL) {;
              write(sock, command, sizeof(command));
          }
          memset(buf, 0, sizeof(buf));
      }
      return 0;
  }
  </code></pre>
  
  <p>Essentially the client receives the command scanned by the server, executes it using <code>popen()</code>, and sends the contents of the command line by line until <code>NULL</code>.</p>
  
  <p>The problem is that the code suddenly stops working just after the first command. the transmission of the command and the command's output is satisfactory, however after printing the output of the first command the program just stops working. I think is a problem with <code>fgets()</code>, however I could be wrong. Is there any solutions for this problem?</p>