Forum: Ruby socket recv or gets?

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.
Jamal S. (Guest)
on 2008-10-17 11:41
Hello there,

I wonder whats the difference between these methods ( gets and recv )

loop {
      data = current_client.recv(100)
      puts data
}

When I use "gets" instead of recv my socket connection stops and never
print out the message?

Why? :D

Thanks..
Robert K. (Guest)
on 2008-10-17 13:34
(Received via mailing list)
On 17.10.2008 09:40, Jamal S. wrote:
> print out the message?
>
> Why? :D

See documentation. #gets tries to read a line (i.e. until it sees a line
terminator).  Also, I believe recv also works with UDP while I am not
sure about gets.

Cheers

  robert
Jamal S. (Guest)
on 2008-10-17 14:09
Robert K. wrote:
> On 17.10.2008 09:40, Jamal S. wrote:
>> print out the message?
>>
>> Why? :D
>
> See documentation. #gets tries to read a line (i.e. until it sees a line
> terminator).  Also, I believe recv also works with UDP while I am not
> sure about gets.
>
> Cheers
>
>   robert

Hmm, I looked before at documentation and they wrote...

"A separator of nil reads the entire contents..."

so I did :)

@data = gets()

But my code stops after that and wouldn't continue? even though
gets(nil) is the same, I also tried that, didn't work.
unknown (Guest)
on 2008-10-17 14:21
(Received via mailing list)
Jamal S. wrote:

> so I did :)
>
> @data = gets()

in analogy to your recv-Example it should look like:
loop {
      data = current_client.gets
      puts data
}

;)
Andi
Jamal S. (Guest)
on 2008-10-17 14:33
unknown wrote:
> Jamal S. wrote:
>
>> so I did :)
>>
>> @data = gets()
>
> in analogy to your recv-Example it should look like:
> loop {
>       data = current_client.gets
>       puts data
> }
>
> ;)
> Andi

This doesn't work :)

I also tried

while(data = gets)
   p data
end

The code doesn't continue :(
Robert K. (Guest)
on 2008-10-17 15:25
(Received via mailing list)
On 17.10.2008 12:09, Jamal S. wrote:
>>
>>   robert
>
> Hmm, I looked before at documentation and they wrote...
>
> "A separator of nil reads the entire contents..."
>
> so I did :)
>
> @data = gets()

Look again. http://www.ruby-doc.org/core/classes/IO.html#M002297

Note, that instead of gets(nil) you can as well use read.

> But my code stops after that and wouldn't continue? even though
> gets(nil) is the same, I also tried that, didn't work.

You need to understand blocking IO: the IO operation blocks until
everything is read.  In case of a socket, it will only return if there
was an error or the other party closed the socket.  So, you need to
define the protocol in a way that you either know beforehand how much
you need to read (then you can use read(length) or recv(length)) or you
need a separator that you can use to recognize message end (then you can
use gets(separator)).

Kind regards

  robert
This topic is locked and can not be replied to.