Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have a problem with IO::Select. See the server.pl/client.pl listing below for how I try to use it. My problem is, that at first select does what it should, but then can_read(0) always returns something, which it should not.
I think you can understand what the problem is as soon as you look at the code+logfile. I can't find out how to solve this and I would appreciate any help.

The server:
#!/usr/bin/perl use strict; use warnings; use IO::File; use IO::Socket; use IO::Select; use Data::Dumper; my $sock = new IO::Socket::INET ( LocalHost => 'perlplexity.org', LocalPort => 1200, Listen => 5, Proto => 'tcp', Reuse => 1, ); die "Socket couldn't be created: $!" unless $sock; $sock->autoflush; my $message = ""; my $state = "UNCONNECTED"; my $fh = new IO::File "~/.somefile", "r"; my $select = new IO::Select(); # create handle set for reading $select->add($sock); # add the main socket to the set $select->add($fh); # add the main socket to the set while (1) { my @ready = $select->can_read(0); my $conn; my $buff; foreach my $rh (@ready) { print "GOT SOMETHING!\n"; if ($rh == $sock) { $conn = $rh->accept(); $select->add($conn); print "CONNECTION ADDED\n"; } elsif ($rh == $conn) { $buff = ""; my $rx_txt; while (length($rx_txt)) { $sock->recv($rx_txt, 1024); $buff.=$rx_txt; } $select->remove($rh); if ($buff eq "HELLO\n") { $state = "CONNECTED"; print "CONNECTED\n"; } else { $state = "UNCONNECTED"; print "UNCONNECTED\n"; $select->remove($rh); close($rh); } } elsif ($rh == $fh) { if ($state == "CONNECTED") { while (<$fh>) {$sock->send($_);} print $fh ""; } } } }
This is the client:
use strict; use warnings; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'perlplexity.org', PeerPort => 1200, Proto => 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sock; $sock->send("HELLO\n"); close ($sock);
The logfile:
lofile, obtained from "perl server.pl > log" and the perl client.pl on + the other machine GOT SOMETHING! CONNECTION ADDED GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! GOT SOMETHING! ... this goes on until I interrupt it with ctrl-c
The question is: Why are there so many "GOT SOMETHINGS"?

In reply to Problem with IO::Select by Anonymous Monk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (6)
    As of 2019-07-17 22:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?