M@RIX

Hi. this is my first (second actually after posting it in the wrong forum) post of this forum and I hope to get a response Wink
my problem is this:
I'm trying to write a program that GETs a file from the HTTP protocol using winsock 2. sending the request is not a problem for me. my problem is receiving binary responses. I can GET a text page BTW.
this is the portion of the code that does the receiving:

Code Snippet

FILE *file;
fopen_s(&file , "out.txt" , "wb");
char tempbuf[128];
// Receive until the peer closes the connection
while(true) {
int retval;
retval = recv(ConnectSocket , tempbuf , sizeof(tempbuf)-1 , 0);
if (retval == 0) {
break; // Connection has been closed
}
else {
tempbuf[retval] = 0;
fwrite(tempbuf , 1 , strlen(tempbuf) , file);
}
}
fclose(file);


what is that that I'm doing wrong
I will provide any extra information neccessery.
thanx.


Re: Visual C++ General how to GET a binary file using winsock 2

Sdi

If you're receiving binary data, why are you fwrite()ing 'strlen(tempbuf)' Is the data in the buffer after the recv() call If not, the problem is in the recv(). If the data is there, the problem is in writing it out or in looking at the binary-file-with-.txt-extension.



Re: Visual C++ General how to GET a binary file using winsock 2

M@RIX

Q: If you're receiving binary data, why are you fwrite()ing 'strlen(tempbuf)'
A: strlen(tempbuf) is the length of the buffer to be written to the file, not the buffer itself.

Q: Is the data in the buffer after the recv() call
A: yes. so there isn't a problem with recv().

Q: If the data is there, the problem is in writing it out or in looking at the binary-file-with-.txt-extension.
A: no I have checked this with hex editors. an example:
actual file (google logo gif):47 49 46 38 39 61 14 01 6E 00 F7 00 00 F7 F7 F7
recv()ed file:47 49 46 38 39 61 14 01 6E 94 C6 18 FF 75 63 BD




Re: Visual C++ General how to GET a binary file using winsock 2

Boof2

How are you getting info for the recv()ed file If it is the data written then it is because of the strlen. It uses 0x00 to mean end of string. In the comparisons you'll notice once you get to the 0x00 in the source the resultant file is wrong.

Don't use C string functions to handle binary data like that. What is the return value of recv()





Re: Visual C++ General how to GET a binary file using winsock 2

superdos

The first time you recv(), you must have received 48byte data, and you truncated it to 8byte(0x47 - 0x6e) by using strlen().

The second time you recv(), you must have received 128byte data, and you truncated it to 3byte(0x94 - 0x18).

And I guess the seventeenth byte you have received is 0x96.

My suggesting is, use return value of recv() (not strlen()!) to determine your data length.






Re: Visual C++ General how to GET a binary file using winsock 2

Viorel.

I would suggest the following sequence:

Code Snippet

while(true)

{

int retval = recv(ConnectSocket, tempbuf, sizeof(tempbuf), 0);

if( retval <= 0)

{

break; // Connection has been closed, or error

}

fwrite(tempbuf, retval, 1, file);

}

I hope it works.





Re: Visual C++ General how to GET a binary file using winsock 2

M@RIX

thanx all.
It worked like magic. I didn't know it was this east to fix.




Re: Visual C++ General how to GET a binary file using winsock 2

Sdi

Sorry for being overly subtle. strlen() is for NUL-terminated text strings. That isn't what you have; you have binary data, which may include binary zero bytes. And you already know the length--you got it from recv().