in reply to writing to telnet?

I wrote these as guidelines. Please do not feel intimidated, it's just that I want to provide as much feedback as I can. By all means, please ask more questions if you've got them. A few thoughts:

This is a version I adapted from your snippet and that incorporates what I say. It can read and write fine from as many clients as your machine supports.

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $server = IO::Socket::INET->new ( LocalPort => 1337, Type => SOCK_STREAM, Reuse => 1, Listen => 5 ) or die "could not open port\n"; warn "server ready waiting for connections..... \n"; my $client; while ($client = $server->accept()) { my $pid; while (not defined ($pid = fork())) { sleep 5; } if ($pid) { close $client; # Only meaningful in the client } else { $client->autoflush(1); # Always a good idea close $server; &do_your_stuff(); } } sub do_your_stuff { warn "client connected to pid $$\n"; while(my $line = <$client>) { print "client> ", $line; print $client "pid $$ > ", $line; } exit 0; }

Update: Thanks (and ++) to chromatic for correcting my confusion with Perl's fork() and C's. Looks like this mistake is going to live with me for a lot of time...

Best regards

-lem, but some call me fokat

Replies are listed 'Best First'.
Re: Re: writing to telnet?
by chromatic (Archbishop) on Mar 10, 2003 at 00:30 UTC
    note that the return value of fork() is always defined. Errors are signalled by returning -1.

    Which version of Perl is this? I've just checked the documentation for 5.6.0, 5.8.0, and 5.5.3, and they all claim to return undef on failure.

Re: Re: writing to telnet?
by pg (Canon) on Mar 09, 2003 at 19:26 UTC
    Wow, very neat, both the rules and the code.

    Only one small thing, autoflush is turned on by default, unless someone is using versions earlier than 1.18. The module coming with AS5.8.0 is version 1.26.