Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've written some code to perform a GET request and it's working to an extent, except it won't receive large amounts of data properly. It's strange because how I output the data appears to determine how much was received.

I own the server this is performing the GET request to, and I've set it to output the numbers 0 to 9999 with a PHP script.

Working:
C++
while (resp_len == BUFFER_SIZE)
{
	resp_len = recv(sock, (char*)&buffer, BUFFER_SIZE, 0);
		
	if (resp_len > 0)
	{
		string data = string(buffer).substr(0, resp_len);
		printf(data.c_str());
	}
}

Not working:
C++
while (resp_len == BUFFER_SIZE)
{
	resp_len = recv(sock, (char*)&buffer, BUFFER_SIZE, 0);
		
	if (resp_len > 0)
	{
		string data = string(buffer).substr(0, resp_len);
		response += data;
	}
}

printf(response.c_str());

With the working example, it returns 49,062 bytes consistently. With the failing one, only 8,372, and this is also consistent.

I need the entire response in a std::string to return from this function. Could you help me figure this out please?

What I have tried:

Multiple methods to perform GET requests, none of which work apart from this, and this is temperamental depending on how the data is outputted.
Posted
Updated 1-Jul-18 11:53am
v3
Comments
Rick York 1-Jul-18 16:18pm    
What happens if you get a response of less than BUFFER_SIZE? Shouldn't it continue while it gets any data ie., resp_len > 0?

Your not working code is too fast (the code executes faster than the time required to receive BUFFER_SIZE bytes). The first code is only working because the printf() call requires some time for execution.

The recv() function returns the number of bytes actually available, up to the specified buffer size. If you call it again too fast, the return value will be less than BUFFER_SIZE.

You would have to change your code to receive data until a complete data set has been received. This can be done by an end marker (e.g. a null byte with string data), or by sending the number of the bytes in the data set at first. The latter is typically done by sending a defined header before the payload. The definition of such headers and the payload data format is called protocol.
 
Share this answer
 
Fixed - when playing around with the code, I saw that some data at the end was repeating, and I realised it was because it was trying to fill up the buffer for the last chunk of data even though the remaining data wasn't large enough, so it re-read from the start of that chunk to fill up the buffer, and duplicated some data.

This version reads the data in chunks but only tries to assign what's available, not always BUFFER_SIZE.

C++
int nDataLength;

while ((nDataLength = recv(sock, (char*)&buffer, BUFFER_SIZE, 0)) > 0)
{
	for(int x = 0; x < nDataLength; x++)
	{
		response += buffer[x];
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900