[erlang-questions] determining a socket's file descriptor integer value

Steve Vinoski vinoski@REDACTED
Wed Apr 28 05:47:54 CEST 2010


On Tue, Apr 27, 2010 at 11:29 PM, Jacob Vorreuter
<jacob.vorreuter@REDACTED> wrote:
> When a socket is opened is it possible to get any information about its file descriptor?
>
> I have an Erlang proxy server that accepts incoming HTTP connections, determines an appropriate backend server, opens a socket to that server and then proxies the HTTP request and response data between the backend server socket and the accepted client socket.  I'm curious if I can accept the client socket in Erlang and then pass it to a NIF along with the ip and port of the backend server and do the actual proxying of data in C.  Is that crazy talk?  If I could determine the integer value of the client socket file descriptor that would be enough information to read and write from/to that socket in the C code, right?
>
> The Erlang socket is a tcp_inet port driver from what I can tell.  Is there some way to query it for the information I'm looking for?

You can use prim_inet:getfd(Socket), with the caveat that prim_inet is
an internal module.

You might want to look at the Yaws sendfile linked-in driver I wrote a
couple years ago, since it does similar things to what you're talking
about.

http://github.com/klacke/yaws/blob/master/c_src/yaws_sendfile_drv.c
http://github.com/klacke/yaws/blob/master/src/yaws_sendfile.erl

Not sure a NIF will work for you, though, given that you need to avoid
blocking the VM.

--steve


More information about the erlang-questions mailing list