[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Problem with socket.recv

Joel VanderWerf

10/29/2007 11:21:00 PM

Frank Preiswerk wrote:
> Hi everyone,
>
> I'm currently developing a Ruby TCP client and have a problem with
> socket.recv().
> After establishing a connection with my server, I exchange a couple of
> messages until the server begins to do some calculations based on the
> client's request that take about 10 seconds. After the server finishes, it
> sends back the result. Now my problem is that "data = socket.recv(size)"
> seems to behave non-blocking for some reason. It does not wait for the 10
> seconds but rather executes immediately and "data" remains empty. When I add
> "sleep(10)" before, it works...

#recv gives you data as soon as there is data available, not necessarily
waiting for size bytes.

You may want to buffer it in your ruby code.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

52 Answers

Robert Klemme

10/30/2007 8:30:00 AM

0

2007/10/30, Francis Cianfrocca <garbagecat10@gmail.com>:
> On 10/30/07, Frank Preiswerk <frank.preiswerk@stud.unibas.ch> wrote:
> >
> > Yes, I understand that socket.recv() reads UP TO size bytes, but doesn't
> > necessarily wait.
> > How would you buffer the data?
> >
> > Maybe my server indeed sends some data when I think it doesn't, so my
> > client
> > receives a bit of data and thinks that's it. Buffering should do the trick
> > here. But how?
>
> If the server closes the connection after sending the data, you can simply
> use socket#read. If you need to wait for the data and then process them as
> they comes in, use #select in a loop until all the data have been received.

There is another option - even if the server does not close after
sending: use #read with a given size limit. #read will block and
return as soon data is available. Using #read without limit is a bad
idea on a socket anyway because it has to read until EOS, as you
indicated.

DRb would be another option if both sides are written in Ruby.

Kind regards

robert

7stud --

10/30/2007 9:09:00 AM

0

Robert Klemme wrote:
> 2007/10/30, Francis Cianfrocca <garbagecat10@gmail.com>:
>>
>> If the server closes the connection after sending the data, you can simply
>> use socket#read. If you need to wait for the data and then process them as
>> they comes in, use #select in a loop until all the data have been received.
>
> There is another option - even if the server does not close after
> sending: use #read with a given size limit. #read will block and
> return as soon data is available.

That's not what I'm seeing. What I see is: read() blocks until it
either receives the limit number of bytes or eof is encountered(when the
server closes the socket). In other words, read() does not return as
soon as data is available if the amount of bytes read is less than the
limit number of bytes. On the other hand, recv() returns whatever it
reads immediately. Here is the code:

#server:
--------------
require 'socket'

port = 3030
server = TCPServer.new('', port)

while(conn = server.accept) #serves forever
conn.print 'h'
sleep(5)

conn.print 'ello world'
conn.close
end
--------------


#client using recv():
--------------------
require 'socket'

port = 3030
socket = TCPSocket.new('localhost', port)

all_data = []

while true
partial_data = socket.recv(1012)
puts partial_data

if partial_data.length == 0
break
end

all_data << partial_data
end
socket.close

puts all_data.join()

--output:--
h
ello world
#blank string returned when server closed socket
hello world
------------------------



#client using read():
-----------------
require 'socket'

port = 3030
socket = TCPSocket.new('localhost', port)

all_data = []

while partial_data = socket.read(1012)
puts partial_data
all_data << partial_data
end
socket.close

puts all_data.join()

--output: --
hello world
hello world
----------------


So the way I see it:

1) If the amount of data sent by the server is less than the size limit
specified in read(), read() will block until the server closes the
socket. Therefore, the server must close the socket.

2) If the amount of data sent by the server is more than the size limit
specified in read(), and you just write:

all_data = read(1012)
puts data

then you aren't reading all the data. You just get the first 1012 bytes.





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

Robert Klemme

10/30/2007 10:16:00 AM

0

2007/10/30, 7stud -- <bbxx789_05ss@yahoo.com>:
> Robert Klemme wrote:
> > 2007/10/30, Francis Cianfrocca <garbagecat10@gmail.com>:
> >>
> >> If the server closes the connection after sending the data, you can simply
> >> use socket#read. If you need to wait for the data and then process them as
> >> they comes in, use #select in a loop until all the data have been received.
> >
> > There is another option - even if the server does not close after
> > sending: use #read with a given size limit. #read will block and
> > return as soon data is available.
>
> That's not what I'm seeing. What I see is: read() blocks until it
> either receives the limit number of bytes or eof is encountered(when the
> server closes the socket). In other words, read() does not return as
> soon as data is available if the amount of bytes read is less than the
> limit number of bytes. On the other hand, recv() returns whatever it
> reads immediately. Here is the code:

Yes, I was imprecise: when using #read(limit) the method will block
until either limit bytes were sent or the server closes the
connection; up to limit bytes will be returned. When the server closed
the connection then the next #read will return nil indicating EOS.

Thanks for catching that!

Kind regards

robert

Robert Klemme

10/30/2007 10:19:00 AM

0

2007/10/30, Francis Cianfrocca <garbagecat10@gmail.com>:

> We know nothing about the protocol that the OP is using (because he hasn't
> told us yet). It may not be realistic for the client to have an expectation
> about how much data the server will send. #select may turn out to be the
> best choice.

Quoting from the original posting:

> Now my problem is that "data = socket.recv(size)"
> seems to behave non-blocking for some reason. It does not wait for the 10
> seconds but rather executes immediately and "data" remains empty. When I add
> "sleep(10)" before, it works...

So it seems pretty clear that he knows how many bytes to expect. In
which case I'd use #read because the code will be simpler than with
#select.

> #read with no parameters works if the server closes the connection after
> sending. But as a general rule, that's a bad server design (ideally, the
> client should close the connection, for reasons relating to the TIME_WAIT
> state).

Agreed: the client should establish and close a connection.

Kind regards

robert

Joel VanderWerf

10/30/2007 4:47:00 PM

0

Frank Preiswerk wrote:
> Yes, I understand that socket.recv() reads UP TO size bytes, but doesn't
> necessarily wait.
> How would you buffer the data?

Here's an idea that's a little different from what you're asking for (it
assumes the data stream is chunked using 4 byte length fields, rather
than knowing the length in advance), but it shows a working buffer
implementation. (See also
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...)


module Messageable
LEN_LEN = 4

def recv_message
if (data = recv(LEN_LEN))
if data.empty?
nil
else
len = Message::Length.new(data).value
# for extra safety:
# if len > MAXLEN
# raise MessageLengthError, "MAXLEN exceeded: #{len} > #{MAXLEN}"
# end

msg = ""
part = nil
while (delta = len - msg.length) > 0 and
(part = recv(delta)) and
not part.empty?
yield part if block_given?
msg << part
end
msg.empty? ? nil : msg
end
end
end
end


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Warhol

2/25/2012 11:10:00 AM

0

On Feb 25, 5:02 am, Bubba <Bub@ba> wrote:
> On Fri, 24 Feb 2012, Warhol <mol...@hotmail.com> wrote:
> >Moon Hoax - Rockets CANNOT work in space
>
> >one of the biggest space hoaxes of all time!
>
> >rocket propulsion cannot work in space.
>
> >you get tricked by being told about newtons third law and how the
> >propellant pushes against the body so therefore (equal opposite) has
> >to push the rocket.
>
> >but newtons third law ironically proves this to be false if viewed
> >from the other way round, the propellant cannot push against a vacuum
> >(zero force) so in turn (equal and opposite) applies zero force to the
> >rocket/vehicle itself.
>
> >can anyone find me a video of a rocket, firecracker or something
> >similar creating force in a vacuum?
>
> While everyone knows the "manned" Apollo Moon landings were a
> hoax, rocket propulsion does in fact work in the near vacuum of
> deep space BECAUSE for every action there is an equal and opposite
> reaction.  And there is very little atmospheric or gravitational
> resistance in deep space to such rocket propulsion, meaning that
> the "equal and opposite" sum effect is nearly just that.  The solar
> wind from the Sun probably affects deep space instruments more than
> any other influence, at least within our own solar system.
>
> The published study of astronomy isn't all conspiracy.  Some of it
> is actually educational, enlightening, entertaining and inspiring.
> I only wish all of it was that way.  Much of it is not.
>
> --
> Bub


Right, Bubba...

We all know they never went to the Moon... I know there ain't no ISS
or sattelites above there and also Radio communication don't work
neither in a vacuum environment...

Rocket push against a vacuum = zero force

equal and opposite... remember.the laws?

did Isaac Newton have a science degree when he discovered gravity?

this called thinking!

Warhol

2/25/2012 11:11:00 AM

0

On Feb 25, 5:44 am, harry k <turnkey4...@hotmail.com> wrote:
> On Feb 24, 8:02 pm, Bubba <Bub@ba> wrote:
>
>
>
>
>
>
>
>
>
> > On Fri, 24 Feb 2012, Warhol <mol...@hotmail.com> wrote:
> > >Moon Hoax - Rockets CANNOT work in space
>
> > >one of the biggest space hoaxes of all time!
>
> > >rocket propulsion cannot work in space.
>
> > >you get tricked by being told about newtons third law and how the
> > >propellant pushes against the body so therefore (equal opposite) has
> > >to push the rocket.
>
> > >but newtons third law ironically proves this to be false if viewed
> > >from the other way round, the propellant cannot push against a vacuum
> > >(zero force) so in turn (equal and opposite) applies zero force to the
> > >rocket/vehicle itself.
>
> > >can anyone find me a video of a rocket, firecracker or something
> > >similar creating force in a vacuum?
>
> > While everyone knows the "manned" Apollo Moon landings were a
> > hoax, rocket propulsion does in fact work in the near vacuum of
> > deep space BECAUSE for every action there is an equal and opposite
> > reaction.  And there is very little atmospheric or gravitational
> > resistance in deep space to such rocket propulsion, meaning that
> > the "equal and opposite" sum effect is nearly just that.  The solar
> > wind from the Sun probably affects deep space instruments more than
> > any other influence, at least within our own solar system.
>
> > The published study of astronomy isn't all conspiracy.  Some of it
> > is actually educational, enlightening, entertaining and inspiring.
> > I only wish all of it was that way.  Much of it is not.
>
> > --
> > Bub- Hide quoted text -
>
> > - Show quoted text -
>
> Well, at least you seem to be not _quite_ as stupid as Warhol.
>
> Harry K


In other words, you have no idea what you're talking about. Take some
physics classes then come back for some non-ignorant discussions.

Jeff

2/25/2012 11:15:00 AM

0


>
> Right, Bubba...
>
> We all know they never went to the Moon... I know there ain't no ISS
> or sattelites

So how come that you can see ISS and some satellites from Earth even
with the naked eye?

Jeff

Warhol

2/25/2012 11:35:00 AM

0

On Feb 25, 12:15 pm, Jeff <j...@jsystems.com> wrote:
> > Right, Bubba...
>
> > We all know they never went to the Moon... I know there ain't no ISS
> > or sattelites
>
> So how come that you can see ISS and some satellites from Earth even
> with the naked eye?
>
> Jeff


you see a flying light, which they tell is the ISS, but that is BS...
all a HOAX and illusions to fool the blind sheeps.

I have already many threads here, in this groups, over the ISS HOAX
and other space Hoaxes...

the biggest Space hoaxes of all time!

Neil Armstrong's cryptic speech for the Tax Payers.
http://www.youtube.com/watch?v=P...

"There are Great ideas undiscovered, breakthroughes trues available to
those who can remove one of truth's protecting layers"

The great breakthroughes is that there ain't such thing as Space
travel yet.

Jeff

2/25/2012 12:02:00 PM

0


> you see a flying light, which they tell is the ISS, but that is BS...
> all a HOAX and illusions to fool the blind sheeps.
>
> I have already many threads here, in this groups, over the ISS HOAX
> and other space Hoaxes...
>
> the biggest Space hoaxes of all time!
>

Well oh! font of all knowledge, please tell how this hoax is
perpetuated, when multiple satellites can be viewed by eye from earth,
and their continuous orbits observed from different locations, and the
orbits predicted by orbital maths.

Jeff