wavenator has asked for the wisdom of the Perl Monks concerning the following question:

i built this server application it sends and receives msg's but the buffer block the input handle how can i denie it?
use IO::Socket; # Create the receiving socket my $s = new IO::Socket::INET ( LocalPort => 7070, Proto => 'tcp', Listen => 16, Reuse => 1, ); die "Could not create socket: $!\n" unless $s; my ($ns, $buf); use IO::Select; $read_set = new IO::Select(); $read_set->add($s); while (1) { # forever my ($rh_set) = IO::Select->select($read_set, undef, undef, 0); foreach $rh (@$rh_set) { $rh->autoflush(1); if ($rh == $s) { $ns = $rh->accept(); $read_set->add($ns); } if ($rh == $ns) { $buf = <$rh>; print "$buf"; } else{ $msg=<STDIN>; print $ns "$msg"; } } }

Replies are listed 'Best First'.
Re: make my code non block
by Somni (Friar) on Oct 12, 2007 at 00:48 UTC
    Regarding your stated question, in order to not block on your read of STDIN you need to do the same thing you're doing with your sockets; namely, select on it. Do realize, however, that filehandles attached to terminals will not normally have input ready until they see a newline, so your input will not be ready until the user hits enter.

    I notice you're mixing IO::Select with buffered reads (<$rh>) and writes (print $ns). While you may sometimes be able to get away with this (there are POSIX rules, apparently, that let you), I would not rely on it.

    When using select you should use sysread and syswrite (or send and recv, though you don't need the flags), as mentioned in the documentation for perldoc -f select and shown in the example code in the UDP Message Passing section of perlipc.

    However, manually handling the buffers for all this can easily get tedious, so maybe you should look into an event system like Event or POE.

      thx for replay but it still block each other
Re: make my code non block
by chrism01 (Friar) on Oct 12, 2007 at 00:40 UTC
Re: make my code non block
by BrowserUk (Patriarch) on Oct 12, 2007 at 01:28 UTC
      windows