|Just another Perl shrine|
reading lines from sockets with whileby Amoe (Friar)
|on Feb 18, 2002 at 18:19 UTC||Need Help??|
Amoe has asked for the
wisdom of the Perl Monks concerning the following question:
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:
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