Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Socket IO NO new line

by jasonk (Parson)
on May 07, 2008 at 02:36 UTC ( [id://685124]=note: print w/replies, xml ) Need Help??


in reply to Socket IO NO new line

You can use sysread to read any available bytes from the stream without worrying about whether there is a newline in there or not. Put whatever you read into a buffer in case it was only part of a transaction...

#!/usr/bin/perl -w use strict; use warnings; use IO::Socket; use IO::Select; my $listener = IO::Socket::INET->new( LocalPort => 42424, Listen => 1, ) or die "failed to create socket: $!"; my $select = IO::Select->new( $listener ); my %buffers = (); while ( my @ready = $select->can_read ) { foreach my $fh ( @ready ) { if ( $fh == $listener ) { my $new = $listener->accept; $buffers{ $new } = ''; $select->add( $new ); } else { $fh->sysread( $buffers{ $fh }, 1024, length( $buffers{ $fh + } ) ); while ( $buffers{ $fh } =~ s/STX(.*?)ETX// ) { process( $1 ); } } } } sub process { print "GOT: $_[0]\n"; }

www.jasonkohles.com
We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-19 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found