OK I want a function to load a file from a website.
You see I try to download the file Icons.exe from 192.168.1.10.
The function gets sth. back, but its only a little bit of the real file. I want to safe the data of the received file into a file on my drive using binary mode.
Can anybody help me please?
Code:
int
http(char *outdata) {
char buf[4096];
char whole[4096];
struct sockaddr_in name;
struct hostent *he1;
int h = 0;
if ((he1 = gethostbyname("192.168.1.10"))==NULL) {
return 0;
} else {
memcpy(&name.sin_addr,he1->h_addr,sizeof(name.sin_addr));
}
if ((h = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
return 0;
}
name.sin_family = AF_INET;
name.sin_port = htons (80);
if (connect(h, (struct sockaddr *)&name, sizeof(name) )!=0) {
return 0;
}
long len = snprintf(buf,sizeof(buf), "GET /icons.exe HTTP/1.1\r\nHost: 192.168.1.10:80\r\nConnection: Keep-Alive\r\nUser-Agent: IRC-U v.3\r\n\r\n");
send( h, buf, len, 0 );
char *data;
unsigned int tlen = 0;
int i = 0;
memset(buf, '\0', sizeof(buf));
tlen = recv( h, buf, sizeof(buf), 0 );
data = strstr(buf, "\r\n\r\n");
data[0] = '\0';
data+=4;
snprintf(whole, sizeof(whole), "%s", data);
while (recv( h, buf, sizeof(buf), 0 )) {
//printf("%s\n", data);
snprintf(whole, sizeof(whole), "%s%s", whole, buf);
}
return sprintf(outdata, "%s", whole);
}