C socket to Ruby socket

Discussion in 'Ruby' started by Andre Nathan, Sep 5, 2006.

  1. Andre Nathan

    Andre Nathan Guest

    Hello

    What is the correct way to convert a C socket (int) to a Ruby Socket
    object in a C extension? Something like

    int s = socket(...);
    VALUE sock = rb_some_func(s);

    In my extension there's a callback function that receives a socket as an
    argument, and I'd like to pass that socket as an argument to a ruby
    block, so I need to convert it to a Socket object first.

    Thanks in advance,
    Andre
     
    Andre Nathan, Sep 5, 2006
    #1
    1. Advertisements

  2. On 9/5/06, Andre Nathan <> wrote:
    > Hello
    >
    > What is the correct way to convert a C socket (int) to a Ruby Socket
    > object in a C extension? Something like
    >
    > int s = socket(...);
    > VALUE sock = rb_some_func(s);
    >
    > In my extension there's a callback function that receives a socket as an
    > argument, and I'd like to pass that socket as an argument to a ruby
    > block, so I need to convert it to a Socket object first.
    >
    > Thanks in advance,
    > Andre
    >
    >
    >


    does it need to be a Socket? class IO can be initialized with a file descriptor.
     
    Francis Cianfrocca, Sep 5, 2006
    #2
    1. Advertisements

  3. Andre Nathan

    Andre Nathan Guest

    Hi!

    On Wed, 2006-09-06 at 01:17 +0900, Kent Sibilev wrote:
    > You probably can call Socket.for_fd using rb_funcall.


    I got it to work with an IO:

    rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2NUM(socket));

    However I can't seem to be able to make rb_cSocket visible to my
    extension. Any hints on how to do that?

    Thanks,
    Andre
     
    Andre Nathan, Sep 6, 2006
    #3
    1. Advertisements

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. Avizz
    Replies:
    3
    Views:
    14,869
    Andy Fish
    Sep 29, 2003
  2. greatestmclarenfan
    Replies:
    2
    Views:
    754
    Steve Horsley
    Feb 16, 2006
  3. Rune
    Replies:
    6
    Views:
    579
    Raymond Hettinger
    Aug 26, 2003
  4. Laszlo Nagy
    Replies:
    1
    Views:
    7,192
    Mark Wooding
    Jan 27, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    998
  6. anne001
    Replies:
    1
    Views:
    965
  7. Serge Savoie

    Ruby Socket to Java Socket

    Serge Savoie, Sep 25, 2008, in forum: Ruby
    Replies:
    7
    Views:
    304
    Adrian Gh
    Jul 27, 2009
  8. Kamaljeet Saini
    Replies:
    2
    Views:
    238
    Kamaljeet Saini
    Jan 30, 2009
Loading...