[Chicago-talk] Reading from a socket

Paul Baker pbaker at where2getit.com
Wed Feb 18 10:43:56 CST 2004


On Feb 18, 2004, at 12:32 AM, Jay Strauss wrote:

> 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?

Could you set the input record separater to null? Similar to doing 
slurp mode where you set it to undef?

> 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
> #   byte by byte and return it.
> sub receive {
>     my ($result,$byte);
>
	{ local $/ = char(0); $result = <$s>; }

>     return $result;
> }

I don't know if that would work, but it seems to me to be the perlish 
way to do it.


-- 
Paul Baker

"Reality is that which, when you stop believing in it, doesn't go away."
          -- Philip K. Dick

GPG Key: http://homepage.mac.com/pauljbaker/public.asc




More information about the Chicago-talk mailing list