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 hellowc.pl
# client, and answers them with a Hello World response, thus
# demonstrating sockets. The system running the hellows.pl 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/hellows.pid";
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 hellows.pid
+: $!\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 "hellows.pid") {
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
# hellows.pl 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$/
|