Haagenti
Haagenti

Reputation: 8154

C programming Sockets recv

I have made a successful call web request with a socket and printed out the result. This is done with the send and recv method. Now I would like to be able to give a ruff indication of how many bytes are received and how many bytes needs to be downloaded. But I have simply no clue how to achieve this in C.

Code for receiving I have so far is: (I'm not on the computer with the actual code)

while ((rev = recv(sockfd, buf, size-1, 0)) > 0) {
    //Print result etc
}

Upvotes: 0

Views: 218

Answers (1)

meandbug
meandbug

Reputation: 202

The socket is limited to packets, it has no clue about what you are downloading.

If you are downloading a file from a web server using HTTP, you can however use the HTTP response header to get the desired information "Content-Length" See: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Thus read the first packet and interpret the first bytes of your received data as "HTTP response header" to know how much must be downloaded.

Upvotes: 1

Related Questions