Thread: send/recv binary files using sockets in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You could have a fixed length buffer (i.e. 512 or 1024 bytes). Then read the file (buffer length) bytes at a time and send it that way. On the other side, it doesn't really matter how big the buffer is. Just receive as much as you can (check the return of recv()), and write to the new file the number of bytes you received.

    And yeah, you find the file size and do what Salem said about sending the file length first (i.e. a standard long header).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One key you need to remember is that the receiver needs to know the length of the data before it starts receiving it via socket. Now how you implement the server and the data transfer protocol is arbitrary.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    What I did

    When I wrote my first file transfer socket program I did it like this.

    NOTE: I had to make the server interface with VisualBasic sockets for someone which made this a bit more complex.

    I first got the file size which is seen in previous posts
    Then I sent this.
    s:filename:size
    This told the other end I was sending a file with the name filename and the size of size.
    Once the client approved the transfer I would have a 512block buffer and send the data prefixed with a command like this.
    d:data
    d:data2
    d:data3
    etc...
    the d told the client it was data that was meant to be applied to the file.
    Just to make sure the client knew the server was finished at the end I would send EOF without the d:
    that way the client knew it was not to be applied to the file but that it had reached the end.

    I hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  3. Processing binary or plaintext files
    By Jags in forum C Programming
    Replies: 12
    Last Post: 08-04-2006, 02:35 PM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Binary files in C++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2001, 04:48 PM