Reputation: 6156
I am trying to understand some of the concepts regarding TCP data transmission.
Suppose we are using a socket (in C
) to send and receive GET HTTP
requests for a website. Regarding to the receiving end, what I have seen is the implementation below. Basically, a response
buffer is created and filled up iteratively.
memset(response, 0, sizeof(response));
total = sizeof(response)-1;
received = 0;
while (received < total) {
bytes = recv(sockfd, response + received, total - received, 0);
if (bytes < 0)
error("ERROR reading response from socket");
if (bytes == 0)
break;
received += bytes;
}
The following two things weren't very clear to me
response
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.socket
?Upvotes: 0
Views: 103
Reputation: 26717
Where does the response get data from, cache from the OS? I haven't learnt operating systems, which makes it difficult for me to comprehensive the buffering process.
Maybe, maybe not, you don't care as you are the user of the feature. All of this is guaranteed by the TCP socket protocol. However if you forget to read the socket the socket data will be full. There is a limit to the data you can queue.
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 socket?
Yes. Tou don't need to worried about that. Beautiful, isn't it?
With respect to your code, I anticipate some problems:
// not necessary as you should only read the bytes affected by recv()
// memset(response, 0, sizeof(response));
// should declare total and received here
size_t total = sizeof(response) - 1;
size_t received = 0;
while (received < total) {
// should declare byte only here
ssize_t bytes = recv(sockfd, response + received, total - received, 0);
if (bytes < 0)
error("ERROR reading response from socket");
if (bytes == 0)
break;
received += (size_t)bytes;
}
// as you want read a string don't forget
response[received] = '\0';
Upvotes: 0
Reputation: 73051
Where does the response 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.
The TCP packets are received from the network device (Ethernet card, Wi-Fi adapter, etc), and their payload-data is placed into a temporary buffer inside the TCP/IP stack. When your program calls recv()
, some or all of that data is copied from the temporary buffer into your program's buffer (response
).
The TCP/IP stack will not do any caching of data other than what is described above. (e.g. if a web browser wants to cache a local copy of the web page so that it won't have to download it a second time, it will be up to the web browser itself to do that at the application level; the TCP/IP stack and the OS will not do that on their own)
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 socket?
It is handled transparently inside the TCP stack. In particular, each TCP packet has a checksum and a sequence-number included in its header, and the TCP stack checks that sequence-number of each packet it receives, to make sure that it matches the next number in the sequence (relative to the previous packet it received from the same TCP stream). If it is not the expected next-packet-number, then the TCP stack knows that a packet was lost somehow, and it responds by sending a request to the remote computer that the dropped packet(s) be resent. Note that the TCP stack may have to drop subsequent packets as necessary until the originally-expected sequence can be resumed, because it is required to deliver the payload data to your application in the exact order in which it was sent (i.e. it isn't allowed to deliver "later" bytes before "earlier" bytes, even if some of the "earlier" bytes got lost and had to be retransmitted).
Upvotes: 1