Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

launching another program but using the existing socket

by Anonymous Monk
on Jul 22, 2001 at 02:34 UTC ( [id://98710]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm relatively new to Perl, and I've got a question about a client/server wrapper app I'm trying to work on. Specifically, here's what I want the client side to do:

1. prompt user for input (an integer)

2. initiate connection with server

3. send user's input to server

4. launch another app, which will also connect to (actually, through) the same server

I'd like my client to ultimately use the same port as the app it launches. What I don't know is, how do I make the launched app (already a stand-alone program) use the same connection I just established??

The server app I'm planning is going to be a TCP redirector, which first accepts user input (corresponds to a particular IP and port), and then redirects the resulting connection to that particular IP and port.

Any advice appreciated - I've tried looking around for an existing TCP redirector in Perl - didn't find one.

Thanks in advance, Glenn

  • Comment on launching another program but using the existing socket

Replies are listed 'Best First'.
Re: launching another program but using the existing socket
by repson (Chaplain) on Jul 22, 2001 at 06:26 UTC
    When you exec another process it has access to any open file-handles within your program whose system file desriptor is within the value of $^F.

    The system uses file numbers 0=STDIN, 1=STDOUT, 2=STDERR and anything else is higher.

    To open using just the fileno, first discover the socket's using fileno($socket) or $socket->fileno and pass that to the execed program on command line or with environment variables.

    Then use the open syntax open FOO, ">& $fileno"; or whatever it is in the open documentation, or use $fh = File::Handle->new_from_fd and $fh->fdopen with the fileno.

    Alternativly follow nardo's advice because those filehandles are always inherited by an exec'ed process and that will make the client program compatible with inetd.

      A couple of fairly minor points:

      First, you probably want open FOO, ">&=$fileno" as using ">&$fileno" will dup() the file descriptor and then open a Perl file handle to it, leaving you with two file descriptors open to the same socket so that closing just the one doesn't shut down the connection.

      Second, I recall recently someone discovering that sockets in Perl are never set to "close on exec" no matter what you do with $^F. I supposed you could consider this a feature, especially in this case. But I'd be a bit shy of depending on it until I found out why it is that way.

              - tye (but my friends call me "Tye")
Re: launching another program but using the existing socket
by nardo (Friar) on Jul 22, 2001 at 03:34 UTC
    If you want an inetd type program which will execute another program where the program's stdin and stdout will be the socket, you can do something like:
    open(STDOUT, '>&'.fileno($socket)); open(STDIN, '<&'.fileno($socket)); exec("$program_path");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://98710]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-18 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found