#!/usr/bin/perl -w # Perl Raw TCP client # http://www.perl.com/doc/manual/html/pod/perlipc.html # comments and modifications by -Ian! idallen@idallen.ca # # $0 [ hostname [ port ] ] # # This client connects to a remote TCP port and reads whatever lines come back. # (no user input is sent to the remote system) use strict; use Socket; # bounds of a TCP port number # my $LOWPORT = 0; my $HIPORT = 65535; my ($remote, $argport, $port, $iaddr, $paddr, $proto, $line); # first arg is host, second is port; both have defaults if missing # (Note: you can't specify port zero using the code below!) # $remote = shift || 'localhost'; $argport = shift || 55555; # hopefully unused dynamic port # if the arg port has only digits in it (more than zero), use it # otherwise, look it up in /etc/services # $port = $argport; $port = getservbyname($argport, 'tcp') if $argport !~ /^\d+$/; die "$0: unrecognized port '$argport'\n" unless $port; die "$0: out-of-range port '$port' not $LOWPORT-$HIPORT\n" if $port < $LOWPORT || $port > $HIPORT; print "Using host '$remote' and port '$port'\n"; # Perl inet_aton will do DNS-style (resolver) lookups on names for you! # $iaddr = inet_aton($remote) or die "$0: unknown host: '$remote'\n"; $paddr = sockaddr_in($port, $iaddr); # We could let the protocol default to TCP by specifying zero here. # (See "man 2 socket": "Normally only a single protocol exists to support # a particular socket type within a given protocol family, in which case # protocol can be specified as 0.") # $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "$0: socket error: $!\n"; connect(SOCK, $paddr) or die "$0: connect error: $!\n"; # this format doesn't detect or report read errors # while (defined($line = )) { print $line; } close(SOCK) || die "close: $!";