Forum: Ruby reading rompt protocols

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaebff3a91b1d10c9fcf71589b45c05a?d=identicon&s=25 Mihai Vlad (Guest)
on 2006-02-23 17:12
(Received via mailing list)
I'm trying to implement a function that reads from a socket ( TCP/IP
based protocol with prompt ).

Server sends:

Welcoming you\r\n
Please login\r\n
<login>


I need this function to be a wrapper for gets (readline),  that would
read each line of the 3. The problem is that only the first two lines
end in CRLF( or only \n doesn't really matter ). I've tried rewriting
something like:

    def _gets()
        c = ''
        answer = ''

        while true
            c = @sock.sysread( 1 )
            if c == "" then
                break
            end

            #adding the character to the string
            answer = answer+c

            if c.match("\n") then
                break
            end
        end

        return answer
    end


The bad part is that sysread hangs on trying to read the socket.

I've used this pseudo code in pearl, but there the sysread function
would return >0 or 0 in case of error. [ if ( sysread( $sock, $char, 1)
 >0  ) {....} ]


I'm not interested in time outing the sysread function nor using a
single recv function, so please, can someone please tell me a solution?
This topic is locked and can not be replied to.