#!/usr/bin/perl -w use IO::Socket; $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "localhost", PeerPort => "daytime(13)", ) or die "cannot connect to daytime port at localhost"; while ( <$remote> ) { print }
When you run this program, you should get something back that looks like this:
Wed May 14 08:40:46 MDT 1997
Here are what those parameters to the new constructor mean:
"www.perl.com"
, or an address like "204.148.40.9"
. For demonstration purposes, we've used the special hostname "localhost"
, which should always mean the current machine you're running on. The
corresponding Internet address for localhost is "127.1"
, if you'd rather use that.
"daytime"
on systems with a well-configured system services file,[FOOTNOTE: The
system services file is in /etc/services under Unix] but just in case, we've specified the port number (13) in
parentheses. Using just the number would also have worked, but constant
numbers make careful programmers nervous.
while
loop? That's what's called an indirect filehandle, a scalar variable
containing a filehandle. You can use it the same way you would a normal
filehandle. For example, you can read one line from it this way:
$line = <$handle>;
all remaining lines from is this way:
@lines = <$handle>;
and send a line of data to it this way:
print $handle "some data\n";