5 Socket - load the C socket.h defines and structure manipulators
11 $proto = (getprotobyname('udp'))[2];
12 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
13 $sockaddr_in = pack_sockaddr_in(AF_INET,7,inet_aton("localhost"));
14 $sockaddr_in = pack_sockaddr_in(AF_INET,7,INADDR_LOOPBACK);
15 connect(Socket_Handle,$sockaddr_in);
16 $peer = inet_ntoa((unpack_sockaddr_in(getpeername(Socket_Handle)))[2]);
21 This module is just a translation of the C F<socket.h> file.
22 Unlike the old mechanism of requiring a translated F<socket.ph>
23 file, this uses the B<h2xs> program (see the Perl source distribution)
24 and your native C compiler. This means that it has a
25 far more likely chance of getting the numbers right.
27 In addition, some structure manipulation functions are available:
29 =item inet_aton HOSTNAME
31 Takes a string giving the name of a host, and translates that
32 to the 4-byte string (structure). Takes arguments of both
33 the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
34 cannot be resolved, returns undef.
36 =item inet_ntoa IP_ADDRESS
38 Takes a four byte ip address (as returned by inet_aton())
39 and translates it into a string of the form 'd.d.d.d'
40 where the 'd's are numbers less than 256 (the normal
41 readable four dotted number notation for internet addresses).
45 Note - does not return a number.
47 Returns the 4-byte wildcard ip address which specifies any
48 of the hosts ip addresses. (A particular machine can have
49 more than one ip address, each address corresponding to
50 a particular network interface. This wildcard address
51 allows you to bind to all of them simultaneously.)
52 Normally equivalent to inet_aton('0.0.0.0').
56 Note - does not return a number.
58 Returns the 4-byte loopback address. Normally equivalent
59 to inet_aton('localhost').
63 Note - does not return a number.
65 Returns the 4-byte invalid ip address. Normally equivalent
66 to inet_aton('255.255.255.255').
68 =item pack_sockaddr_in FAMILY, PORT, IP_ADDRESS
70 Takes three arguments, an address family (normally AF_INET),
71 a port number, and a 4 byte IP_ADDRESS (as returned by
72 inet_aton()). Returns the sockaddr_in structure with those
73 arguments packed in. For internet domain sockets, this structure
74 is normally what you need for the arguments in bind(), connect(),
75 and send(), and is also returned by getpeername(), getsockname()
78 =item unpack_sockaddr_in SOCKADDR_IN
80 Takes a sockaddr_in structure (as returned by pack_sockaddr_in())
81 and returns an array of three elements: the address family,
82 the port, and the 4-byte ip-address.
91 @ISA = qw(Exporter DynaLoader);
93 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
94 INADDR_ANY INADDR_LOOPBACK INADDR_NONE
176 ($constname = $AUTOLOAD) =~ s/.*:://;
177 $val = constant($constname, @_ ? $_[0] : 0);
179 if ($! =~ /Invalid/) {
180 $AutoLoader::AUTOLOAD = $AUTOLOAD;
181 goto &AutoLoader::AUTOLOAD;
184 ($pack,$file,$line) = caller;
185 croak "Your vendor has not defined Socket macro $constname, used";
188 eval "sub $AUTOLOAD { $val }";
194 # Preloaded methods go here. Autoload methods go after __END__, and are
195 # processed by the autosplit program.