user1265125
user1265125

Reputation: 2656

Receiving data over HTTP - Inconsistent size

memset(buf, 0, sizeof(buf));
    int htmlstart = 0;
    char * htmlcontent;
    char *mainpage = (char *) malloc(MAXBUF);
    while((tmpres = recv(sock, buf, MAXBUF, 0)) > 0)
    {
        if(htmlstart == 0) //on first run, ignore headers
        {
            htmlcontent = strstr(buf, "\r\n\r\n");
            if(htmlcontent != NULL)
            {
                htmlstart = 1;
                htmlcontent += 4;
            }
        }
        else
        {
            htmlcontent = buf;
        }
        if(htmlstart)
        {
            mainpage = (char *) realloc( mainpage, (strlen(mainpage) + strlen(htmlcontent) + 1) );
            // printf("%s",htmlcontent);
            strcat(mainpage,htmlcontent);
        }
        memset(buf, 0, tmpres);
    }
    if(tmpres < 0)
    {
        perror("Error receiving data");
    }

    printf("%d",(int)strlen(mainpage));

I wrote a simple program to receive an data over HTTP after establishing connection with the server. But I'm having a strange problem if I try to receive a large object like an image. Each time I run the program the last print statement which prints the total size of the HTTP data (without headers), comes out to be different. So what I'm receiving is a corrupt image/part of the image.

Any thoughts on why this might be happening?

EDIT: If I check the cumulative size of htmlcontent before concatenating it with mainpage, even then the size is the same as mainpage after the whole receipt. So the problem can't be in strcat or any other string function.

Upvotes: 0

Views: 125

Answers (2)

mfro
mfro

Reputation: 3335

if you strcat() something to an undefined buffer (mainpage), strlen() will return arbitrary results.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182769

You forgot to implement the HTTP protocol! The HTTP protocol specifies how you know when you have the entire object, for example, using things like a Content-Length header. You have to implement the protocol. The recv function just knows it's reading from a stream of bytes.

Also, your use of strlen and strcat is incorrect. The recv function tells you how many bytes it received. The strlen function is only for strings, not arbitrary chunks of data you haven't parsed or processed yet.

Upvotes: 1

Related Questions