I apologize to those who feel (with some justification) that this is
off-topic...
Using read() fails and using recv() or sysread() succeeds. I do have a
simple client and server that fail. By the way, I think that the free
version of ZA is ok, its the ZA Security Suite that is bad.
I am half suspicious that ZA and possibly other firewalls are mucking with
the text sent across, like changing the newline (this would explain the
hanging).
Here is a client and server that break. I grabbed them off this newsgroup
and they work on my developement machine, but fail on my test machine (by
having the client hang)...
======================
sockettest_srv.pl
#!/usr/bin/perl -w
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;
use IO::Socket; # for autoflush
my $EOL = "\015\012";
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
my $port = shift || 2345;
my $proto = getprotobyname('tcp');
($port) = $port =~ /^(\d+)$/ or die "invalid port";
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
pack("l", 1)) || die "setsockopt:
$!";
bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
listen(Server,SOMAXCONN) || die "listen: $!";
logmsg "server started on port $port";
my $paddr;
$SIG{CHLD} = \&REAPER;
for ( ; $paddr = accept(Client,Server); close Client)
{
autoflush Client 1;
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "connection from $name [",
inet_ntoa($iaddr), "]
at port $port";
print Client "From the server, hello there, $name, it's now ",
scalar localtime, $EOL;
}
======================
sockettest_cl.pl
#!/usr/bin/perl -w
use strict;
use Socket;
my ($remote,$port, $iaddr, $paddr, $proto, $line);
$remote = shift || 'localhost';
$port = shift || 2345; # random port
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
my $sockhand;
print "Socket open...\n";
socket($sockhand, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
print "Socket connect...\n";
connect($sockhand, $paddr) || die "connect: $!";
if (0)
{
print "recv(Socket) read lines...\n";
my $data = "";
while (defined (recv($sockhand, $data, 1024, 0)) && length($data) > 0)
{
print $data;
}
}
else
{
print "<Socket> read lines...\n";
while (defined($line = <$sockhand>))
{
print $line;
}
}
print "Socket close\n";
close ($sockhand) || die "close: $!";