If the program takes input from STDIN and outputs to STDOUT,
the "cheap" way is to just put your program in /etc/inetd.conf,
if you are using inetd, or by using something more sophisticated
and secure like 'tcpserver' from Dan Bernstein
a.k.a. DJB.
For a more "Perly" approach, you have to redirect both
STDIN and STDOUT to the socket:
!/usr/bin/perl
use strict;
use IO::Socket::INET;
my $sock = new IO::Socket::INET (
LocalPort => 5555,
Listen => 128,
Type => SOCK_STREAM,
Proto => 'tcp'
);
unless ($sock)
{
die "Could not create socket. Port might be in use.\n";
}
for(;;)
{
my $new_sock = $sock->accept();
if ($new_sock)
{
unless (fork())
{
$new_sock->autoflush(1);
open (STDOUT, "<&=".$new_sock->fileno());
open (STDIN, "<&=".$new_sock->fileno());
exec ("program");
}
}
}
You will note that this code has some "issues" which can
be resolved by tweaking your socket to be more like a
terminal. This, as they say, is left as an exercise for
the reader. | [reply] [d/l] |
elite! that code seems to have helped out.. but ya, i gotta tweek it a lil. thanks!
| [reply] |
You can put multiple handles into an IO::Select object, so you can include (for instance) STDIN along with your TCP sockets and get notified when there's either console input or TCP input ready to read. There are also modules like Event and POE that provide an event loop that can include IO, and may make it easier to write such a program.There is a Net::Telnet module that can make your life easier if you're dealing with a remote host over Telnet. | [reply] |