home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / perl5 / Socket.z / Socket
Encoding:
Text File  |  1998-10-30  |  7.1 KB  |  199 lines

  1.  
  2.  
  3.  
  4. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C
  10.      socket.h defines and structure manipulators
  11.  
  12. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  13.          use Socket;
  14.  
  15.          $proto = getprotobyname('udp');
  16.          socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
  17.          $iaddr = gethostbyname('hishost.com');
  18.          $port = getservbyname('time', 'udp');
  19.          $sin = sockaddr_in($port, $iaddr);
  20.          send(Socket_Handle, 0, 0, $sin);
  21.  
  22.          $proto = getprotobyname('tcp');
  23.          socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
  24.          $port = getservbyname('smtp');
  25.          $sin = sockaddr_in($port,inet_aton("127.1"));
  26.          $sin = sockaddr_in(7,inet_aton("localhost"));
  27.          $sin = sockaddr_in(7,INADDR_LOOPBACK);
  28.          connect(Socket_Handle,$sin);
  29.  
  30.          ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
  31.          $peer_host = gethostbyaddr($iaddr, AF_INET);
  32.          $peer_addr = inet_ntoa($iaddr);
  33.  
  34.          $proto = getprotobyname('tcp');
  35.          socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
  36.          unlink('/tmp/usock');
  37.          $sun = sockaddr_un('/tmp/usock');
  38.          connect(Socket_Handle,$sun);
  39.  
  40.  
  41. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  42.      This module is just a translation of the C _s_o_c_k_e_t._h file.  Unlike the old
  43.      mechanism of requiring a translated _s_o_c_k_e_t._p_h file, this uses the hhhh2222xxxxssss
  44.      program (see the Perl source distribution) and your native C compiler.
  45.      This means that it has a far more likely chance of getting the numbers
  46.      right.  This includes all of the commonly used pound-defines like
  47.      AF_INET, SOCK_STREAM, etc.
  48.  
  49.      In addition, some structure manipulation functions are available:
  50.  
  51.      inet_aton HOSTNAME
  52.           Takes a string giving the name of a host, and translates that to the
  53.           4-byte string (structure). Takes arguments of both the
  54.           'rtfm.mit.edu' type and '18.181.0.24'. If the host name cannot be
  55.           resolved, returns undef. For multi-homed hosts (hosts with more than
  56.           one address), the first address found is returned.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  71.  
  72.  
  73.  
  74.      inet_ntoa IP_ADDRESS
  75.           Takes a four byte ip address (as returned by _i_n_e_t__a_t_o_n()) and
  76.           translates it into a string of the form 'd.d.d.d' where the 'd's are
  77.           numbers less than 256 (the normal readable four dotted number
  78.           notation for internet addresses).
  79.  
  80.      INADDR_ANY
  81.           Note: does not return a number, but a packed string.
  82.  
  83.           Returns the 4-byte wildcard ip address which specifies any of the
  84.           hosts ip addresses. (A particular machine can have more than one ip
  85.           address, each address corresponding to a particular network
  86.           interface. This wildcard address allows you to bind to all of them
  87.           simultaneously.)  Normally equivalent to _i_n_e_t__a_t_o_n('0.0.0.0').
  88.  
  89.      INADDR_BROADCAST
  90.           Note: does not return a number, but a packed string.
  91.  
  92.           Returns the 4-byte 'this-lan' ip broadcast address.  This can be
  93.           useful for some protocols to solicit information from all servers on
  94.           the same LAN cable.  Normally equivalent to
  95.           _i_n_e_t__a_t_o_n('255.255.255.255').
  96.  
  97.      INADDR_LOOPBACK
  98.           Note - does not return a number.
  99.  
  100.           Returns the 4-byte loopback address. Normally equivalent to
  101.           _i_n_e_t__a_t_o_n('localhost').
  102.  
  103.      INADDR_NONE
  104.           Note - does not return a number.
  105.  
  106.           Returns the 4-byte 'invalid' ip address. Normally equivalent to
  107.           _i_n_e_t__a_t_o_n('255.255.255.255').
  108.  
  109.      sockaddr_in PORT, ADDRESS
  110.  
  111.      sockaddr_in SOCKADDR_IN
  112.           In an array context, unpacks its SOCKADDR_IN argument and returns an
  113.           array consisting of (PORT, ADDRESS).  In a scalar context, packs its
  114.           (PORT, ADDRESS) arguments as a SOCKADDR_IN and returns it.  If this
  115.           is confusing, use _p_a_c_k__s_o_c_k_a_d_d_r__i_n() and _u_n_p_a_c_k__s_o_c_k_a_d_d_r__i_n()
  116.           explicitly.
  117.  
  118.      pack_sockaddr_in PORT, IP_ADDRESS
  119.           Takes two arguments, a port number and a 4 byte IP_ADDRESS (as
  120.           returned by _i_n_e_t__a_t_o_n()). Returns the sockaddr_in structure with
  121.           those arguments packed in with AF_INET filled in.  For internet
  122.           domain sockets, this structure is normally what you need for the
  123.           arguments in _b_i_n_d(), _c_o_n_n_e_c_t(), and _s_e_n_d(), and is also returned by
  124.           _g_e_t_p_e_e_r_n_a_m_e(), _g_e_t_s_o_c_k_n_a_m_e() and _r_e_c_v().
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  137.  
  138.  
  139.  
  140.      unpack_sockaddr_in SOCKADDR_IN
  141.           Takes a sockaddr_in structure (as returned by _p_a_c_k__s_o_c_k_a_d_d_r__i_n())
  142.           and returns an array of two elements: the port and the 4-byte ip-
  143.           address.  Will croak if the structure does not have AF_INET in the
  144.           right place.
  145.  
  146.      sockaddr_un PATHNAME
  147.  
  148.      sockaddr_un SOCKADDR_UN
  149.           In an array context, unpacks its SOCKADDR_UN argument and returns an
  150.           array consisting of (PATHNAME).  In a scalar context, packs its
  151.           PATHNAME arguments as a SOCKADDR_UN and returns it.  If this is
  152.           confusing, use _p_a_c_k__s_o_c_k_a_d_d_r__u_n() and _u_n_p_a_c_k__s_o_c_k_a_d_d_r__u_n()
  153.           explicitly.  These are only supported if your system has <_s_y_s/_u_n._h>.
  154.  
  155.      pack_sockaddr_un PATH
  156.           Takes one argument, a pathname. Returns the sockaddr_un structure
  157.           with that path packed in with AF_UNIX filled in. For unix domain
  158.           sockets, this structure is normally what you need for the arguments
  159.           in _b_i_n_d(), _c_o_n_n_e_c_t(), and _s_e_n_d(), and is also returned by
  160.           _g_e_t_p_e_e_r_n_a_m_e(), _g_e_t_s_o_c_k_n_a_m_e() and _r_e_c_v().
  161.  
  162.      unpack_sockaddr_un SOCKADDR_UN
  163.           Takes a sockaddr_un structure (as returned by _p_a_c_k__s_o_c_k_a_d_d_r__u_n())
  164.           and returns the pathname.  Will croak if the structure does not have
  165.           AF_UNIX in the right place.
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.