Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Does IO::Select work? Anywhere?

by zentara (Archbishop)
on Oct 21, 2012 at 12:08 UTC ( [id://1000213]=note: print w/replies, xml ) Need Help??


in reply to Does IO::Select work? Anywhere?

This code runs rock solid for me on Linux, it's about as simple an IO::Select example that you can get. Start server first, then client.
Server:

#!/usr/bin/perl use IO::Socket; use IO::Select; my @sockets; my $machine_addr = 'localhost'; $main_sock = new IO::Socket::INET(LocalAddr=>$machine_addr, LocalPort=>1200, Proto=>'tcp', Listen=>3, Reuse=>1, ); die "Could not connect: $!" unless $main_sock; print "Starting Server\n"; $readable_handles = new IO::Select(); $readable_handles->add($main_sock); while (1) { ($new_readable) = IO::Select->select($readable_handles, undef, undef +, 0); foreach $sock (@$new_readable) { if ($sock == $main_sock) { $new_sock = $sock->accept(); $readable_handles->add($new_sock); } else { $buf = <$sock>; if ($buf) { print "$buf\n"; my @sockets = $readable_handles->can_write(); #print $sock "You sent $buf\n"; foreach my $sck(@sockets){print $sck "$buf\n";} } else { $readable_handles->remove($sock); close($sock); } } } } print "Terminating Server\n"; close $main_sock; getc();

And a general purpose client:

#!/usr/bin/perl -w use strict; use IO::Socket; my ( $host, $port, $kidpid, $handle, $line ); ( $host, $port ) = ('localhost',1200); my $name = shift || ''; if($name eq ''){print "What's your name?\n"} chomp ($name = <>); # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port $port on $host: $!"; $handle->autoflush(1); # so output gets there right away print STDERR "[Connected to $host:$port]\n"; # split the program into two processes, identical twins die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process if ($kidpid) { # copy the socket to standard output while ( defined( $line = <$handle> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } # the else{} block runs only in the child process else { # copy standard input to the socket while ( defined( $line = <STDIN> ) ) { print $handle "$name->$line"; } }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Does IO::Select work? Anywhere?
by BrowserUk (Patriarch) on Oct 21, 2012 at 13:07 UTC
    This code runs rock solid for me on Linux, it's about as simple an IO::Select example that you can get.

    Thank you. But ... :)

    • It is pretty much the same -- ie. just as incomplete -- as every other sample I can find.

      Whether those kicking around the internet, or in the Perl POD or even those in Chapter 12 of Advanced Perl Programming.

      They only deal with reading. Not replying; not handling exceptions, ...

    • It (apparently) breaks all the rules of select loop processing ...

      By using blocking IO; and buffered IO primitives; assumes accept will actually return a client; doesn't handle signals; ...

    If there is a complete, correct example out there, I'm damned if I can find it.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      If there is a complete, correct example out there, I'm damned if I can find it.

      :-) Yeah, that is why I like using the eventloop method that Glib and Wx use, where their eventloop can detect conditions like IN , HUP, ERR, etc. I would think if you looked at the c based source code for glib's io::watch method, you will probably find the c code, which allows the eventloop's intelligence to report back IN or HUP, or ERR.

      From some discussion I remember from back in my c experiments, there are some sort of error flag set, with values like eagain, einttr, etc. See blocking sockets . That code looks like it could be rewritten in a Perl script, although you may need require 'sys/ioctl.ph';.

      P.S. See the low level Perl code, multiplexing server at perl examples


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        that is why I like using the eventloop method that Glib and Wx use

        I guess it explains the popularity of such frameworks, when the primary perl tool -- IO:Select -- has been broken for 12 years or more.

        And probably why all the examples are so bare bones. Every time anyone has tried to write anything more complex, it breaks. So they either give up or resort to some higher-level framework.

        P.S. See the low level Perl code, multiplexing server at perl examples

        Thanks. Unfortunately, the first thing I see in the I/O Multiplexing Server example is:

        sub readline { # 'select'-compatible function for reading one line of input from # a filehandle. # # readline() expects one argument -- filehandle S # # sample call: $mystring = readline('S') # my $filehandle = $_[0]; my $c=''; my $retstr=''; my $endoffile=0; while ($c ne "\n" && ! $endoffile) { if (sysread($filehandle, $c, 1) > 0) { $retstr = $retstr . $c; } else { $endoffile=1; } } return $retstr; }

        What if the client gets interrupted before it sends the newline?

        You've got a something like 900 seconds default before the connection times out and the endoffile flag gets raised; during which time, nothing else happens. No new clients get accepted, no existing clients get serviced.

        And this is teaching material??


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found