1IO::Socket(3pm) Perl Programmers Reference Guide IO::Socket(3pm)
2
3
4
6 IO::Socket - Object interface to socket communications
7
9 use IO::Socket;
10
12 "IO::Socket" provides an object interface to creating and using
13 sockets. It is built upon the IO::Handle interface and inherits all the
14 methods defined by IO::Handle.
15
16 "IO::Socket" only defines methods for those operations which are common
17 to all types of socket. Operations which are specified to a socket in a
18 particular domain have methods defined in sub classes of "IO::Socket"
19
20 "IO::Socket" will export all functions (and constants) defined by
21 Socket.
22
24 new ( [ARGS] )
25 Creates an "IO::Socket", which is a reference to a newly created
26 symbol (see the "Symbol" package). "new" optionally takes
27 arguments, these arguments are in key-value pairs. "new" only
28 looks for one key "Domain" which tells new which domain the socket
29 will be in. All other arguments will be passed to the configuration
30 method of the package for that domain, See below.
31
32 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
33
34 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
35 by default. This was not the case with earlier releases.
36
37 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
38
40 See perlfunc for complete descriptions of each of the following
41 supported "IO::Socket" methods, which are just front ends for the
42 corresponding built-in functions:
43
44 socket
45 socketpair
46 bind
47 listen
48 accept
49 sockname (getsockname)
50 shutdown
51
52 Some methods take slightly different arguments to those defined in
53 perlfunc in attempt to make the interface more flexible. These are
54
55 accept([PKG])
56 perform the system call "accept" on the socket and return a new
57 object. The new object will be created in the same class as the
58 listen socket, unless "PKG" is specified. This object can be used
59 to communicate with the client that was trying to connect.
60
61 In a scalar context the new socket is returned, or undef upon
62 failure. In a list context a two-element array is returned
63 containing the new socket and the peer address; the list will be
64 empty upon failure.
65
66 The timeout in the [PKG] can be specified as zero to effect a
67 "poll", but you shouldn't do that because a new IO::Select object
68 will be created behind the scenes just to do the single poll. This
69 is horrendously inefficient. Use rather true select() with a zero
70 timeout on the handle, or non-blocking IO.
71
72 socketpair(DOMAIN, TYPE, PROTOCOL)
73 Call "socketpair" and return a list of two sockets created, or an
74 empty list on failure.
75
76 Additional methods that are provided are:
77
78 atmark
79 True if the socket is currently positioned at the urgent data mark,
80 false otherwise.
81
82 use IO::Socket;
83
84 my $sock = IO::Socket::INET->new('some_server');
85 $sock->read($data, 1024) until $sock->atmark;
86
87 Note: this is a reasonably new addition to the family of socket
88 functions, so all systems may not support this yet. If it is
89 unsupported by the system, an attempt to use this method will abort
90 the program.
91
92 The atmark() functionality is also exportable as sockatmark()
93 function:
94
95 use IO::Socket 'sockatmark';
96
97 This allows for a more traditional use of sockatmark() as a
98 procedural socket function. If your system does not support
99 sockatmark(), the "use" declaration will fail at compile time.
100
101 connected
102 If the socket is in a connected state, the peer address is
103 returned. If the socket is not in a connected state, undef is
104 returned.
105
106 Note that connected() considers a half-open TCP socket to be "in a
107 connected state". Specifically, connected() does not distinguish
108 between the ESTABLISHED and CLOSE-WAIT TCP states; it returns the
109 peer address, rather than undef, in either case. Thus, in general,
110 connected() cannot be used to reliably learn whether the peer has
111 initiated a graceful shutdown because in most cases (see below) the
112 local TCP state machine remains in CLOSE-WAIT until the local
113 application calls shutdown() or close(); only at that point does
114 connected() return undef.
115
116 The "in most cases" hedge is because local TCP state machine
117 behavior may depend on the peer's socket options. In particular, if
118 the peer socket has SO_LINGER enabled with a zero timeout, then the
119 peer's close() will generate a RST segment, upon receipt of which
120 the local TCP transitions immediately to CLOSED, and in that state,
121 connected() will return undef.
122
123 send(MSG, [, FLAGS [, TO ] ])
124 Like the built-in send(), except that:
125
126 · "FLAGS" is optional and defaults to 0, and
127
128 · after a successful send with "TO", further calls to send() on
129 an unconnected socket without "TO" will send to the same
130 address, and "TO" will be used as the result of peername().
131
132 recv(BUF, LEN, [,FLAGS])
133 Like the built-in recv(), except that:
134
135 · "FLAGS" is optional and defaults to 0, and
136
137 · the cached value returned by peername() is updated with the
138 result of recv().
139
140 peername
141 Returns the cached peername, possibly set by recv() or send()
142 above. If not otherwise set returns (and caches) the result of
143 getpeername().
144
145 protocol
146 Returns the numerical number for the protocol being used on the
147 socket, if known. If the protocol is unknown, as with an AF_UNIX
148 socket, zero is returned.
149
150 sockdomain
151 Returns the numerical number for the socket domain type. For
152 example, for an AF_INET socket the value of &AF_INET will be
153 returned.
154
155 sockopt(OPT [, VAL])
156 Unified method to both set and get options in the SOL_SOCKET level.
157 If called with one argument then getsockopt is called, otherwise
158 setsockopt is called.
159
160 getsockopt(LEVEL, OPT)
161 Get option associated with the socket. Other levels than SOL_SOCKET
162 may be specified here.
163
164 setsockopt(LEVEL, OPT, VAL)
165 Set option associated with the socket. Other levels than SOL_SOCKET
166 may be specified here.
167
168 socktype
169 Returns the numerical number for the socket type. For example, for
170 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
171
172 timeout([VAL])
173 Set or get the timeout value (in seconds) associated with this
174 socket. If called without any arguments then the current setting
175 is returned. If called with an argument the current setting is
176 changed and the previous value returned.
177
179 On some systems, for an IO::Socket object created with new_from_fd(),
180 or created with accept() from such an object, the protocol(),
181 sockdomain() and socktype() methods may return undef.
182
184 Socket, IO::Handle, IO::Socket::INET, IO::Socket::UNIX
185
187 Graham Barr. atmark() by Lincoln Stein. Currently maintained by the
188 Perl Porters. Please report all bugs to <perlbug@perl.org>.
189
191 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights
192 reserved. This program is free software; you can redistribute it
193 and/or modify it under the same terms as Perl itself.
194
195 The atmark() implementation: Copyright 2001, Lincoln Stein
196 <lstein@cshl.org>. This module is distributed under the same terms as
197 Perl itself. Feel free to use, modify and redistribute it as long as
198 you retain the correct attribution.
199
200
201
202perl v5.30.1 2019-11-29 IO::Socket(3pm)