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

In reply to Fork is changing up my IO::Select behaviour by Workman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.