-
August 13th, 2014, 10:54 AM
#16
Re: Winsock - sending a picture failed.
Code:
FILE *PlikSave; char *bufor; char FileSize[6] = { 0 }; int BytesReceived, size, BytesReceivedTemp=0;
BytesReceived= recv(MojSocket, FileSize, sizeof(FileSize), NULL);
size= atoi(FileSize);
bufor = (char*)malloc(sizeof(char)* size);
while(BytesReceivedTemp!=270398)
{
BytesReceived=recv(MojSocket, bufor, SizeofFile, NULL);
BytesReceivedTemp=BytesReceivedTemp + BytesReceived;
}
cout<<BytesReceivedTemp; //it prints 270398, correct! :)
PlikSave = fopen("D:\\C++\\Visual C\\WinSock\\Klient\\Debug\\Download2.ico", "wb");
fwrite(bufor, 1, size, PlikSave);
fclose(PlikSave);
free(bufor);
-
August 13th, 2014, 11:10 AM
#17
Re: Winsock - sending a picture failed.
Originally Posted by Patry20
Code:
...
bufor = (char*)malloc(sizeof(char)* size);
while(BytesReceivedTemp!=270398)
{
BytesReceived=recv(MojSocket, bufor, SizeofFile, NULL);
BytesReceivedTemp=BytesReceivedTemp + BytesReceived;
}
you are passing the same buffer bufor to every call of recv! So you overwrite the data you receied before!
Victor Nijegorodov
-
August 13th, 2014, 11:25 AM
#18
Re: Winsock - sending a picture failed.
Yes, that's why I asked:
"So, I think that maybe is something wrong with the "bufor" variable? Maybe it is being overwritten each time the loop restarts?"
I tried strcat:
Code:
[...]
char *bufor2;
bufor2 = (char*)malloc(sizeof(char)* size);
memset(bufor2, 0, 270398);
while(BytesReceivedTemp!=270398)
{
BytesReceived=recv(MojSocket, bufor, SizeofFile, NULL);
strcat(bufor2,bufor);
BytesReceivedTemp=BytesReceivedTemp + BytesReceived;
memset(bufor, 0, 270398) //Memseting because program was triggering a breakpoint without that.
}
fwrite(bufor2, 1, size, PlikSave);
Still cannot open the image. I suppose there is a problem with appending bufor to bufor2.
-
August 13th, 2014, 11:53 AM
#19
Re: Winsock - sending a picture failed.
Did you test a file transfer with some other file types and with smaller size?
Did you try to compare the original file with the transferred one?
Victor Nijegorodov
-
August 13th, 2014, 12:01 PM
#20
Re: Winsock - sending a picture failed.
Why are you trying to assemble the whole buffer before you write to file? Why not first open the file and then append each chunk read to the file?
You can't use strcat as the buffer may contain a zero.
if you want to assemble the whole buffer first before writing to file, allocate memory for the whole buffer first then pass to recv the address of the part of the buffer into which you want the next chunk read. ie 0 for first pass, BytesReceived for second pass etc etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 13th, 2014, 03:32 PM
#21
Re: Winsock - sending a picture failed.
Why not first open the file and then append each chunk read to the file?
Yes, that did the trick! Finally got it working!
Thank you guys for your time, you've been a great help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|