mnperez has asked for the wisdom of the Perl Monks concerning the following question:
There are several articles written that I have been able to find on reading data from sockets, and there is one about reading from multiple sockets that I think touches a little on how I would accomplish this, but I am still a bit vague on how to do it correctly.
I need to read data from a socket a single byte at a time, after a certain point. It is a custom protocol which is combined ASCII and BINARY, with ASCII as the headers and certain meta-data components, and BINARY for the raw data.
I would like to be able to read the headers line by line (since they appear at the beginning of the session and never again), then read byte-by-byte through the rest of the session, scanning for certain ASCII characters within it that denote the beginning of the meta-data, and so on and so forth.
Anyone have any insight on how to accomplish this?
Perl doesn't really distinguish between text and binary data
heres a kind of way to do what you want (untested).
# create socket $sock with IO::Socket or similar
my @headers;
while (<$sock>) {
last unless /\S/; # end on a blank 'line' ("\n\n"), like in HTTP
chomp;
push @headers, $_;
}
while (defined($_ = getc($sock))) {
my $dec = ord($_);
# process eithier $_, the ascii equivilent or $dec, the decimal valu
+e
}