Detect end-of-file of Binary file

  • Follow


Hi All,

I want to transfer the binary file over the network using tcp socket.
In this how could I detect the end-of-file at the receiving end for
closing the file ?,  as the binary file could have the end-of-file bit
pattern in between. the program is in c++ on linux.

Thanks in advance,
torpedo

0
Reply torpedo (6) 7/1/2005 8:41:37 AM

torpedo@bluebottle.com wrote:
> Hi All,
> 
> I want to transfer the binary file over the network using tcp socket.
> In this how could I detect the end-of-file at the receiving end for
> closing the file ?,  as the binary file could have the end-of-file bit
> pattern in between. the program is in c++ on linux.
> 
> Thanks in advance,
> torpedo
> 

Hi you ...

first of all ... there is no thing like an end of file bit pattern ...
    this is old MS-DOS specific stuff (Ctrl-Z) ... forget about that
    when talking about binary files and networkconnections.

What you want is to transfer a file, and you dont want to change the
    content on the transfer (no LF CRLF recoding that is often done
    for textfile transfers between unix and dos/windows systems)

Your options are:

1) Use a dedicated socket connection for the transfer (like ftp does)
    and close the connection on the sending side when you've written all
    filedata into the socket.
    The receiving side will be able to read all data. The final
    returncode of read() will be 0.

2) Use the socketconnection you already have (perhaps you have one) ...
    send the length of the file first ... coded in a form the
    receiverside can decode (keep in mind that the receiver might
    live on a system with different integer representation) ...
    then send all data of the file.
    The receiving side reads and decodes the length ... and then reads
    until the anounced number of bytes has arrived ... then its done ...
    and the socket can be used for more transfers or other stuff.

Regards ... Rainer



0
Reply Rainer 7/1/2005 8:52:34 AM


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

torpedo@bluebottle.com wrote:
> Hi All,
> 
> I want to transfer the binary file over the network using tcp socket.
> In this how could I detect the end-of-file at the receiving end for
> closing the file ?,  as the binary file could have the end-of-file bit
> pattern in between. 

Repeat after me: "There is no such thing as an 'end-of-file' bit pattern in a
network connection."

> the program is in c++ on linux.

Your blocking read call will return 0 bytes read when it encounters the end of
the data stream (as in the other end closed the stream, which is the absolute
indication of no more data to come).



- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCxWYVagVFX4UWr64RApHkAJ0UkSXxCvZWOYZ+6yCz0qVlDIfMtgCfd/qk
BgJX0DMRYS2C3SYhFxlPsLA=
=0Fgw
-----END PGP SIGNATURE-----
0
Reply Lew 7/1/2005 3:49:41 PM

torpedo@bluebottle.com wrote:
> I want to transfer the binary file over the network using tcp socket.
> In this how could I detect the end-of-file at the receiving end for
> closing the file ?,  as the binary file could have the end-of-file bit
> pattern in between. the program is in c++ on linux.

No such pattern exists. Develop a protocol where the feeder first 
supplies the byte count of the file, then the file. When the receiver 
has received enough number of bytes, the file has been received. This 
also has the advantage that your receiver can fetch the file in the 
most economy block sizes.

-atl-
-- 
A multiverse is figments of its own creations
0
Reply Ari 7/2/2005 8:04:06 AM

3 Replies
757 Views

(page loaded in 0.485 seconds)

Similiar Articles:













7/24/2012 1:46:30 AM


Reply: