Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Monks,

I've many questions raising now., Let me tell what's in my mind.

So, is it a very worse idea to use <$fh> to read from a socket? Because, it is failing straight away without doing lot of magic(stuffs) in the code.

Server side
while(@ready = $sel->can_read) { if($fh == $lsn) { # Accept the socket $new = $lsn->accept; $sel->add($new); print "New client accepted\n"; print $new "abc\n"; print $new "abcd\n"; print $new "abcde\n"; print $new "abcdef\n"; } else { # Process socket $a = <$fh> ; if (not defined $a) { $sel->remove($fh); $fh->close; print "Client closed\n"; } else { print $a; } } }
Client side
while(@ready = $sel->can_read) { foreach $fh (@ready) { my $rr = <$fh>; exit if ( not defined $rr ); print $rr; } }

In the above sample, client is receiving only abc(first message) sent by server. When server quits, can_read is returning and client is receiving the remaining messages, not blocking. Getting messages one after another.

Then, I tried with sysread as suggested here. When using sysread, should I read byte by byte till '\n'? Because, I don't know the size of the message. Is it a good way to read byte by byte, do concatenation and then process the message?(I don't think so.)

Please, explain me some way to write a error free way(pleasant way too) to receive messages in socket.. or let me know what am I doing wrong.

Thanks.

In reply to Re^2: IO::Select - reading multiple lines by nagalenoj
in thread IO::Select - reading multiple lines by nagalenoj

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 romping around the Monastery: (13)
    As of 2014-09-17 17:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (92 votes), past polls