Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get fd or handle from socket object

Get fd or handle from socket object


I want to create a native (c++) module for node.js which is able to send sockets to another node process, which is completely unrelated to the current process. To do so, I tought of using the ancillary library, which has a really, really easy API for this. The problem I have to solve now is how I can get the fd or the handle of a socket object of node.js.

There's a TCPWrap class in & tcp_wrap.h, which has a property called handle_, which holds a uv_tcp_t object from libuv, but that property is private. Also I can't #include because it's just a module of node.js an not directly in node.js itself. I don't know if it's a good idea to copy the source files to my module just to get the that class...

Have you any ides how I could do it?

I doesn't have to run on winows, tough.


Problem courtesy of: Van Coding


I finally found a way to do it. You can find the node module here:

I've just taken the headers "tcp_wrap.h","stream_wrap.h" and "handle_wrap.h" and then included "tcp_wrap.h".

I could then get the object the following way:

TCPWrap* wrap = static_cast(args[0]->ToObject()->GetPointerFromInternalField(0));
StreamWrap* s = (StreamWrap*)wrap;

The following code then gives access to the file descriptor

Solution courtesy of: Van Coding


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Get fd or handle from socket object


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription