The following code fails, even if you have the echo server running:
#!/usr/bin/perl -w
use strict;
use Socket;
my
($remote, $port, $serv, $iaddr, $paddr, $proto_name, $proto,
$line, $an_ix, $loop_count, $do_shutdown);
# configuration
$loop_count = 01000; # set to 010000 to succeed
$do_shutdown = 01; # otherwise the programme will wait indefinitely
$proto_name = 'tcp';
($proto = getprotobyname ($proto_name)) || die "$proto_name: $!";
$remote = 'localhost';
($iaddr = inet_aton ($remote)) || die "$remote: $!";
$serv = 'echo';
($port = getservbyname ($serv, 'tcp')) || die "$serv: $!";
socket (SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect SOCK, sockaddr_in ($port, $iaddr) || die "connect: $!";
for ($an_ix = 0; $an_ix < $loop_count; ++$an_ix) { print SOCK 'a'; print '+'; }
shutdown SOCK, 01 if $do_shutdown; read (SOCK, $line, 01) >= 0 || die "read: $!";
die "fail: expected 'a', got '$line'" unless $line eq 'a';
close SOCK;
exit;
The reason is that shutdown does not flush the file handle representing the socket. Which is not what one would expect.
What do you think?