Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

by tachyon (Chancellor)
on Nov 11, 2004 at 10:06 UTC ( #406971=note: print w/replies, xml ) Need Help??

in reply to A non-blocking server using 'select' calls

See IO::Socket::INET -- Jettero verses non-blocking in windows for lots of stuff. That is raw socket code but your problem is simply that you need to read one byte at a time if you want to do *teletype mode*. At the moment you read POSIX::BUFSIZ which is probably 512 bytes, thus the blocking while it waits for the rest of the data. s/POSIX::BUFSIZ/1/ and all should be happy.



Replies are listed 'Best First'.
Re^2: A non-blocking server using 'select' calls
by nikos (Scribe) on Nov 11, 2004 at 10:14 UTC
    Thank you for a reply. I'll read it. It seems like can_read waits for a newline character before it sees something like it's buffering the input.

      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.



        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-11-15 07:00 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (181 votes). Check out past polls.