gethostbyaddr() - Network Utility Functions

This section provides a tutorial example on how to use network utility functions to get default port numbers of standard network services, or get the host name on the network by a given IP address.

getprotobyname($protoName) - Returns the protocol number of the specified protocol name in scalar context, and ($protoName,$aliases,$protoNumber) in list context.

getservbyname($serviceName,$protoName) - Returns the port number of the specified network service name and protocol name in scalar context, and ($serviceName,$aliases,$portNumber,$protoName) in list context.

getservbyport($portNumber,$protoName) - Returns the network service name of the specified port number and protocol name in scalar context, and ($serviceName,$aliases,$portNumber,$protoName) in list context.

gethostbyname($hostName) - Returns the packed IP address of the specified host name in scalar context, and ($name,$aliases,$addrtype,$length,@addrs) in list context. The host name can also be specified in IP address format.

gethostbyaddr($packedHostAddress) - Returns the host name of the specified packed IP address in scalar context, and ($name,$aliases,$addrtype,$length,@addrs) in list context.

Here is sample program to show you how these functions work:

#- NetworkInfo.pm
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   $s = getprotobyname('tcp');
   @l = getprotobyname('tcp');
   print "\ngetprotobyname():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   $s = getservbyname('ftp', 'tcp');
   @l = getservbyname('ftp', 'tcp');
   print "\ngetservbyname():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   $s = getservbyport(23, 'tcp');
   @l = getservbyport(23, 'tcp');
   print "\ngetservbyport():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   $s = gethostbyname('www.perl.org');
   @l = gethostbyname('www.perl.org');
   print "\ngetservbyname():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   $s = gethostbyname('216.109.118.67');
   @l = gethostbyname('216.109.118.67');
   print "\ngetservbyname():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   $domain = 2;
   $s = gethostbyaddr(pack('C4', 216,109,118,67), $domain);
   @l = gethostbyaddr(pack('C4', 216,109,118,67), $domain);
   print "\ngethostbyaddr():\n";
   print "   scalar = ", $s, "\n";
   print "   list = (", join(',',@l), ")\n";
   exit;

Output:

getprotobyname():
   scalar = 6
   list = (tcp,TCP,6)

getservbyname():
   scalar = 21
   list = (ftp,,21,tcp)

getservbyport():
   scalar = telnet
   list = (telnet,,23,tcp)

getservbyname():
   scalar = ?v  
   list = (x2.develooper.com,www.perl.org,2,4,?v  )

getservbyname():
   scalar = +mvC
   list = (216.109.118.67,,2,4,+mvC)

gethostbyaddr():
   scalar = p4.www.dcn.yahoo.com
   list = (p4.www.dcn.yahoo.com,,2,4,+mvC)

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

Socket Communication Over the Internet

 What Is Socket Communication?

 connect() - Establishing a Socket Communication

 ReverseEchoer.pl - A Simple Socket Server Program

 SocketClient.pl - A Simple Socket Client Program

gethostbyaddr() - Network Utility Functions

 Socket.pm - The Socket Module

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version