im currently trying to write a c++ code on a mac to download a larger file from a website (~1GB). I think i have an error somewhere where i convert the socket buffer to a string, cause my resulting file (movie file) has some small blocks of nul chars spread through the whole file, and i need to somehow delete them from the string optained by the socket buffer.
This is the part which handles the http connection and the part which saves the date to the file. Some Parts may not be in this example like error handling or the complete socket building.
//I have error handling in here but stripped out from this example
char buffer[512];
portno = atoi("8080");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server = gethostbyname(address);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
bzero(buffer,512);
header.copy(buffer,512);
n = write(sockfd,buffer,strlen(buffer));
std::string str_buff;
while((n = read(sockfd,buffer,511)) > 0){
std::string temp(buffer,511);
//Is this the error^^^^^^^^^?
write_chunk_to_file(temp);
//cut
void write_chunk_to_file(std::string chunk){
write.open(path+fname, std::ios::out | std::ios::app);
write << remove_header(chunk);
write.close();
//cut
std::string remove_header(std::string chunk){
if(chunk.find("")){
chunk = chunk.substr(chunk.find(""),chunk.length());
}
return chunk;
}
When i compare the file my code downloads with the file wget downloads, i have some smaller blocks only consisting of NUL chars in my file and some extra bytes seem to exist also in my file.
Does anyone has a clue?
Aucun commentaire:
Enregistrer un commentaire