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

Howdy, I've been working on a way to accept input from a TCP socket in an interactive session. Remember the old game Ledgend of the Red Dragon? Well I've for a nice copy running on a linux server but somehow I need to accept incoming TCP (multi connections) and send that data to the L.O.R.D. session. The weenie TCP server's I've built so far take input, but all output and threads go to STDIN/OUT on my console rather than on my test-telnet session. Does this even make sence? Thanks for the advice whatever it maybe. terron@terron.com

Replies are listed 'Best First'.
Re: TCP Server Socket Question/Prob
by tadman (Prior) on Jul 11, 2001 at 18:11 UTC
    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.
      elite! that code seems to have helped out.. but ya, i gotta tweek it a lil. thanks!
Re: TCP Server Socket Question/Prob
by bikeNomad (Priest) on Jul 11, 2001 at 17:48 UTC
    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.