Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: A non-blocking server using 'select' calls

by tachyon (Chancellor)
on Nov 11, 2004 at 10:31 UTC ( [id://406977]=note: print w/replies, xml ) Need Help??


in reply to Re^2: A non-blocking server using 'select' calls
in thread A non-blocking server using 'select' calls

Sorry that aint necessarily so. Here is some trivial code that does the teletype thing. The client(s) have no way to disconnect BTW, that is left as an exercise for the reader yada yade.

#!/usr/bin/perl $|++; use IO::Socket; use IO::Select; my $lsn = IO::Socket::INET->new( Listen => 10, LocalAddr => 'localhost', LocalPort => 9000 ); my $client = new IO::Select( $lsn ); while( my @ready = $client->can_read ) { for my $fh (@ready) { if($fh == $lsn) { warn "Accepted new socket\n"; my $new = $lsn->accept; $client->add($new); } else { # process socket sysread($fh, $_, 1 ); print; # NB syswrite(STDOUT,$_,1) will be unbuffered by +default } } }

Note that sysread bypasses STDIO and all buffering. Read will sort of buffer (newlines only in this one byte reading config). I never use recv so really have NFI what it does.

cheers

tachyon

Replies are listed 'Best First'.
Re^4: A non-blocking server using 'select' calls
by nikos (Scribe) on Nov 11, 2004 at 11:34 UTC
    I've tried it on my system, and it gets inside the while loop only when a newline character is send to the server. $client->can_read returns only when a newline character is received. I tried telnet and netcat as clients. It turns out the problem is not with sysread or recv but can_read. Thanks.

      It's not. Add $|++; to the top of your script. I have tested that server on Win2K, WinXp, RH7, Fedora2 and it works as advertised. On Linux you need the $|++ as print passes through STDIO and gets buffered. It will work if you replace the print with syswrite( STDOUT, $_, 1 ) as well.

      cheers

      tachyon

        I know it sounds weird. I've tried changing $|. Nothing happens. It still waits for a newline.
        -bash-2.05b$ uname -a FreeBSD gateway.intra 5.2-CURRENT FreeBSD 5.2-CURRENT #1: Tue Aug 3 1 +4:26:12 MSD 2004 root@gateway.intra:/usr/obj/usr/src/sys/My i386 -bash-2.05b$ perl -v This is perl, v5.6.1 built for i386-freebsd

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found