C/C++使用berkeleys / bsd套接字从http下载文件

我想知道如何使用c / c中的套接字下载.exe文件.

我正在使用cygwin和g

我试过berkeleys套接字,但我似乎无法下载文件.
我在下面嵌入了我的代码:

    #include <fstream>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netdb.h>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <map>

    using namespace std;

    int main(){



    int sock_descriptor; // integer number to access socket
struct sockaddr_in serv_addr; // uses predefined sockaddr_in struct
struct hostent *server; // from netdb.h to determine host name out of ip address
char recvBuff[1024];  // Receiving buffer 
char hostname[] = "localhost";

char req[] = "GET /fjernsupport.exe HTTP/1.1"
                "Host: localhost\n"
                "Connection: keep-alive\n"
                "Cache-Control: no-cache\n"
                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"
                "Pragma: no-cache\n"
                "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\n"
                "Accept-Encoding: gzip,deflate,sdch\n"
                "Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,da;q=0.4\n"
                "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\n\n";

sock_descriptor = socket(AF_INET, SOCK_STREAM, 0); // SOCK_STREAM = TCP, AF_INET = DOMAIN
if(sock_descriptor < 0){
    std::cout << "Failed creating socket\n" << std::endl;
}
bzero((char *)&serv_addr, sizeof(serv_addr));
server = gethostbyname(hostname);
if(server==NULL){
    std::cout << "Failed to find server name" << std::endl;
    return 0;
}
serv_addr.sin_family = AF_INET;
memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *)(server->h_addr), server->h_length);

serv_addr.sin_port = htons(80);  // Ensures integer interpretion is correct
if(connect(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
    std::cout << "Failed to connect to server" << std::endl;
}else{
    std::cout << "Succesfully connected" << std::endl;
}
cout << "SEND: " << req << endl;
write(sock_descriptor, req, sizeof(req));
bool isFile = false;
ofstream outFile;
outFile.open("outfile.exe", ios::out | ios::binary);
while(true){
    memset(recvBuff, 0, 1024);
    if(read(sock_descriptor, recvBuff, sizeof(recvBuff)-1) > 0){
        //cout << "RECV: " << recvBuff << endl;


                outFile << recvBuff;

                //cout << "WRITING THIS: " << recvBuff << endl;



        //cout << "Newline? " << endl;
    }else{
        outFile.close();
        cout << "Returning here";
        return 0;
    }


}
outFile.close();
cout << "finished";
return 0;

我的代码完美连接并获得正确的响应头,解决了数据问题.它不会将数据移动到文件中.
主要担心的是,我不确定这是否是我收到的错误数据,或者是否是创建outfile.exe的错误方法.

我知道上面的代码也是将响应http头文件写入文件.但即便如此,也不要让文件足够大.
outfile.exe只有大约763kbs,而fjernsupport.exe大约是3mb

最佳答案 您正尝试将二进制数据保存为NUL终止字符串:

            outFile << recvBuff;

相反,您应该保存read()调用的返回值(因为它指示保存到缓冲区的字节数),然后将那么多字节写入outFile.

if((bytes = read(sock_descriptor, recvBuff, sizeof(recvBuff))) > 0){
            //...
            outFile.write(recvBuff, bytes);
点赞