gpt4 book ai didi

c++ - 下载器没有获取所有数据

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:55 24 4
gpt4 key购买 nike

所以我想做一个基于http协议(protocol)的下载器。它适用于小的 txt 文件,但如果我尝试下载更大的文件,比如图片,它不会获取所有数据。

void accel::getfile(int from, int to){
//send GET message
string msg = msg_get(fpath, true, from, to);
int back = send(socketC, (char*)&msg[0], msg.size(), 0);

//recv response
int buffsize = 512; //buffer size. should not be less than 512
string buff(buffsize, 0);//, resp; //buffer, response message
fstream output(fname, ios::out, ios::trunc); //file
bool found = false;
int dld = 0;

do {
//recv msg to buffer
back = recv(socketC, (char*)&buff[0], buffsize, 0);
dld += back;
//buff.insert(back, "\0");
//cout << buff.c_str();
//is the writing of the body started?
if (found){
output.write(buff.c_str(), back);
cout << ".";
}
//if not find where the body starts
else {
int point = buff.find("\r\n\r\n");
if (point != -1){
char *p = (char*)&buff[point+4];
output.write(p, back-point-4);
cout << ".";
found = true;
}
}
} while (back == buffsize);

output.close();

cout << "\nComplete\n";
cout << dld << "bytes downloaded\n";
}

要求:

string msg_head(string fpath, bool keep_alive){
string msg;
//
msg = "HEAD ";
msg += fpath;
msg += " HTTP/1.0\r\n";

if (keep_alive)
msg += "Connection: keep-alive\r\n\r\n";
else
msg += "Connection: close\r\n\r\n";

return msg;
}

string msg_get(string fpath, bool keep_alive, int from, int to){
string msg;
char number[10];

msg = "GET ";
msg += fpath;
msg += " HTTP/1.0\r\n";

msg += "Range: bytes=";
sprintf(number, "%d", from);
msg += number;
msg += "-";
sprintf(number, "%d", to);
msg += number;
msg += "\r\n";

if (keep_alive)
msg += "Connection: keep-alive\r\n\r\n";
else
msg += "Connection: close\r\n\r\n";

return msg;
}

最佳答案

如果还没有足够的可用数据,

recv 可能不会填满整个缓冲区。您应该通过 HTTP protocol 中指定的方法之一检测消息的结尾.

关于c++ - 下载器没有获取所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4752264/

24 4 0
文章推荐: java - 在 Java 中使用 DynamoDB 的 PutItem ConditionExpression(避免替换属性)
文章推荐: jquery - animate.css 用于着陆页的欢迎效果
文章推荐: java - 使用 Java 将图像插入 MS Access 数据库的 SQL
文章推荐: css -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com