# Socket writing limit

**URL:** <https://www.ruby-forum.com/t/socket-writing-limit/181735>\
**Category:** Ruby\
**Created:** [January 17, 2010, 11:50am UTC](https://www.ruby-forum.com/t/socket-writing-limit/181735 "2010-01-17T11:50:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philliam\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@Philliam\_A](https://www.ruby-forum.com/u/Philliam_A)\
**Post date:** [January 17, 2010, 11:50am UTC](https://www.ruby-forum.com/t/socket-writing-limit/181735/1 "2010-01-17T11:50:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Philliam\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@Philliam\_A](https://www.ruby-forum.com/u/Philliam_A)\
**Post date:** [January 17, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/socket-writing-limit/181735/2 "2010-01-17T18:49:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Philliam\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@Philliam\_A](https://www.ruby-forum.com/u/Philliam_A)\
**Post date:** [January 17, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/socket-writing-limit/181735/3 "2010-01-17T23:50:41Z")

</div>

Wow thanks, this is very helpful.
