Полезная информация

Learning Perl

Learning PerlSearch this book
Previous: 16.2 Packing and Unpacking Binary DataChapter 16
System Database Access
Next: 16.4 Exercise
 

16.3 Getting Network Information

Perl supports network programming in a way that is very familiar to those who have written network code in C programs. In fact, most of the Perl functions that provide network access have the same names and similar parameters as their C counterparts. We can't teach a complete course on network programming in this chapter, but let's look at one of the task fragments to see how it's done in Perl.

One of the things you need to find out is the address that goes with a name, or vice versa. In C, you use the gethostbyname (3) routine to convert a network name to a network address. You then use this address to create a connection from your program to another program somewhere else.

The Perl function to translate a hostname to an address has the same name and similar parameters as the C routine, and looks like this:

($name, $aliases, $addrtype, $length, @addrs) =
    gethostbyname($name); # generic form of gethostbyname

The parameter to this function is a hostname, e.g., slate.bedrock.com. The return value is a list of four or more parameters, depending on how many addresses are associated with the name. If the hostname is not valid, the function returns an empty list.

If gethostbyname is called in a scalar context, only the (first) address is returned.

When gethostbyname completes successfully, $name is the canonical name, which differs from the input name if the input name is an alias. $aliases are a list of space-separated names by which the host is also known. $addrtype gives a coded value to indicate the form of the addresses. In this case, for slate.bedrock.com, we can presume that the value indicates an IP address, usually represented as four numbers under 256, separated by dots. $length gives the number of addresses, which is actually redundant information since you can look at the length of @addrs anyway.

But the useful part of the return value is @addrs. Each element of the list is a separate IP address, stored in an internal format, handled in Perl as a four-character string.[2] While this four-character string is exactly what other Perl networking functions are looking for, suppose we wanted to print out the result for the user to see. In this case, we need to convert the return value into a human-readable format with the assistance of the unpack function and a little additional massaging. Here's code that prints one of slate.bedrock.com's IP addresses:

($addr) = (gethostbyname("slate.bedrock.com"))[4];
print "Slate's address is ",
    join(".",unpack("C4", $addr)), "\n";

[2] Well, at least until IPv6.

unpack takes the four-byte string and returns four numbers. These just happen to be in the right order for join to glue in a dot between each pair of numbers to make the human-readable form. See Appendix C, Networking Clients, for information about building simple networking clients.


Previous: 16.2 Packing and Unpacking Binary DataLearning PerlNext: 16.4 Exercise
16.2 Packing and Unpacking Binary DataBook Index16.4 Exercise