transfer file using recv() and fwrite()

Discussion in 'C Programming' started by michelle, Jun 26, 2003.

  1. michelle

    michelle Guest

    Hi, I am trying to transfer a binary file from a client program to the
    server, so on the server side I am using recv() to receive the binary
    data to the buffer and then write to a file. The follwoing is my
    code. Somehow, the newly transfered file is different from the
    original file. I don't know if the error is at recv() or fwrite().
    For receving, I already check that the bytes being sent equals to the
    btyes being received. So anyone knows what's wrong with my code?
    Thanks.

    char buf[length]; (length is how many bytes of data)
    fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
    data)
    //this part is right, coz it receive right amount of bytes.
    while (bytes != length) {
    bytes += recv(clisock, buf, length-bytes, 0);

    }

    num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
    bytes)
    fclose(fid);
     
    michelle, Jun 26, 2003
    #1
    1. Advertising

  2. In article <>,
    michelle <> wrote:
    >Hi, I am trying to transfer a binary file from a client program to the
    >server, so on the server side I am using recv() to receive the binary
    >data to the buffer and then write to a file. The follwoing is my
    >code. Somehow, the newly transfered file is different from the
    >original file. I don't know if the error is at recv() or fwrite().
    >For receving, I already check that the bytes being sent equals to the
    >btyes being received. So anyone knows what's wrong with my code?
    >Thanks.
    >
    >char buf[length]; (length is how many bytes of data)
    > fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
    >data)
    >//this part is right, coz it receive right amount of bytes.
    > while (bytes != length) {
    > bytes += recv(clisock, buf, length-bytes, 0);


    Depending on the size of the data and the size of the messages, it could
    happen that you receive multiple messages. The recv() function returns
    the number of bytes received so that adds up nicely to the amount of bytes
    you expect, but you store all messages at the start of the buffer
    (overwriting older messages with newer ones.) You could use the number of
    bytes received as an offset in the buffer for new messages.

    >
    > }
    >
    > num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
    >bytes)
    > fclose(fid);



    Regards,
    Rob van der Leek

    --
    Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
    Ricardishof 73-A | PGP key: see my webpage:
    2614 JE Delft, The Netherlands | http://www.ricardis.tudelft.nl/~rob
    +31 (0)6 155 244 60
     
    Rob van der Leek, Jun 26, 2003
    #2
    1. Advertising

  3. michelle

    michelle Guest

    Hi, Rob. Your solution is right, that solves the problem in seconds
    verse I was trying to debug whole day yesterday. Thank you very
    much!!!!

    Michelle

    (Rob van der Leek) wrote in message news:<bdeeca$2qu$>...
    > In article <>,
    > michelle <> wrote:
    > >Hi, I am trying to transfer a binary file from a client program to the
    > >server, so on the server side I am using recv() to receive the binary
    > >data to the buffer and then write to a file. The follwoing is my
    > >code. Somehow, the newly transfered file is different from the
    > >original file. I don't know if the error is at recv() or fwrite().
    > >For receving, I already check that the bytes being sent equals to the
    > >btyes being received. So anyone knows what's wrong with my code?
    > >Thanks.
    > >
    > >char buf[length]; (length is how many bytes of data)
    > > fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
    > >data)
    > >//this part is right, coz it receive right amount of bytes.
    > > while (bytes != length) {
    > > bytes += recv(clisock, buf, length-bytes, 0);

    >
    > Depending on the size of the data and the size of the messages, it could
    > happen that you receive multiple messages. The recv() function returns
    > the number of bytes received so that adds up nicely to the amount of bytes
    > you expect, but you store all messages at the start of the buffer
    > (overwriting older messages with newer ones.) You could use the number of
    > bytes received as an offset in the buffer for new messages.
    >
    > >
    > > }
    > >
    > > num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
    > >bytes)
    > > fclose(fid);

    >
    >
    > Regards,
    > Rob van der Leek
     
    michelle, Jun 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    964
    Dave Thompson
    Jul 21, 2003
  2. Replies:
    6
    Views:
    749
    Keith Thompson
    Feb 9, 2005
  3. Vivienne
    Replies:
    3
    Views:
    463
    Vivienne
    Jan 15, 2007
  4. deepak nayak
    Replies:
    3
    Views:
    432
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 25, 2007
  5. Jeff
    Replies:
    16
    Views:
    1,465
    Kenny McCormack
    Jun 9, 2008
Loading...

Share This Page