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

Hello Monks, I am currently downtrodden. Firstly, because I sent my message without asking a question and I'm trying to write communication software. I have a TCP client and a server using IO::Socket and I am trying to get them to talk to one another. I could get the server to receive data from the client using IO::Select, but I couldn't write anything to the client. Since this server had lots of other code in it, I wrote a new server and client with just the essential communication code in it to isolate the problem. They talked back an forth perfectly. Here's the "isolated" server IO::Select code.
my $new_sock = $sock->accept(); my( $select, @ok_to_read, @ok_to_write, $buf, $handle1, $handle2 ); $select = IO::Select->new(); $select->add($new_sock); while ($new_sock){ my @read_from = $select->can_read(1); my @write_to = $select-> can_write(1); foreach $handle1 (@read_from) { $buf = <$new_sock>; # <<<<<<-----------< if($buf){ print $buf; } } foreach $handle2 (@write_to){ print $new_sock "Hello from the SERVER new_sock\n"; } }
The client, which appears to be independent of this problem has for loop in it which sends 500 messages which get printed out in the terminal window of the server and the server's message "Hello from the client... " gets printed out in the client window. Everything looked rosy. But when I transplanted the code back into the real server. I was back to where I started. The offending line is the one with the arrow pointing to it. The real server cannot use a simple accept() statement. It uses a fork as well. The problem is that offending line $buf = <$new_sock> will continue to work, but the subsequent "print $new_sock" no longer works. If I take out the offending line, I can print to the client again. In my opinion, it is somehow the relation of the fork and the $buf = <$new_sock> line that's causing the problem. I have read about using two sockets, but this would be difficult since the client is a machine controller. Is there some other way that I could read from $new_sock that would allow me to use IO:Select? Or is that the right question? Here's the code with the forking server:
while(1) { # loop for the parent # create a connection to incoming EC accept($EC, $SERVER); next if $pid = fork; # parent die "fork: $!" unless defined $pid; # failure #=================== Now, it's all about the child processes ========= +=========# # the socket of the parent is no use to child close($SERVER); # my $new_sock = $sock->accept(); my( $select, @ok_to_read, @ok_to_write, $buf, $handle1, $handle2 ); $select = IO::Select->new(); $select->add($EC); while ($EC){ @ok_to_read = $select->can_read(1); foreach $handle1 (@ok_to_read){ $buf = <$EC>; if($buf){ print $buf; } } @ok_to_write = $select->can_write(1); foreach $handle2 (@ok_to_write){ print $handle2 "Hello from the SERVER new_sock\n"; } #sleep (1); } close($SERVER); } #$loop for the parent

Replies are listed 'Best First'.
Re: Fork is changing up my IO::Select behaviour
by ikegami (Patriarch) on Feb 23, 2010 at 23:16 UTC

    Don't use readline (<>) on filehandles you pass to select.

    It can block (which defies the purpose of using select), and it can hide waiting data from select (meaning select won't wake up even though data is waiting to be read).

    foreach $handle1 (@read_from) { $buf = <$new_sock>; if($buf){ print $buf; } }
    should be
    foreach my $handle1 (@read_from) { my $buf = ''; my $rv = sysread($handle1, $buf, 64*1024, length($buf)); # Handle error if !defined($rv) # Handle eof if !$rv print $buf; # Whatever we got, including partial lines }
    or
    foreach my $handle1 (@read_from) { our $buf; local *buf = \( $buf{fileno($handle1)} ); # alias $buf = '' if !defined($buf); my $rv = sysread($handle1, $buf, 64*1024, length($buf)); # Handle error if !defined($rv) # Handle eof if !$rv while ($buf =~ s/^(.*\n)//) { print $1; # Full line at a time } }

    Does that also fix the problem you are reporting?

      Thanks very much for your reply. Clearly and improvement on my $buf = <$EC>. I used both of your code snips but the behaviour did not change. I am still not able to write to the socket, only read from it. The code is entering the for @ok_to_write loop (i changed the array name), where I can print to the server terminal using a bare print "\n"; . But it will not print to the socket.

      Trying to write to the client Hello from the client... 1 Trying to write to the client Hello from the client... 2 Trying to write to the client Hello from the client... 3

      I tried your code in the isolated client and server (without the fork) and got the same behaviour as before. The data flies between the client and server. I'm pretty sure there's something about the fork that plays a part here. Another difference I notice, is that with forking server, the data from the client is just plodding in.

      Here's the code as it now stands in Server_rebuild.pl

      #!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; # Server_rebuild.pl my( $EC, $pid); my $SERVER = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '50000', Proto => 'tcp', Listen => 3, Reuse => 1, ); die "Could not create socket: $!\n" unless $SERVER; while(1) { # loop for the parent # create a connection to incoming EC accept($EC, $SERVER); next if $pid = fork; # parent die "fork: $!" unless defined $pid; # failure #=================== Now, it's all about the child processes ========= +=========# # the socket of the parent is no use to child close($SERVER); # my $new_sock = $sock->accept(); my( $select, @ok_to_read, @ok_to_write, $buf, $handle2 ); #$handle1, $select = IO::Select->new(); $select->add($EC); while ($EC){ @ok_to_read = $select->can_read(1); @ok_to_write = $select->can_write(1); foreach my $handle1 (@ok_to_read) { my $buf = ''; my $rv = sysread($handle1, $buf, 64*1024, length($buf)); # Handle error # Handle eof while ($buf =~ s/(.*\n)//) { print $1; # Full line at a time } } foreach $handle2 (@ok_to_write){ print $handle2 "Hello from the SERVER new_sock\n"; print $EC "Hello from the SERVER new_sock\n"; print "Trying to write to the client\n"; } #sleep (1); } } #$loop for the parent

        I believe I have the behaviour problem solved. I wasn't using autoflush. I don't fully understand it because the data passed back and forth very well when just using accept(), but not with accept() and fork(). In any case, $EC->AUTOFLUSH; allowed me to write out of the server to the client.

        Thanks for your help. I've added your code.

        Suffering from buffering?
        use IO::Handle; $EC->autoflush(1);

        (doh! I had opened the window a while ago and forgot to check for replies before trying to debug)