Binary data

This is a discussion on Binary data within the C++ Programming forums, part of the General Programming Boards category; Code: while(fileones.get(buffer, 1024)) { send(mysocket, buffer, 1024, 0); ZeroMemory(buffer, 1024); // reset buffer } Before asking where I declared mysocket, ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    Binary data

    Code:
      while(fileones.get(buffer, 1024))
       {
             send(mysocket, buffer, 1024, 0);
             ZeroMemory(buffer, 1024); // reset buffer
       }
    Before asking where I declared mysocket, buffer, etc. It's all there, this is just the only part giving me trouble. It successfully sends 2 kbs(recv()'d and written to file on the other end), but that's it. The file is around 3mbs. If anyone could give me an idea as to why it only reads 2 kbs, that would be great. Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Oh I think it reads all 3MB, but you're assuming that send() always sends the whole buffer the first time you call it.
    You need to check that send() actually sent the data (check the return result).

    Also, your ZeroMemory() seems kinda pointless.

    Also, what if your file isn't a multiple of 1024 bytes in length? You send an arbitrary amount of non-file data across the link.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Quote Originally Posted by Salem
    You need to check that send() actually sent the data (check the return result).
    I checked the return result, and both times it's 1024. It only runs through the while loop twice.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    get(..) seems like a wierd function to use with a buffer like that on a binary file. It appends a null-terminator to the end.

    Edit: I don't see why you're just reading small chunks of the file and sending them.
    Last edited by Tonto; 04-16-2006 at 10:12 AM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    I'm doing it because I don't want to send the entire file in one send, since it's crashed the program before, and would not allow me to see the progress of the send.

    Well, thanks anyway.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you are using Windows, indicated by the use of ZeroMemory macro, you can use WSASend and the lpCompletionRoutine.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Are you sure you opened the file in binary mode?

    Without the send(), can you read the file to the end anyway?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    I got it to work by modifying the while loop, and using read() instead. Thanks for the replies everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  4. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21