# Buffer in TCPSocket when communicating via 127.0.0.1?

**URL:** <https://www.ruby-forum.com/t/buffer-in-tcpsocket-when-communicating-via-127-0-0-1/104309>\
**Category:** Ruby\
**Created:** [May 16, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/buffer-in-tcpsocket-when-communicating-via-127-0-0-1/104309 "2007-05-16T17:43:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipe\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@Felipe\_N](https://www.ruby-forum.com/u/Felipe_N)\
**Post date:** [May 16, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/buffer-in-tcpsocket-when-communicating-via-127-0-0-1/104309/1 "2007-05-16T17:43:48Z")

</div>

I’m using the TCPSocket.send, TCPSocket.gets and TCPSocket.gets to  
make communication between a ruby script and a C program in Linux. I’m  
experiencing some troubles with synchronization and lag.  
There is no problem with the C program; it’s working fine.

There is any buffer in TCPSocket when communicating via 127.0.0.1?  
Where I can read anything about this ?

Thanx!

---

<div class="post-metadata">

**Author:** ![Felipe\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@Felipe\_N](https://www.ruby-forum.com/u/Felipe_N)\
**Post date:** [May 16, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/buffer-in-tcpsocket-when-communicating-via-127-0-0-1/104309/2 "2007-05-16T19:21:57Z")

</div>

On Thu, May 17, 2007 at 12:43:13AM +0900, Felipe N. wrote:

> I’m using the TCPSocket.send, TCPSocket.gets and TCPSocket.gets to  
> make communication between a ruby script and a C program in Linux. I’m  
> experiencing some troubles with synchronization and lag.  
> There is no problem with the C program; it’s working fine.
> 
> There is any buffer in TCPSocket when communicating via 127.0.0.1?  
> Where I can read anything about this ?

Can you describe your symptoms more accurately?

If you are seeing a delay of 100ms before a packet is sent, then you are  
probably hitting the Nagle algorithm in the kernel. You can setsockopt  
TCP\_NODELAY to turn it off.

Also look at IO#sync=true

---

<div class="post-metadata">

**Author:** ![Felipe\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@Felipe\_N](https://www.ruby-forum.com/u/Felipe_N)\
**Post date:** [May 17, 2007, 7:24am UTC](https://www.ruby-forum.com/t/buffer-in-tcpsocket-when-communicating-via-127-0-0-1/104309/3 "2007-05-17T07:24:58Z")

</div>

Felipe N. wrote:

> I’m using the TCPSocket.send, TCPSocket.gets and TCPSocket.gets to  
> make communication between a ruby script and a C program in Linux. I’m  
> experiencing some troubles with synchronization and lag.  
> There is no problem with the C program; it’s working fine.
> 
> There is any buffer in TCPSocket when communicating via 127.0.0.1?  
> Where I can read anything about this ?

You might want to be using TCPSocket#recv instead of #gets. IIRC, the  
latter adds a layer of buffering.
