Winsock - sending a picture failed. - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: Winsock - sending a picture failed.

  1. #16
    Join Date
    Aug 2014
    Posts
    9

    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);

  2. #17
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Winsock - sending a picture failed.

    Quote Originally Posted by Patry20 View Post
    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

  3. #18
    Join Date
    Aug 2014
    Posts
    9

    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.

  4. #19
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    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

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,370

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #21
    Join Date
    Aug 2014
    Posts
    9

    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.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center