程序清单17-4:client.pl
#!/usr/bin/perl
#
# define constants for talking to server
#
$PACKIT = 'S n C4 x8';
$AF_INET = 2;
$SOCK_STR = 1; # STREAMS
$DEF_PROTO = 0; #default protocol of IP
$port = 6668 unless $port;
#
# Get the name of the server
#
($name, $aliases, $addrtype, $len, @addrs) = gethostbyname("www.ikra.com");
($a,$b,$c,$d) = unpack('C4',$addrs[0]);
print "Server Name=$name, Server Address= $a.$b.$c.$d\n";
$that = pack($PACKIT,$AF_INET,$port,$addrs[0]);
#
# Confine yourself to the local host machine address of 127.0.0.1
#
$this = pack($PACKIT,$AF_INET,$port,127,0,0,1);
#
# Disable buffering on this socket.
#
select(CLIENT_SOCKET);
$| = 1;
select(stdout);
socket(CLIENT_SOCKET,$AF_INET,$SOCK_STR,$DEF_PROTO)
or die "$0: Cannot open socket\n";
print "Created socket\n";
#
# Attempt to connect to server.
#
do
{
sleep(1);
$result = connect(CLIENT_SOCKET,$that);
i f (result != 1) {
print "Sleeping\n";
}
} while ($result != 1);
sleep(5);
print "After connection\n";
#
# send data to server
#
# write(CLIENT_SOCKET,"hello",5);
read(CLIENT_SOCKET,$buf,100);
print "[" . $buf . "]\n";
close(CLIENT_SOCKET);
# end of client program