programming-examples/perl/Network/Daytime client, using symbolic host and service names.pl

17 lines
533 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/usr/bin/perl
use strict;
use Socket;
use constant DEFAULT_ADDR => 'your server';
my $packed_addr = gethostbyname(shift || DEFAULT_ADDR) or die "Can't look up host: $!";
my $protocol = getprotobyname('tcp');
my $port = getservbyname('daytime','tcp') or die "Can't look up port: $!";
my $destination = sockaddr_in($port,$packed_addr);
socket(SOCK,PF_INET,SOCK_STREAM,$protocol) or die "Can't make socket: $!";
connect(SOCK,$destination) or die "Can't connect: $!";
print <SOCK>;