A call to ssize_t send(int sockfd, const void *buf, size_t len, int flags)
sends at most len
bytes of the buffer pointed to by buf
. It may send fewer bytes than the maximum. In this code, the variable sent
counts the number of bytes that have already been sent on the socket; while this number is less than the total number of bytes that need to be sent, the loop will continue to execute.
For example, assuming the string get
is 50 bytes. In the first iteration of the loop (error handling removed), suppose the socket sends only 30 bytes (instead of the 50 that are requested):
while(sent < strlen(get)) // sent = 0, strlen(get) = 50
{
// o terceiro parâmetro - strlen(get)-sent - é igual a 50
tmpres = send(sock, get+sent, strlen(get)-sent, 0);
// assumindo que o socket enviou 30 bytes, tempres = 30
sent += tmpres; // sent = 30
}
Now the third parameter of send
is equal to 20. If on this second call the socket sends 20 bytes:
while(sent < strlen(get)) // sent = 30, strlen(get) = 50
{
tmpres = send(sock, get+sent, strlen(get)-sent, 0);
// assumindo que o socket enviou 20 bytes, tempres = 20
sent += tmpres; // sent = 50
}
And with all bytes sent, the loop ends.