title: How does TCP datagram arrive at a client computer?
body: <p>I am trying to understand some of the concepts regarding TCP data transmission.</p>
  
  <p>Suppose we are using a socket (in <code>C</code>) to send and receive <code>GET HTTP</code> requests for a website. Regarding to the receiving end, what I have seen is the implementation below. Basically, a <code>response</code> buffer is created and filled up iteratively.</p>
  
  <pre><code>memset(response, 0, sizeof(response));
  total = sizeof(response)-1;
  received = 0;
  while (received &lt; total) {
      bytes = recv(sockfd, response + received, total - received, 0);
      if (bytes &lt; 0)
          error("ERROR reading response from socket");
      if (bytes == 0)
          break;
      received += bytes;
  }
  </code></pre>
  
  <p>The following two things weren't very clear to me</p>
  
  <ol>
  <li>Where does the <code>response</code> get data from, cache from the OS or data directly from the website? I haven't learnt operating systems, which makes it difficult for me to comprehensive the buffering process.</li>
  <li>Also, theoretically TCP would check for transmission loss, where does this happen? When I do the socket programming, I didn't see any handlers regarding transmission loss, is it automatically handled by <code>socket</code>?</li>
  </ol>