[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can't read more than one message from a socket

Chris Birkinshaw

1/21/2009 10:47:00 PM

I am connecting to a server and then waiting to receive messages over
the socket. For each message received I wish to then execute some code.

I have tried recv, gets, and readpartial all with no luck.

I tried something like this:

loop {
data = socket.recv( 100 )
puts "Line received"
puts data
}

What I see when sending messages from the server is that the first
message is received and printed, then the program hangs for 15 secs,
then the loop starts but each time the data is empty.

I am obviously doing something really stupid but I've spend hours now on
this so if someone could hint at how to do this I would be most
grateful.

Thanks,

Chris
--
Posted via http://www.ruby-....

5 Answers

Eric Hodel

1/22/2009 2:04:00 AM

0


On Jan 21, 2009, at 14:46 PM, Chris Birkinshaw wrote:

> I am connecting to a server and then waiting to receive messages over
> the socket. For each message received I wish to then execute some
> code.
>
> I have tried recv, gets, and readpartial all with no luck.
>
> I tried something like this:
>
> loop {
> data = socket.recv( 100 )
> puts "Line received"
> puts data
> }
>
> What I see when sending messages from the server is that the first
> message is received and printed, then the program hangs for 15 secs,
> then the loop starts but each time the data is empty.
>
> I am obviously doing something really stupid but I've spend hours
> now on
> this so if someone could hint at how to do this I would be most
> grateful.

Since you didn't post your entire program, we can only be of limited
assistance.

This program works for me:

$ ruby -rsocket -e 'sv = TCPServer.new nil, 4000; so = sv.accept;
while data = so.read(10) do p :read => data end'
{:read=>"0123456789"}
{:read=>"\r\n234567\r\n"}
$

It has many flaws, for example it doesn't handle any error conditions,
and only responds to one connection from the server.

The output you see is from connecting with telnet:

$ telnet localhost 4000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
0123456789
234567
^]
telnet> quit
Connection closed.
$

Chris Birkinshaw

1/22/2009 8:17:00 PM

0

Eric Hodel wrote:

> $ ruby -rsocket -e 'sv = TCPServer.new nil, 4000; so = sv.accept;
> while data = so.read(10) do p :read => data end'
> {:read=>"0123456789"}
> {:read=>"\r\n234567\r\n"}
> $
>


Thanks for the help. I used your structure (while data = socket.read) as
follows in my client app (after opening the in_socket to the server):

messageCount = 0
while data = in_socket.recv( 100 )
if data.size > 0
messageCount += 1
puts "Received message #{messageCount}: #{data}"
else
puts "empty"
end
end

I put in the check for empty messages because I am seeing some odd
behaviour.
Basically I can send a certain message to this client no problems
(message 1 to 4 below), but as soon as message 5 is received the program
hangs for 15 secs then goes into a mad loop of empty messages:


Received message 1: MESSAGET 18
1234 162 999 9999
Received message 2: MESSAGET 18
1234 162 999 9999
Received message 3: MESSAGET 18
1234 162 999 9999
Received message 4: MESSAGET 18
1234 162 999 9999
Received message 5: MESSAGET 35
0 162 9 0 "&" 82 -1000 2 9 25 "0"
empty
empty
empty
...


Something in that particular received message is making it go bonkers.
Any ideas?
--
Posted via http://www.ruby-....

Roger Pack

1/23/2009 12:56:00 AM

0

> I tried something like this:
>
> loop {
> data = socket.recv( 100 )
> puts "Line received"
> puts data
> }

recv should work--if you receive "" it means teh connection closed.
Cheers!
-=r
--
Posted via http://www.ruby-....

Brian Candler

1/23/2009 10:38:00 AM

0

Chris Birkinshaw wrote:
> while data = in_socket.recv( 100 )

Try using read(100) instead of recv(100).

read returns nil after the socket has been closed.

recv is a very low-level function, and I'd suggest using it only for UDP
datagrams.
--
Posted via http://www.ruby-....

Robert Klemme

1/23/2009 4:10:00 PM

0

2009/1/21 Chris Birkinshaw <chris.birkinshaw@googlemail.com>:
> I am connecting to a server and then waiting to receive messages over
> the socket. For each message received I wish to then execute some code.
>
> I have tried recv, gets, and readpartial all with no luck.
>
> I tried something like this:
>
> loop {
> data = socket.recv( 100 )
> puts "Line received"
> puts data
> }
>
> What I see when sending messages from the server is that the first
> message is received and printed, then the program hangs for 15 secs,
> then the loop starts but each time the data is empty.
>
> I am obviously doing something really stupid but I've spend hours now on
> this so if someone could hint at how to do this I would be most
> grateful.

Are both processes Ruby processes? In that case it's certainly a lot
more hassle free to use DRb. If not, you should define a protocol
yourself. Either use messages with a fixed length or transmit the
length as first item or use a special EOM marker (you could use a line
delimiter for this, which has the advantage that you can use gets or
each without arguments).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end