Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

DOS program interface

by Dovkont (Novice)
on Oct 04, 2003 at 14:54 UTC ( [id://296515]=perlquestion: print w/replies, xml ) Need Help??

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


Sorry for maybe a kind of stupid question, but I (with very little previous knowledge of Perl) have to create an web interface to a simple DOS program (that uses standard command line I/O) running under IIS on XP. The problem is that there have to be running a number of instances of this program, and that the "system" command refers to the program only during the run of the script, and when the forms is refreshed, it does not recognize the program and it's prompt anymore, creating a new one.... Is there any way (assuming using pipes, but unknown to me how) to make this working, providing there is no way to change the program itself? If possible, please send the answer to

Thanks in advance :)

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: DOS program interface
by jonadab (Parson) on Oct 04, 2003 at 15:17 UTC

    I'm not certain I understand what you want, but I think what you're saying is that you want one instance of the DOS app to keep running, and you want the user to interact with it through a CGI script that they can access multiple times, each time interacting with the same already-running process is that right? If so, you'll actually need two Perl scripts, which can communicate with one another locally via a socket or something. The one script will monitor the DOS program directly, and the other one will be the CGI script that interacts with the user. The CGI script will run each time, send the user's input to the other script, get the results, send them to the browser, and exit. This may be more than you want to undertake with little previous knowledge of Perl, however.

    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
      I did something sort of like this recently. What I ended up doing was writing a simple forking TCP server using Socket::IO::Inet which used expect to control the actual program. Then my second program, the cgi, just opened a tcp connection to whatever port my server was listening on, sent commands and read data.
      Thank you very much for the reply. Although, there is a slight problem with this... That is it: "communicate with one another locally via a socket or something"... Is there any simple description (and a sample, preferrably) of HOW to make such communication?

        I believe I have an example of socket communication sitting around here someplace...

        Yes, here it is. Bear in mind that I wrote this a long time ago and haven't looked at it recently, and also that I didn't know Perl so well then as now. In addition to adapting the server part to talk to the DOS program, you'll also need to make changes to the path and file names and hostname to make sense on your system, and also you'll probably want to adjust them so that the information they communicate is something more relevant to your problem than "Hello, World", but the basic stuff is here. Here's the server part...

        #!/usr/bin/perl # Hello World Server, by Jonadab the Unsightly One A demonstration of # sockets. Listens on port 5 for connections made by the # client, and answers them with a Hello World response, thus # demonstrating sockets. The system running the server # must have port 5 open. (Alternately, both the server and client # could be altered to use another port, but the server must listen on # the same port the client will connect to.) To stop the server, # delete the PID file. The server will then exit after answering the # next request. So, to terminate it immediately, after deleting the # PID file, run the client one final time to send the last request. # Be sure to change the $serverhost and $serverlog variables to # soemthing that makes sense on your system. $serverhost = 'raptor1'; $serverport = 5; $serverlog = '/var/log/hellows.log'; $pidfile = "/var/lock/"; require 5.002; use sigtrap; use Socket; use IO::Socket; open LOG, ">>$serverlog" or die "Unable to append to server log $serverlog : $!"; open PID, ">$pidfile" or die "Unable to write to PID file +: $!\n"; { print PID "$$\n"; close PID; } my $sock = new IO::Socket::INET ( LocalHost => $serverhost, LocalPort => $serverport, Proto => 'tcp', Listen => 1, Reuse => 5, ); if ($sock) { print "Hello World Server is running, waiting for client connections +...\n"; while (-e "") { my $hellowsock = $sock->accept(); $_ = <$hellowsock>; { if (/[Hh]ello.*[Ww]orld.*[?]/) { print "..."; $canswered++; if ($canswered % 20 == 0) { print "\n +"; } print LOG localtime(time) . "\tAnswering Hello World request.\n"; print $hellowsock "Hello, client world! The server is here!\n"; } } } close($sock); } else { print LOG localtime(time) . "\tDIED: Unable to create socket: $!\n" +; die "Hello World Server unable to create socket: $!\n"; } print "\nAnswered $canswered requests from Hello World clients this se +ssion.\n";

        And here's the client script...

        #/usr/bin/perl -w # Hello World Client, by Jonadab the Unsightly One # Give it an IP address of a machine running the # server, and it will query that server # for a Hello World response, demonstrating sockets. require 5.002; use sigtrap; use Socket; $serverhost = 'raptor1'; $serverport = 5; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => $serverhost, PeerPort => $serverport, Proto => 'tcp', ); die "Hello World Client unable to create socket: $!\n" unless $sock; print $sock "Hello, are you there, server world?\n"; print <$sock>; close($sock);

        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: DOS program interface (slightly off-subject)
by atcroft (Abbot) on Oct 05, 2003 at 00:26 UTC

    Welcome to the Monastery, Dovkont. I hope your stay here will be a productive and educational one.

    While your question sounds like a very interesting one, I will not try to answer it at the moment, because the answers I have seen posted look to be well on their way to pointing you in the right direction. I hope they provide the direction you need, and I (for one) would be interested in hearing how they help you solve the puzzle, or where they fell short, when you have had a chance to try them.

    After reading the above, you may be wondering why I have bothered to post. You mention that you have a limited knowledge of perl, but sound like you have a need or desire to learn perl. That being the case, I feel you have come to the right place. The Monastery has a great deal of resources to offer to one wanting to learn (or learn more) about perl. The Tutorials section is an excellent place to start, and one that I highly recommend, containing information both on modules and techniques in perl, as well as information on the resources and general expectations in the Monastery (at the very least, the posts under the Welcome to the Monastery section may prove helpful in learning your way around here). Beyond the Tutorials section, there is the Categorized Q&A section, where a number of commonly-encountered questions are answered, the SoWP section where most questions are asked, and the Super Search, where you can try to determine if something you are wondering about has been asked about before. There is also the Chatterbox (or CB as it is known), where discussions occur most any time of the day, and questions can be asked,

    I would caution you on thing or two, however.

    1. First of all, as the improvement of the body monktorate is a major goal here, asking for help in a way that would not further that goal (such as having the response sent back to you via email) is a bit frowned upon. jeffa points out an alternate method for being notified in his tutorial. It's not that uncommon an occurrance, but something to be aware of.
    2. Number two, the number of monks present at any time, and with a particular experience or skill set, varies by hour of the day and day of the week/month/year, so if you do not get a response immediately, don't fret too much about it.
    3. Lastly, the experience range herein is enormous, as is the range of personalities and styles. If you receive a terse or somewhat "cutting" response from someone, try to learn from it, but don't take it too personally. Most of the monks herein behave as professionals, and I cannot recall a time when I have personally observed a response (except perhaps from someone new at the time) that was an intentional personal attack.

    Again, welcome, and I hope your stay with be a productive, educational, and enjoyable one.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-17 02:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found