1Socket(3pm) Perl Programmers Reference Guide Socket(3pm)
2
3
4
6 Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton,
7 inet_ntop - load the C socket.h defines and structure manipulators
8
10 use Socket;
11
12 $proto = getprotobyname('udp');
13 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
14 $iaddr = gethostbyname('hishost.com');
15 $port = getservbyname('time', 'udp');
16 $sin = sockaddr_in($port, $iaddr);
17 send(Socket_Handle, 0, 0, $sin);
18
19 $proto = getprotobyname('tcp');
20 socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
21 $port = getservbyname('smtp', 'tcp');
22 $sin = sockaddr_in($port,inet_aton("127.1"));
23 $sin = sockaddr_in(7,inet_aton("localhost"));
24 $sin = sockaddr_in(7,INADDR_LOOPBACK);
25 connect(Socket_Handle,$sin);
26
27 ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
28 $peer_host = gethostbyaddr($iaddr, AF_INET);
29 $peer_addr = inet_ntoa($iaddr);
30
31 $proto = getprotobyname('tcp');
32 socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
33 unlink('/var/run/usock');
34 $sun = sockaddr_un('/var/run/usock');
35 connect(Socket_Handle,$sun);
36
38 This module is just a translation of the C socket.h file. Unlike the
39 old mechanism of requiring a translated socket.ph file, this uses the
40 h2xs program (see the Perl source distribution) and your native C
41 compiler. This means that it has a far more likely chance of getting
42 the numbers right. This includes all of the commonly used pound-
43 defines like AF_INET, SOCK_STREAM, etc.
44
45 Also, some common socket "newline" constants are provided: the
46 constants "CR", "LF", and "CRLF", as well as $CR, $LF, and $CRLF, which
47 map to "\015", "\012", and "\015\012". If you do not want to use the
48 literal characters in your programs, then use the constants provided
49 here. They are not exported by default, but can be imported
50 individually, and with the ":crlf" export tag:
51
52 use Socket qw(:DEFAULT :crlf);
53
54 In addition, some structure manipulation functions are available:
55
56 inet_aton HOSTNAME
57 Takes a string giving the name of a host, and translates that to an
58 opaque string (if programming in C, struct in_addr). Takes
59 arguments of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the
60 host name cannot be resolved, returns undef. For multi-homed hosts
61 (hosts with more than one address), the first address found is
62 returned.
63
64 For portability do not assume that the result of inet_aton() is 32
65 bits wide, in other words, that it would contain only the IPv4
66 address in network order.
67
68 inet_ntoa IP_ADDRESS
69 Takes a string (an opaque string as returned by inet_aton(), or a
70 v-string representing the four octets of the IPv4 address in
71 network order) and translates it into a string of the form
72 'd.d.d.d' where the 'd's are numbers less than 256 (the normal
73 human-readable four dotted number notation for Internet addresses).
74
75 INADDR_ANY
76 Note: does not return a number, but a packed string.
77
78 Returns the 4-byte wildcard ip address which specifies any of the
79 hosts ip addresses. (A particular machine can have more than one
80 ip address, each address corresponding to a particular network
81 interface. This wildcard address allows you to bind to all of them
82 simultaneously.) Normally equivalent to inet_aton('0.0.0.0').
83
84 INADDR_BROADCAST
85 Note: does not return a number, but a packed string.
86
87 Returns the 4-byte 'this-lan' ip broadcast address. This can be
88 useful for some protocols to solicit information from all servers
89 on the same LAN cable. Normally equivalent to
90 inet_aton('255.255.255.255').
91
92 INADDR_LOOPBACK
93 Note - does not return a number.
94
95 Returns the 4-byte loopback address. Normally equivalent to
96 inet_aton('localhost').
97
98 INADDR_NONE
99 Note - does not return a number.
100
101 Returns the 4-byte 'invalid' ip address. Normally equivalent to
102 inet_aton('255.255.255.255').
103
104 sockaddr_family SOCKADDR
105 Takes a sockaddr structure (as returned by pack_sockaddr_in(),
106 pack_sockaddr_un() or the perl builtin functions getsockname() and
107 getpeername()) and returns the address family tag. It will match
108 the constant AF_INET for a sockaddr_in and AF_UNIX for a
109 sockaddr_un. It can be used to figure out what unpacker to use for
110 a sockaddr of unknown type.
111
112 sockaddr_in PORT, ADDRESS
113 sockaddr_in SOCKADDR_IN
114 In a list context, unpacks its SOCKADDR_IN argument and returns an
115 array consisting of (PORT, ADDRESS). In a scalar context, packs
116 its (PORT, ADDRESS) arguments as a SOCKADDR_IN and returns it. If
117 this is confusing, use pack_sockaddr_in() and unpack_sockaddr_in()
118 explicitly.
119
120 pack_sockaddr_in PORT, IP_ADDRESS
121 Takes two arguments, a port number and an opaque string, IP_ADDRESS
122 (as returned by inet_aton(), or a v-string). Returns the
123 sockaddr_in structure with those arguments packed in with AF_INET
124 filled in. For Internet domain sockets, this structure is normally
125 what you need for the arguments in bind(), connect(), and send(),
126 and is also returned by getpeername(), getsockname() and recv().
127
128 unpack_sockaddr_in SOCKADDR_IN
129 Takes a sockaddr_in structure (as returned by pack_sockaddr_in())
130 and returns an array of two elements: the port and an opaque string
131 representing the IP address (you can use inet_ntoa() to convert the
132 address to the four-dotted numeric format). Will croak if the
133 structure does not have AF_INET in the right place.
134
135 sockaddr_un PATHNAME
136 sockaddr_un SOCKADDR_UN
137 In a list context, unpacks its SOCKADDR_UN argument and returns an
138 array consisting of (PATHNAME). In a scalar context, packs its
139 PATHNAME arguments as a SOCKADDR_UN and returns it. If this is
140 confusing, use pack_sockaddr_un() and unpack_sockaddr_un()
141 explicitly. These are only supported if your system has
142 <sys/un.h>.
143
144 pack_sockaddr_un PATH
145 Takes one argument, a pathname. Returns the sockaddr_un structure
146 with that path packed in with AF_UNIX filled in. For unix domain
147 sockets, this structure is normally what you need for the arguments
148 in bind(), connect(), and send(), and is also returned by
149 getpeername(), getsockname() and recv().
150
151 unpack_sockaddr_un SOCKADDR_UN
152 Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
153 and returns the pathname. Will croak if the structure does not
154 have AF_UNIX in the right place.
155
156 inet_pton ADDRESS_FAMILY, HOSTNAME
157 Takes an address family, either AF_INET or AF_INET6, and a string
158 giving the name of a host, and translates that to an opaque string
159 (if programming in C, struct in_addr or struct in6_addr depending
160 on the address family passed in). The host string may be a string
161 hostname, such as 'www.perl.org', or an IP address. If using an IP
162 address, the type of IP address must be consistant with the address
163 family passed into the function.
164
165 This function is not exported by default.
166
167 inet_ntop ADDRESS_FAMILY, IP_ADDRESS
168 Takes an address family, either AF_INET or AF_INET6, and a string
169 (an opaque string as returned by inet_aton() or inet_pton()) and
170 translates it to an IPv4 or IPv6 address string.
171
172 This function is not exported by default.
173
174
175
176perl v5.12.4 2011-06-07 Socket(3pm)