Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Win32::Process and Sockets

by Eradicatore (Monk)
on Sep 25, 2001 at 20:43 UTC ( #114578=perlquestion: print w/replies, xml ) Need Help??
Eradicatore has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a perl program on windows that calls a second perl program and then "talks" to that second program. I can start the second program using win32::Process but then how can I do the talking? I assume over a socket on localhost, but I'm not sure how to do that and I've been trying to find more information. Here is what this second program is going to look like for the most part. It's just going to sit there and listen for things from the socket (here just reading from standard in for an example...) and then go get a web page it's told to get.
#!/usr/local/bin/perl -w use LWP::UserAgent; use URI::URL; my $ua = LWP::UserAgent->new; while($line = <STDIN>) { chomp($line); if ($line =~ /now_done/) { last; } else { $url = $line; $file = 'some_web_page.txt'; my $req = HTTP::Request->new(GET => $url); $req->header('Accept' => 'text/html'); $ua->request($req, $file); } }; print "done now\n"; exit;
So you can run this program as is, and if you type in something like "" and enter it will go get that page and put it in a file. But how would the original program that started this program send it things. Thanks for any help!!

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Replies are listed 'Best First'.
Re: Win32::Process and Sockets
by CubicSpline (Friar) on Sep 25, 2001 at 21:40 UTC
    I think the socket approach is likely the best, and most documented. Other possiblities include named pipes, and shared memory which I would avoid for your needs here.

    Basically, I would say to set up your child process to listen on a defined port and the set up the parent process to send instructions to that port. Is there any reason something like that wouldn't work?

    Here is a good node on setting up a client/server setup using IO::Socket. It helped me get off the ground.

      Thanks for the information! I really need to avoid using fork though because I'm already using that now, and since it's completely unsupported right now on windows activestate perl, and is crashing on the exit of the forked process, I need a new solution. Win32::Process works well I believe, but I need to start the process shown above, and then talk to it. So let me see if I understand what I should do. I should pick a port number (8000 or up??) and see if its available. Then if so, open a socket on it (socket::inet or just socket??) and then pass that socket number as a command line argument to the new process I'll be starting. Then that new process can just open a socket from it's end to the same port number, and just start listening to that socket as it does now with STDIN? Is that right? What protocol should I use? I just want to send ascii text one line at a time...

      Justin Eltoft

      "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

(tye)Re: Win32::Process and Sockets
by tye (Sage) on Sep 25, 2001 at 21:51 UTC

    If you only want communication between parent and child, then I'd probably opt for IPC::Open2. Though, on Win32, if you want to do asynchronous operations in Perl, using socket is the best approach.

            - tye (but my friends call me "Tye")
Re: Win32::Process and Sockets
by idnopheq (Chaplain) on Sep 25, 2001 at 22:25 UTC
Re: Win32::Process and Sockets
by Rex(Wrecks) (Curate) on Sep 25, 2001 at 22:33 UTC
    If you use sockets, and if this is a production machine, be careful!

    If the socket stays open for to long at a time it might not be to hard for someone to connect to your socket and feed it whatever they want. Be sure to bind this to loopback only! Named pipes are a more secure way than leaving open sockets around.

    Also check out Network Programming with Perl It has some great interprocess communication examples.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://114578]
Approved by root
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-01-17 19:18 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (203 votes). Check out past polls.