[Chicago-talk] Reading from a socket

Jay Strauss me at heyjay.com
Wed Feb 18 00:32:29 CST 2004


Hi, I'm wondering if the is an easier (better) way to read a socket.  I'm
doing (roughly) like below.  I noticed in POE you can just give it a message
terminator, and if automatically reads it correctly.  Is there a way to do
this with IO::Socket::INET or maybe with sysread?


my $s = IO::Socket::INET->new(Proto=>'tcp',PeerAddr=>$host,PeerPort
=>$port);

my $field = receive;

#   Read the bytes of the line one at a time until we find an ascii "0"
#   which the server uses to indicate end of message.  Build up the return
string
#   byte by byte and return it.
sub receive {
    my ($result,$byte);

    while (sysread($s, $byte, 1) == 1) {
        last if ord($byte) == 0;
        $result .= $byte;
    }
    return $result;
}

Thanks
Jay




More information about the Chicago-talk mailing list