Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Whether you’re a UNIX or Windows user, every time you do anything on a TCP/IP network, including the Internet, you’re using sockets. The sockets library quickly became part of the standard UNIX distribution and it’s been there ever since. When Microsoft developers realized in the late 1980s that Windows would need a TCP/IP driver to plug into the Internet, they came up with an application programming interface (API) called Winsock, which, except for some functions that had to be specific to Windows, contains most of the same operations as the Berkeley sockets library.


TIP:  Microsoft took great care to attempt to duplicate the Berkeley sockets functions in their Winsock API. As a result, except for some initialization functions that are required by Windows, it is generally not too difficult to port UNIX networking programs to Windows and vice versa.

Now, just because UNIX and Windows have sockets libraries doesn’t mean that network programming is any less arcane and frustrating. Most of the things you can do with sockets are far beyond the scope of this book. Although the Perl standard library includes all of the UNIX sockets functions, programs that deal regularly with networking really should be written in a compiled language because it’s faster.

As usual, however, Perl is dandy for small, quick network tasks. Let’s take a look at one.

Dealing with Internet Addresses

Internet addresses come in two parts, both of which are intended primarily for human consumption, but only one of which—with some manipulation—is meaningful to computers.

An Internet address is most familiar to you as a domain name such as www.alfredpacker.com. However, the name means absolutely nothing to your computer or anyone else’s. It has to be associated with a numeric IP address before any software can find it. You have an example of the association on your computer, called the hosts file.


TIP:  The hosts file on a UNIX system is /etc/hosts. On Windows NT, you’ll find it in /winnt/system32/drivers/etc/hosts. On Windows 95, it’s in /windows/hosts.

Here is an example of a line from a typical hosts file:

   127.0.0.1       localhost www.alfredpacker.com

Notice that the line is set up in two columns, the first containing an IP address and the second a list of domain names separated by spaces. In this example, the IP address is one that is reserved for local testing through the primary name localhost. The name that follows is known as an alias.

Your hosts file is just a text file and you can put anything in it that you like, so long as you follow the two-column, IP address-domain name convention on each line. Whenever you run programs that deal with domain names, the hosts file is the first place the software looks to find the IP address. If the address isn’t there, TCP/IP software checks the first domain name server in its internal list, which is usually on your Internet service provider’s computer.

As an example of how you can use the hosts file yourself, some large networks are shielded from the world by a device called a firewall, which keeps outsiders from getting in. Insiders have to go through the firewall to get out to the Internet. In one firewall scheme, if you wanted to use the TCP/IP utility telnet to sign on to a remote system, you would have to log in to the firewall first, and software there would ask you for the name of the remote computer and pass you over to it.

However, this security scheme forces you to remember the IP address of the firewall. If its address is, say, 10.64.198.7, you would have to type telnet 10.64.198.7 every time you ran it. But if you put a line in your hosts file:

   10.64.198.7    firewall

you could get away with entering telnet firewall and it would work just the same.


WARNING:  Beware of associating domain names that actually exist with internal or otherwise incorrect IP addresses in the hosts file. TCP/IP software only goes as far as it needs to in resolving domain names, and the hosts file is the first place it looks. If, for example, you jokingly aliased the localhosts domain to www.microsoft.com to test your Web server under that name, the next time you tried to get into the real Microsoft Web site, your own server would be as far as you would get.

IP addresses in the form familiar to you are still meaningless to computers. The address in your hosts file actually represents a four-byte whole number, with each of the bytes separated by periods. The familiar IP address is converted into a number by the TCP/IP software before it can be used as a real Internet address.

You can see exactly how this is done—and create a nice utility for yourself in the meantime—by writing a Perl program to look up names and addresses on the Internet.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.