in reply to Re: IO::Socket::INET->getline() function
in thread IO::Socket::INET->getline() function

Though getline() function gets one line at a time, I believe that it will wait until the write end of the socket is closed and read end is ready. Socket here I mean of the device. But if device does not close the write end and does not open the read end, say bcos the device goes down just before it tries to close the write end, then will this piece of code keep waiting, since it has already established connection to it and is waiting for reading from the read end of the socket of the device???
  • Comment on Re^2: IO::Socket::INET->getline() function

Replies are listed 'Best First'.
Re^3: IO::Socket::INET->getline() function
by zentara (Cardinal) on Apr 27, 2012 at 14:37 UTC
    Yes, as confusing as you made it sound, you are on the right track. It seems obvious once you understand the problem of what waiting for a newline does, but just think about it. How does the receive end know when a line is finished? A line can be longer than 80 characters.... it could be a million characters, (or even more) all on the same line. So the receive end has to wait until it sees a newline, so it knows the line ended. Sockets are bi-directional, but do allow for just 1 way transfer. If you get into the recv and send methods, you will see you need to have a protocol, like the "over and back to you" in radio communication. So you can block the socket with recv too, because you tell it to wait until it receives x amount. Google for perl non blocking sockets and you will see the various ways you can get around this problem. Sysread the socket is usually the best way, and use IO::Select. There are plenty of examples on google already.

    It only takes a little bit of logic to sysread the socket for lines. Probably an input storage buffer.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh