# How to set socket options?

**URL:** <https://www.ruby-forum.com/t/how-to-set-socket-options/130527>\
**Category:** Ruby\
**Created:** [February 29, 2008, 1:59am UTC](https://www.ruby-forum.com/t/how-to-set-socket-options/130527 "2008-02-29T01:59:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 29, 2008, 1:59am UTC](https://www.ruby-forum.com/t/how-to-set-socket-options/130527/1 "2008-02-29T01:59:24Z")

</div>

We can set socket options using setsockopt on BasicSocket, where can I  
get  
various constants SOL\_ constants such as Socket::SOL\_SOCKET etc?  
I am unable to find them in rdoc.

Also while writing a TCP client to a non Ruby (but non blocking) server  
while I write on the socket using socket.write I am not getting  
anything on the server till I do socket.close.  
socket.puts works fine but has an additional newline in the  
message  
which I do not want.

Any pointers in both of these questions will be greatly appreciated.

Thanks  
Nasir

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 29, 2008, 4:53am UTC](https://www.ruby-forum.com/t/how-to-set-socket-options/130527/2 "2008-02-29T04:53:31Z")

</div>

On Feb 28, 2008, at 7:53 PM, Nasir K. wrote:

> Also while writing a TCP client to a non Ruby (but non blocking)  
> server  
> while I write on the socket using socket.write I am not  
> getting  
> anything on the server till I do socket.close.  
> socket.puts works fine but has an additional newline in  
> the message  
> which I do not want.

Try socket.flush. It seems like your socket is line buffered. I’m not  
sure how Ruby decides on buffering and there doesn’t seem to be an  
interface to something like setvbuf. Perhaps someone else can provide  
some insight.

One thing to keep in mind when using TCP is that message boundaries  
are not preserved. A write on one end of a TCP socket does not  
necessarily  
map to a corresponding read with the same data on the other  
end of the socket.

Gary W.

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 29, 2008, 5:14am UTC](https://www.ruby-forum.com/t/how-to-set-socket-options/130527/3 "2008-02-29T05:14:01Z")

</div>

I tried socket.flush it doesnt work either. The reason I asked for  
socketopt  
options is I wanted to try enabling TCP\_NODELAY.

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 29, 2008, 5:50am UTC](https://www.ruby-forum.com/t/how-to-set-socket-options/130527/4 "2008-02-29T05:50:55Z")

</div>

On Fri, Feb 29, 2008 at 6:23 AM, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> We can set socket options using setsockopt on BasicSocket, where can I get  
> various constants SOL\_ constants such as Socket::SOL\_SOCKET etc?  
> I am unable to find them in rdoc.

I am afraid, RDoc won’t have an exhaustive list of these constants.  
However, for all such purposes, I have Richard Stevens book on network  
programming book and you can find them listed there.

> Also while writing a TCP client to a non Ruby (but non blocking) server  
> while I write on the socket using socket.write I am not getting  
> anything on the server till I do socket.close.  
> socket.puts works fine but has an additional newline in the message  
> which I do not want.

As for disabling naggle algorithm you can use:

t\_sock\_addr = Socket.sockaddr\_in(port,ip)  
t\_socket.setsockopt(Socket::IPPROTO\_TCP,Socket::TCP\_NODELAY,1)

Also problem you mention shouldn’t have anything to do with flush or  
naggle algorithm.  
Probably, your server is using newline as message separator in which  
case. But this is just a wild guess.
