title: How to implement a TCP server that receives and sends data in two processes?
body: <p>I am trying to implement a TCP server using C in Linux. I want this server to accept incoming data forever from multiple clients and at the same time send some data back to each connected client in every 3 seconds.</p>
  
  <p>My problem is I don't know how to properly do <code>send()</code> in a different process than the one handling the client.</p>
  
  <p>What I am doing is at the beginning of the program do a <code>fork()</code> and do</p>
  
  <pre><code>while (1) {
    sleep(3);
    // compute and `send()` data to each connected peers
  }
  </code></pre>
  
  <p>in child process, do</p>
  
  <pre><code>sock = create_socket();
  while (1) {
    client_sock = accept_connection(sock);
    if (fork() == 0) {
      close(sock);
      handle_client(client_sock);
      exit(0);
    }
    close(client_sock);
    // clean up zombies
  }
  </code></pre>
  
  <p>in parent process. <code>handle_client()</code> is simply <code>recv()</code> data in a infinite loop. Because <code>send()</code> and <code>recv()</code> are executed in different processes, I couldn't use the socket file descriptors to <code>send()</code> in parent process. What do I need to do in the parent process to do the <code>send()</code>?</p>