Download is not complete ?!?!

hi all,
can somebody tell what is wrong with my code ... when i compile it into an exe then execute it ... it connects successfully but not download the whole file it downloads about 25% of the file ... don't know why ... please help ...

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include <winsock2.h>
#include <stdio.h>
#include "digiwebdlservres.h"
char filedata[8192];
FILE *fp;
int filedatas = 0;
char* removef2(char* x);
char url1[80];

int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow)
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) < 0) {
return 0;
}
struct sockaddr_in dli;
SOCKET dl = socket(AF_INET, SOCK_STREAM, 0);
if (dl == INVALID_SOCKET) {
return 0;
}
dli.sin_family = AF_INET;
dli.sin_addr.s_addr = inet_addr(IP);
dli.sin_port = htons(80);
if (connect(dl, (struct sockaddr*) &dli, sizeof(dli)) < 0) {
return 0;
}
char filename1[1024], host1[1024];
sprintf(filename1, "GET %s HTTP/1.1\n", FILENAME);
sprintf(host1, "Host: %s\n", URL);
if (send(dl, filename1, strlen(filename1), 0) < 0) return 0;
if (send(dl, "Accept: */*\n", 12, 0) < 0) return 0;
if (send(dl, "Accept-Language: en-us\n", 23, 0) < 0) return 0;
if (send(dl, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)\n", 59, 0) < 0) return 0;
if (send(dl, host1, strlen(host1), 0) < 0) return 0;
if (send(dl, "Connection: Close\n", 18, 0) < 0) return 0;
if (send(dl, "\n", strlen("\n"), 0) < 0) return 0;
fp = fopen(dlname, "wb");
while (recv(dl, filedata, 512, 0)) {
removef2(filedata);
}
fclose(fp);
closesocket(dl);
WSACleanup();
return 0;
}
char* removef2(char* x) {
char* ru;
if (ru = strtok(x,"\n")) {
if (filedatas == 1) {
fwrite(ru, 1, strlen(ru), fp);
fwrite("\n", 1, 1, fp);
}
if (strlen(ru) <= 1) {
filedatas = 1;
}
}
while (ru = strtok(NULL,"\n")) {
if (filedatas == 1) {
fwrite(ru, 1, strlen(ru), fp);
fwrite("\n", 1, 1, fp);
}
if (strlen(ru) <= 1) {
filedatas = 1;
}
}
return (char*)1;
}

; in digiwebdlservres.h ...
#define IDD_MAINDIALOG 100
#define IP "64.233.161.104"
#define URL "www.google.com"
#define dlname "c:\\windows\\system32\\cfqwiz32.gif"
#define FILENAME "/intl/en/images/logo.gif"

Regards DiGitalX
[2702 byte] By [DiGitalX] at [2007-11-19 12:18:51]
# 1 Re: Download is not complete ?!?!
There are several error in your code.

* You don't check how many bytes that is received by recv.
* You treat the returned data as a zero-ended-string. It is not, it is binary information and it probably contains a lot of zeros all over the place...
* You tokenize the data using '\n'... and after the last token you add an extra '\n'.

IMO you should read data into some buffer (until you've got a full header), then you should parse that header, and atleast read the Content-Length field. Then read the rest of the data (the amount specified by Content-Length) and store that data... don't use string operations on data that is not strings.

- petter
wildfrog at 2007-11-9 13:50:17 >
# 2 Re: Download is not complete ?!?!
I agree with wildfrog. Your biggest error is the assumption that the various "string" functions will work. They won't, because the data being returned from the web server is binary data which will often contain legitimate NULL bytes, whereas the "string" functions think that a NULL bytes delimits the end of a string.

Also as mentioned by wildfrog, your fwrite functions should be writing the amount of bytes returned by the socket recv() functions, and not based on strlen() functions, for the reasons mentioned above.

Moreover, your program fails to accommodate the fact that the web server might be sending data that is "chunked". See the rfc for HTTP/1.1, which allows chunked data. You probably should ask for data using HTTP/1.0

Mike
MikeAThon at 2007-11-9 13:51:18 >
# 3 Re: Download is not complete ?!?!
guys it works great THANKS but i need a small help plz

i need to get rid of the info that the server send to me first like:

HTTP/1.0 200 OK
.............
Age: 7216

i need only to download the file itself plz
(i will appreciate it if you bring a simple code to show me how)
DiGitalX at 2007-11-9 13:52:16 >
# 4 Re: Download is not complete ?!?!
The dirty way?

The HTTP header ends with a double CrLf, that is CrLfCrLf (or 0x0d, 0x0a, 0x0d, 0x0a)... so as you read the data, after reading the first double CrLf, then the body (or image data) begins.

- petter
wildfrog at 2007-11-9 13:53:22 >
# 5 Re: Download is not complete ?!?!
It Works Guys Thanks For Your Assistance :*
DiGitalX at 2007-11-9 13:54:22 >