[Techtalk] Perl Sockets Communication

Sean McAfee mcafee at transmeta.com
Wed Aug 22 15:40:54 EST 2001


I wrote:
>Kai MacTane <kmactane at GothPunk.com> wrote:
>>LOOP:
>>             for ( ; my $paddr = accept(Client, Sock); close Client)
>>             {
>>                 print CLIENT "Printing to client filehandle.\n";
>>
>>                 my $line = <CLIENT>;
>>                 chomp $line;
>>
>>                 &write_syslog('Client connection; content: "%s"', $line);
><snip>

>I haven't done a thorough test of your program, but here you're accepting a
>new connection on the handle 'Client', but referring to it in the body of
>the loop as 'CLIENT'.  Perl is case-sensitive, so these are different
>handles.

I neglected to mention that Perl can catch mistakes like this if warnings
are enabled, such as by changing the first line of your server script to:

#!/usr/bin/perl -w

Warnings are printed to STDERR, which might not be easy to see for a server
process, but you can easily redirect STDERR to a file:

open STDERR, ">/server/error/log" or die "Can't open error log: $!\n";
select STDERR;
$| = 1;
select STDOUT;

The last three lines above turn on autoflushing for STDERR.  (If this
isn't done, output to the log will be buffered, meaning you won't see any
output until a bunch of it has accumulated.)  There are prettier ways to
turn on autoflushing, but your program suggests that you might be using
an ancient, several-years-old version of Perl that might not support the
prettier ways, so I'm being conservative.


--Sean




More information about the Techtalk mailing list