Mailing List Archive

Re: Sockets on IRIX?
how does this work? remove the 'require' and the 'use strict' if
you don't have 5.001 yet.

--tom

#!/usr/bin/perl -w

require 5.001;
use strict;
use Socket;

my ($remote, # remote host in first arg, or else localhost
$port, # remote port in second arg, or else daytime
$iaddr, # packed remote addr; should be all of them though
$paddr, # packed up sockaddr_in version of iaddr+port
$proto, # tcp protocol
$nport,
$name,
$timeout,
);

( $name = $0 ) =~ s!.*/!!;
$name = 'daytime' if $name eq 'catsock';

$remote = shift || 'localhost';
$port = shift || $name;
$timeout = shift || 60;

alarm $timeout;

$iaddr = gethostbyname($remote) || die "no host $remote: $?";
$nport = getservbyname($port,'tcp') if $port =~ /\D/;
die "Bad port: $port" unless defined $nport;
$paddr = pack('S n a4 x8', AF_INET, $nport, $iaddr);
$proto = getprotobyname('tcp');

$SIG{ALRM} = sub {
my $basename;
($basename = $0) =~ s,.*/,,;
die "$basename: $remote timedout its $name service after $timeout second"
. ($timeout != 1 ? "s" : "") . ".\n";
};

socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
while (<SOCK>) {
print;
}