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
  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
    Hanover Germany
    Posts
    20,396

    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
    Hanover Germany
    Posts
    20,396

    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
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  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
  •  





Click Here to Expand Forum to Full Width

Featured