A simple client/server problem

From: Dan Timis (timis_at_museresearch.com)
Date: 07/30/04


Date: Thu, 29 Jul 2004 22:47:23 -0700
To:  <beginners@perl.org>

Hi everyone,

I am very new to Perl. I need two perl scripts, one would run on a
client, the
other would run on a server.

The perl script on the client machine runs an application. The
application
creates a file called "request.xml" The perl script reads the file,
and sends
it to the server.

A cgi script on the server reads the file and saves it as "request.xml"
then
runs another application. The application reads "request.xml" and
creates
"reply.xml". The server perl script reads "reply.xml" and sends it
back to the
client.

The client reads the data from the server and saves it as "reply.xml"

I looked all over for some example code. I found lots of examples
about how to
upload a file to the server, but they are all about the server part,
and how to
generate html that would let a user choose a file to upload. I could
not find
anything about how a client would connect to a server, send some data,
and then
read some data back.

I think I have figured out the server part. It would be something like
this:

     # read request from client and save it in a file
     read(STDIN, my $request, $ENV{'CONTENT_LENGTH'});
     open REQUEST_FILE, ">" . "request.xml" or
         croak "Cannot create REQUEST_FILE: $!";
     print REQUEST_FILE $request;
     close REQUEST_FILE;

     # save old stdout, redirect stdout to a temp file
     open my $oldout, ">&STDOUT" or
         croak "Can't duplicate STDOUT: $!";
     open STDOUT, ">" . "temp.txt" or
         croak "Can't redirect STDOUT: $!";

     # run the create-reply application,
     # close stdout, and restore the saved stdout
     system"create-reply";
     close STDOUT;
     open STDOUT, ">&", $oldout or
         croak "Can't restore old STDOUT: $!";

     # read the reply.xml file
     open REPLY_FILE, "reply.xml" or
         croak "Cannot open REPLY_FILE: $!";
     (undef, undef, undef, undef, undef, undef, undef, my $replySize,
undef,
undef, undef, undef, undef ) = stat REPLY_FILE;
     read(REPLY_FILE, my $reply, $replysize);
     close REPLY_FILE;

     # send reply.xml to the client
     my $cgi = new CGI;
     print $cgi->header(-type=>'text/xml');
     print $reply;

     # Clean up
     unlink "request.xml";
     unlink "reply.xml";
     unlink "temp.txt";

I think I can also handle most of the client side. What I don't know
how to do
is open a two way connection with the server. Do I do something like
this:

     open CONNECTION "http://www.myserver.com/cgi-bin/generate-reply"

Where can I find some example code?

Thanks,

Dan Timis
Muse Research, Inc.