Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: How to make EOF?

by samtregar (Abbot)
on Mar 04, 2009 at 06:35 UTC ( #748009=note: print w/ replies, xml ) Need Help??

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.


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

    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!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://748009]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-12-01 20:46 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (26 votes), past polls