Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm using 'perl' with 'C'. The C function'll invoke the perl subroutine. The subroutine has to create a socket, accept connection and return the fd of the socket connection. And the fd will be used in my 'C' code for receiving the requests from the client (only one). Please have a look at the below perl code.
sub CreateServer { my($PORT) = 10066; my($ret); $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; $client = $server->accept(); $client->autoflush(1); return $ret; } Can i use the below two lines of code for getting the fd before the return statement $ret = fileno $client; return $ret; And is it valid?.
Otherwise can i use the socket() call instead of the above mentioned IO::Socket::INET->new() method and use 'fileno' to get the fd. Is it OK?. Please suggest if you know some other way. Thanks, Poorna

Replies are listed 'Best First'.
Re: How to get the fd from socket perl call?.
by AgentM (Curate) on Apr 01, 2001 at 21:16 UTC
    This is a perfectly legal use for the fileno function. In UN*X, you are guaranteed that all new file descriptors are unique and static throughout their use. Perl wouldn't dare mangle these numbers (they are, after all, just ints). So cast that variable to an int using perlembed and some *_*_SV(...) magic and you're ready to fly! Be sure to be pedantic and die if fileno returns undef, though. Enjoy the benefits of a fine operating system.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.