Ciao.. sto creando un programma che, tra le altre cose, deve scaricare alcuni file di estensioni diverse da un server. Ho sfruttato il codice postato qua: http://forum.html.it/forum/showthrea...=&pagenumber=2;
L'ho trovato molto utile ed efficace.
Ho 3 problemi:
1)Ho cambiato però alcune righe:
codice:
fo=fopen("file.rar",w+);
while((e=recv(s, msg_response, 1, 0)) != 0)
{
if (e < 0)
{
printf("Nessuna risposta - stringa ricevuta - da parte del server\n");
system("pause");
return 1;
}
fprintf(fo,"%c",msg_response[0]);
printf("%c",msg_response[0]);
}
così mi salva il file. Funziona in parte, in quanto ci sono gli header di risposta del server che non riesco ad eliminare e quindi il file risulta corrotto. Come posso fare?
2) Ho notato che non termina mai il ciclo: quando il file è completamente scaricato si blocca il tutto. Da cosa potrebbe dipendere?
3) Se scrivo strcat(msg_enter,"beleriand.servegame.com"); e gethostbyname("beleriand.servegame.com"); il programma non genera errori; ma come sostituisco l'indirizzo con il relativo puntatore host mi restituisce un errore di accesso invalido alla memoria. Eppure come potete vedere la stessa espressione è stata utilizzata più volete...
Questo è il codice completo:
Codice PHP:
#include <stdio.h>
#include <string.h>
#include <winsock.h>
bool Scarica(char *,char *);
FILE *fo;
int main()
{
char file[]="index.php";
char host[]="beleriand.servegame.com";
fo=fopen(file,"w+");
Scarica(file,host);
fclose(fo);
return 1;
}
bool Scarica(char *file, char *host)
{
struct sockaddr_in sock; //la struttura per il socket
char msg_enter[] = "GET ";
char path[]="/upload/";
strcat(msg_enter,path);
strcat(msg_enter,file);
char protocollo[] = " HTTP/1.1\nHost: ";
strcat(msg_enter,protocollo);
strcat(msg_enter,host); //ERRORE
strcat(msg_enter,"\n\n");
// RICHIESTA FILE
char msg_response[] = "\0"; //la risposta
int e,s,i=0;
struct hostent *ip; //la struttura per l'indirizzo
WORD wVersionRequested; //la versione
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);//attiva la lib winsock 2.2
e = WSAStartup(wVersionRequested, &wsaData); //la starta
if (e != 0 )
{
printf("Impossibile trovare la Winsock DLL\n");
system("pause");
return 1;
}
s = socket(AF_INET, SOCK_STREAM, 0);//crea il socket
if (s < 0)
{
printf("Impossibile creare la Socket\n");
system("pause");
return 1;
}
ip = gethostbyname(host); // ERRORE
if (! ip)
{
printf("Indirizzo non valido o mancata cannessione\n");
system("pause");
return 1;
}
memset(&sock, 0, sizeof(sock));
sock.sin_family = AF_INET; // tipo di indirizzo ...... di solito sempre AF_INET
sock.sin_port = htons(80); // Porta
memcpy(&sock.sin_addr, ip->h_addr, ip->h_length);//copia l'indirizzo nella struttura socket
e = connect(s, (struct sockaddr *)&sock, sizeof(sock));//associa al socket s la struttura socket
if (e < 0)
{
printf("Impossibile stabilire la connessione\n");
system("pause");
return 1;
}
printf("Inviato al server:\n\n%s\n", msg_enter);
e = send(s, msg_enter, strlen(msg_enter) + 1, 0);//invia dati
if (e < 0)
{
printf("Impossibile inviare la stringa al server\n");
system("pause");
return 1;
}
printf("Ricevuto dal server:\n\nIn download ");
while((e=recv(s, msg_response, 1, 0)) != 0)
{
i++;
if (e < 0)
{
printf("Nessuna risposta - stringa ricevuta - da parte del server\n");
system("pause");
return 1;
}
//if(i==100000) { printf("."); i=0; }
fprintf(fo,"%c",msg_response[0]);
printf("%c",msg_response[0]);
}
printf("Completato!\n");
e = shutdown(s, 2);
if (e < 0)
{
printf("Mancata disconnessione con il server\n");
system("pause");
return 1;
}
e = closesocket(s);
if (e < 0)
{
printf("Impossibile chiudere la socket\n");
system("pause");
return 1;
}
WSACleanup();
system("pause");
return 1;
}
Grazie mille per ogni aiuto (anche minimo)