C ++获取请求 - 异常问题 [英] C++ get request - unusual issue

查看:77
本文介绍了C ++获取请求 - 异常问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我写了一些代码来执行GET请求并且它在一定程度上起作用,除非它不能正确接收大量数据。这很奇怪,因为我输出数据的方式似乎决定了收到了多少。



我拥有执行GET请求的服务器,我设置了它使用PHP脚本输出数字0到9999.



工作:

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:

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:

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());



使用工作示例,它返回49,062字节一致。失败的只有8,372,这也是一致的。



我需要std :: string中的整个响应才能从这个函数返回。你能帮帮我解决这个问题吗?



我尝试了什么:



执行GET请求的多种方法,其中没有一种方法可以解决这个问题,根据数据输出方式的不同,这是不合时宜的。


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.

推荐答案

你不工作代码太快(代码执行速度快于接收 BUFFER_SIZE 字节所需的时间)。第一个代码只能工作,因为 printf()调用需要一些时间来执行。



recv()函数返回实际可用的字节数,最多为指定的缓冲区大小。如果再次调用它太快,返回值将小于 BUFFER_SIZE



您必须更改代码才能接收数据,直到收到完整的数据集。这可以通过结束标记(例如,具有字符串数据的空字节)或通过首先发送数据集中的字节数来完成。后者通常通过在有效载荷之前发送定义的报头来完成。这些头和有效载荷数据格式的定义称为协议
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.


固定 - 当玩代码时,我看到最后的一些数据是重复,我意识到这是因为它试图填充最后一块数据的缓冲区,即使剩余的数据不够大,所以它从该块的开头重新读取以填充缓冲区,并复制了一些数据。



此版本以块的形式读取数据,但只尝试分配可用的数据,而不是总是BUFFER_SIZE。



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.

int nDataLength;

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


这篇关于C ++获取请求 - 异常问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆