Eric Hodel
1/22/2009 2:04:00 AM
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.
$