Hello all,

I am writing a function to download a HTML page from another server.

I am wiring this code using MFC's Casyncsocket

Basically, this class runs the callback function (onReceive) whenever it detected some new that can be received.

Originally, I had something like this:

void CLASSNAME::OnReceive(int nErrorCode) 
{
	char buf[1024];
	CString received = "";
	while((size = this->Receive(buf, 1023)) > 0)
	{
		buf[1023] = '\0';
		received += buf;
	}
}

However, since the Casyncsocket calls this function every time there is something new, these procedures will be called more than once.

So I came up with this:

private CString received = ""; /* Global private variable */
void CLASSNAME::OnReceive(int nErrorCode) 
{
	char buf[1024];
	if((size = this->Receive(buf, 1023)) > 0)
	{
		buf[1023] = '\0';
		received += buf;
	}
}

However, with this code, I cannot tell when the HTML is fully downloaded.

Can anyone help me on this?

Thanks.

Maybe this is a stupid question but what will be the value of size when the entire page is read ? Wont it be 0.
Cant you use that check to determine when the page is read ?

Or am I missing some thing ?

Thanks for your reply.
My knowledge of winsock may be incorrect
since I learned everything all by myself (through the internet and experiences)

I came up with this:
When the content size received is zero, it means that at that particular time, there are no packets for the program to receive (due to internet lag or something)
So, the packets can be sent like this:

500kb transferred
234kb transferred
0 transferred
80 transferrer

.... etc

Your code is notified (that is, OnReceive gets called) when it is safe to read. Then, Receive returning 0 means that the peer closed the connection.
It may or may not indicate that the download completed. To make such conclusion you must look for the particular headers, primarily Content-Length, Connection, Transfer-Encoding, inspect them, and decide accordingly.

Thanks for the information.

However, my current primary problem is that I don't know exactly when the "download" is complete. In other words, I need to a way to determine whether "this OnRecieve" call is the "last OnRecieve" call because after I downloaded a page, I want it to call another function.

Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.