how to set socket options?

Discussion in 'Ruby' started by Nasir Khan, Feb 29, 2008.

  1. Nasir Khan

    Nasir Khan Guest

    [Note: parts of this message were removed to make it a legal post.]

    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 <string> I am not getting
    anything on the server till I do socket.close.
    socket.puts <string> 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
     
    Nasir Khan, Feb 29, 2008
    #1
    1. Advertising

  2. Nasir Khan

    Gary Wright Guest

    On Feb 28, 2008, at 7:53 PM, Nasir Khan wrote:
    > Also while writing a TCP client to a non Ruby (but non blocking)
    > server
    > while I write on the socket using socket.write <string> I am not
    > getting
    > anything on the server till I do socket.close.
    > socket.puts <string> 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 Wright
     
    Gary Wright, Feb 29, 2008
    #2
    1. Advertising

  3. Nasir Khan

    Nasir Khan Guest

    [Note: parts of this message were removed to make it a legal post.]

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

    On Thu, Feb 28, 2008 at 10:52 PM, Gary Wright <> wrote:

    >
    > On Feb 28, 2008, at 7:53 PM, Nasir Khan wrote:
    > > Also while writing a TCP client to a non Ruby (but non blocking)
    > > server
    > > while I write on the socket using socket.write <string> I am not
    > > getting
    > > anything on the server till I do socket.close.
    > > socket.puts <string> 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 Wright
    >
    >
     
    Nasir Khan, Feb 29, 2008
    #3
  4. Nasir Khan

    hemant Guest

    On Fri, Feb 29, 2008 at 6:23 AM, Nasir Khan <> 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 <string> I am not getting
    > anything on the server till I do socket.close.
    > socket.puts <string> 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.
     
    hemant, Feb 29, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,084
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,026
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    594
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    711
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    756
Loading...

Share This Page