title: Changing STDOUT to file in ncat source code
body: <p>I managed to compile ncat. I am using -k option to keep server open. Instead of accepting data to STDOUT, my goal is to write to files instead. So far I was able to write to a file instead of STDOUT but my goal is to loop through new files on each new connection. Right now it is appending to the same filename_0 and f++ is not incrementing. Here is what I have so far. The original code will be below. The difference is in the else clause, basically if n is actually greater than 0. On each loop, n is 512 bytes until the last chunk. I just want to be able to have new files from each new connection. filename_0, filename_1, filename_3, etc.</p>
  <p>MODIFIED CODE:</p>
  <pre><code>/* Read from a client socket and write to stdout. Return the number of bytes
     read from the socket, or -1 on error. */
  int read_socket(int recv_fd)
  {
      char buf[DEFAULT_TCP_BUF_LEN];
      struct fdinfo *fdn;
      int nbytes, pending;
      int f = 0;    
  
      fdn = get_fdinfo(&amp;client_fdlist, recv_fd);
      ncat_assert(fdn != NULL);
      nbytes = 0;
      do {
          int n, s;
    
          n = ncat_recv(fdn, buf, 512, &amp;pending);
          
          if (n &lt;= 0) {
              if (o.debug)
                  logdebug(&quot;Closing fd %d.\n&quot;, recv_fd);
  #ifdef HAVE_OPENSSL
              if (o.ssl &amp;&amp; fdn-&gt;ssl) {
                  if (nbytes == 0)
                      SSL_shutdown(fdn-&gt;ssl);
                  SSL_free(fdn-&gt;ssl);
              }
  #endif
              close(recv_fd);
              checked_fd_clr(recv_fd, &amp;master_readfds);
              rm_fd(&amp;client_fdlist, recv_fd);
              checked_fd_clr(recv_fd, &amp;master_broadcastfds);
              rm_fd(&amp;broadcast_fdlist, recv_fd);
  
              conn_inc--;
              if (get_conn_count() == 0)
                  checked_fd_clr(STDIN_FILENO, &amp;master_readfds);
  
              return n;
          }
          else {
              char filename[20];
              snprintf(filename, sizeof(char) * 20, &quot;filename_%i&quot;, f);
              FILE *fp = fopen(filename, &quot;a&quot;);
              if (fp == NULL)
              {
                  printf(&quot;Could not open file&quot;);
                  return 0;
              }
              //Write(STDOUT_FILENO, buf, n);
              s = fwrite(buf, 1, n, fp);
              fclose(fp);
              f++;
              nbytes += n;
          }
  
      } while (pending);
  
      return nbytes;
  }
  </code></pre>
  <p>ORIGINAL CODE:</p>
  <pre><code>int read_socket(int recv_fd)
  {
      char buf[DEFAULT_TCP_BUF_LEN];
      struct fdinfo *fdn;
      int nbytes, pending;
  
      fdn = get_fdinfo(&amp;client_fdlist, recv_fd);
      ncat_assert(fdn != NULL);
  
      nbytes = 0;
      do {
          int n;
  
          n = ncat_recv(fdn, buf, sizeof(buf), &amp;pending);
          if (n &lt;= 0) {
              if (o.debug)
                  logdebug(&quot;Closing fd %d.\n&quot;, recv_fd);
  #ifdef HAVE_OPENSSL
              if (o.ssl &amp;&amp; fdn-&gt;ssl) {
                  if (nbytes == 0)
                      SSL_shutdown(fdn-&gt;ssl);
                  SSL_free(fdn-&gt;ssl);
              }
  #endif
              close(recv_fd);
              checked_fd_clr(recv_fd, &amp;master_readfds);
              rm_fd(&amp;client_fdlist, recv_fd);
              checked_fd_clr(recv_fd, &amp;master_broadcastfds);
              rm_fd(&amp;broadcast_fdlist, recv_fd);
  
              conn_inc--;
              if (get_conn_count() == 0)
                  checked_fd_clr(STDIN_FILENO, &amp;master_readfds);
  
              return n;
          }
          else {
              Write(STDOUT_FILENO, buf, n);
              nbytes += n;
          }
      } while (pending);
  
      return nbytes;
  }
  </code></pre>