I'm writing a module to interface with the MSN Instant Messenger servers and I've run into a little snag. I'm pretty new to socket programming, having only worked through the MP3 server exercise in the Tutorials section here before. Still, IO::Socket::INET looked like a good choice, providing an interface that looked like the file IO I was used to.

Little did I know what dastardly occurences could...occur. I wanted to read the messages the server was sending me, so I thought a good old my $message = <$server> would do the job, and it did. Until I realised that sometimes I might have to read more than one line. No big deal, I though - I'll just do a magic while loop, and that'll automatically stop when the server's stopped sending information. So here's a slice of code:

while (<$server>) { print "got $_"; chomp; # ...do some more stuff... }

And what did I find? When the server sent me a line, crime of crimes, it recieved the first line, then BLOCKED. Recieving the first line was okay - there was only one line - but shouldn't that have been detected. Running over in the debugger, I could see that it did block at the while condition. Surely the while loop or some other sorcery should have seen that the server had nothing else to send and exited?

Super Search led me to How do I get started (reading data from a socket), but that used the same approach as I'm already using. The only way I can see to get around this at the moment is to hardcode a list of when the server will only send one line and when it could send any amount of stuff. This would feel so horrible, it makes me wince just to think about it. Please aid.



--
my one true love

In reply to reading lines from sockets with while by Amoe

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.