Socket writing limit

Hey guys,
I am doing some socket programming in ruby and I was wondering if
there was a limit to the amount of data you should write to a socket
at one time. For example, if I do

a = File.new(“blah”) #file blah is 20mb
str = “”
while !a.eof? do
str += a.gets
end

sock.puts(str)

While this is an extreme example, a similar thing would happen if you
just wrote one line at a time to the socket in a while loop because it
would all happen so fast. So that’s my question, is there a limit of
what you should write to a socket, or how fast you should do it?

On Jan 17, 2010, at 5:50 AM, Philliam A. wrote:

sock.puts(str)

While this is an extreme example, a similar thing would happen if you
just wrote one line at a time to the socket in a while loop because it
would all happen so fast. So that’s my question, is there a limit of
what you should write to a socket, or how fast you should do it?

The short answer is that it doesn’t matter since the Ruby runtime,
the underlying OS library, and the OS itself will all work together
to buffer data and send it as fast as the network will allow.

If you write data faster than the network can drain it, your thread
will block waiting for the data to drain. That is true if you do
a single puts of 20Mb or 20 of 1Mb or …

To better understand this realize that every network connection has
an internal buffer, usually less than 64k*, and that Ruby can’t add
data to be transmitted once that buffer is full. It will be drained
as the data is sent and acknowledge by the other side and Ruby
will continue to “top it off” as long as Ruby has more data to write
(i.e. a call to puts with a 20Mb chunk of data). Your thread will
block until Ruby has been able to hand off all the data into these
network buffers.

NOTE: Just because data has been accepted doesn’t mean it has been
received by the other side!

Gary W.

  • See RFC 1323 for ways in which these buffers can be larger.

Wow thanks, this is very helpful.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs