下载器无法获取所有数据

时间:2011-01-20 20:33:29

标签: c++ visual-studio-2010 http

所以我试图根据http协议制作一个下载器。它适用于小的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;
}

2 个答案:

答案 0 :(得分:0)

您的代码似乎假设recv始终返回最大数据量(即等待出现n个字节)。如果当前有少量数据可用,recv可能会提前返回。

相反,你应该继续,直到断开连接(当recv返回0时,或者出现WSAECONNRESET这样的错误)或预期的字节数(一旦完全接收到标题就应该有)

答案 1 :(得分:0)

如果还没有足够的数据,

recv可能无法填充整个缓冲区。您应该通过HTTP protocol中指定的方法之一检测邮件的结尾。