http://qs321.pair.com?node_id=748009


in reply to How to make EOF?

There isn't an end-of-file character, per-se. You can cause the other end of a socket to detect an EOF by closing the socket. When the other end finishes reading what you wrote eof(SOCKET) will return true.

If you need the other end to read multiple files then you'll need to rely on some convention. Personally I prefer to prefix potentially long transmissions with the size of the transmission rather than relying a special character which could someday appear by accident in the data stream. For example on the sending side:

  print WRITER length($yaml), "\n", $yaml;

Then the reader can do:

my $length = <READER>; chomp($length); my $buffer = ""; while(length $buffer < $length and not eof(READER)) { read(READER, $buffer, 4048, length $buffer); } # do something with the YAML data in buffer...

Possibly interesting side-note - I was just reading the memcached protocol spec and this is exactly how it works too.

-sam

Replies are listed 'Best First'.
Re^2: How to make EOF?
by roboticus (Chancellor) on Mar 04, 2009 at 21:21 UTC
    samtregar:

    Minor (historical) detail: In fact, some operating systems *do* use an EOL marker in files. For example, in CP/M, the directory entries tell you which sectors hold data, but not how many bytes are in your files. So text files would use a ^Z to mark the end of the document. That carried over into DOS, so in DOS programs you'll often find some ^Z characters in files.

    Thank goodness we no longer have to deal with *that* mess!

    ...roboticus