hola

estoy tratando de programar una aplicacion para descargar archivos, del codigo que les pondre, el segundo for( ; ; ), se detiene y no termina de descargar los datos y los archivos quedan incompletos, que podria suceder?, lo estoy haciendo en linux

Código: Seleccionar todo

char *hst, *file, *dire;
char  datos[512];
char buffer[1024];
hst = argv[1];
file = argv[2];
dire = argv[3];
 
int sock;
int i = 0;
int bytes = 0;
int dbytes;
 
memset(buffer, 0, 1024);
 
struct sockaddr_in cox;
struct hostent *ht;
 
sprintf(datos, "GET %s HTTP/1.1\nHost: %s\nUser-Agent: Mozilla/4.0\n\n", file, hst);
 
sock = socket(AF_INET, SOCK_STREAM, 0);
 
ht = gethostbyname(hst);
 
cox.sin_family = AF_INET;
cox.sin_port = htons(80);
cox.sin_addr = *((struct in_addr*)ht->h_addr);
memset(&cox.sin_zero, 0, 8);
 
connect(sock, (struct sockaddr*)&cox, sizeof(struct sockaddr));
 
FILE *fp = fopen(dire, "wb");
 
send(sock, datos, strlen(datos), 0);
 
for(;;){
 
recv(sock, buffer + i, 1, 0);
i++;
if(strncmp(buffer+i-4, "\r\n\r\n", 4) == 0){
break;
}
}
for(;;){
 
memset(buffer,0,1024);
bytes = recv(sock, buffer, 1024, 0);
dbytes += bytes;
printf("%d KB\n", dbytes/1024);
fwrite(buffer, sizeof(char), bytes ,fp);
if(bytes <= 0){
break;
}
}
printf("Bytes: %d\n", dbytes);
fclose(fp);
close(sock);
}
}
todavia le faltan cosas, pero ahorita lo hice para que descargue de esta manera

Código: Seleccionar todo

./downfiles www.oocities.org /dreamlfpg315/dll/ensamblador.txt ensamblador.txt
espero puedan ayudarme a corregirlo

salu2
Responder

Volver a “C/C++”