1IO::Socket(3pm)        Perl Programmers Reference Guide        IO::Socket(3pm)
2
3
4

NAME

6       IO::Socket - Object interface to socket communications
7

SYNOPSIS

9           use IO::Socket;
10

DESCRIPTION

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

CONSTRUCTOR

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

METHODS

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           send
50           recv
51           peername (getpeername)
52           sockname (getsockname)
53           shutdown
54
55       Some methods take slightly different arguments to those defined in
56       perlfunc in attempt to make the interface more flexible. These are
57
58       accept([PKG])
59           perform the system call "accept" on the socket and return a new
60           object. The new object will be created in the same class as the
61           listen socket, unless "PKG" is specified. This object can be used
62           to communicate with the client that was trying to connect.
63
64           In a scalar context the new socket is returned, or undef upon
65           failure. In a list context a two-element array is returned
66           containing the new socket and the peer address; the list will be
67           empty upon failure.
68
69           The timeout in the [PKG] can be specified as zero to effect a
70           "poll", but you shouldn't do that because a new IO::Select object
71           will be created behind the scenes just to do the single poll.  This
72           is horrendously inefficient.  Use rather true select() with a zero
73           timeout on the handle, or non-blocking IO.
74
75       socketpair(DOMAIN, TYPE, PROTOCOL)
76           Call "socketpair" and return a list of two sockets created, or an
77           empty list on failure.
78
79       Additional methods that are provided are:
80
81       atmark
82           True if the socket is currently positioned at the urgent data mark,
83           false otherwise.
84
85               use IO::Socket;
86
87               my $sock = IO::Socket::INET->new('some_server');
88               $sock->read($data, 1024) until $sock->atmark;
89
90           Note: this is a reasonably new addition to the family of socket
91           functions, so all systems may not support this yet.  If it is
92           unsupported by the system, an attempt to use this method will abort
93           the program.
94
95           The atmark() functionality is also exportable as sockatmark()
96           function:
97
98                   use IO::Socket 'sockatmark';
99
100           This allows for a more traditional use of sockatmark() as a
101           procedural socket function.  If your system does not support
102           sockatmark(), the "use" declaration will fail at compile time.
103
104       connected
105           If the socket is in a connected state, the peer address is
106           returned. If the socket is not in a connected state, undef is
107           returned.
108
109           Note that connected() considers a half-open TCP socket to be "in a
110           connected state".  Specifically, connected() does not distinguish
111           between the ESTABLISHED and CLOSE-WAIT TCP states; it returns the
112           peer address, rather than undef, in either case.  Thus, in general,
113           connected() cannot be used to reliably learn whether the peer has
114           initiated a graceful shutdown because in most cases (see below) the
115           local TCP state machine remains in CLOSE-WAIT until the local
116           application calls shutdown() or close(); only at that point does
117           connected() return undef.
118
119           The "in most cases" hedge is because local TCP state machine
120           behavior may depend on the peer's socket options. In particular, if
121           the peer socket has SO_LINGER enabled with a zero timeout, then the
122           peer's close() will generate a RST segment, upon receipt of which
123           the local TCP transitions immediately to CLOSED, and in that state,
124           connected() will return undef.
125
126       protocol
127           Returns the numerical number for the protocol being used on the
128           socket, if known. If the protocol is unknown, as with an AF_UNIX
129           socket, zero is returned.
130
131       sockdomain
132           Returns the numerical number for the socket domain type. For
133           example, for an AF_INET socket the value of &AF_INET will be
134           returned.
135
136       sockopt(OPT [, VAL])
137           Unified method to both set and get options in the SOL_SOCKET level.
138           If called with one argument then getsockopt is called, otherwise
139           setsockopt is called.
140
141       getsockopt(LEVEL, OPT)
142           Get option associated with the socket. Other levels than SOL_SOCKET
143           may be specified here.
144
145       setsockopt(LEVEL, OPT, VAL)
146           Set option associated with the socket. Other levels than SOL_SOCKET
147           may be specified here.
148
149       socktype
150           Returns the numerical number for the socket type. For example, for
151           a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
152
153       timeout([VAL])
154           Set or get the timeout value (in seconds) associated with this
155           socket.  If called without any arguments then the current setting
156           is returned. If called with an argument the current setting is
157           changed and the previous value returned.
158

LIMITATIONS

160       On some systems, for an IO::Socket object created with new_from_fd(),
161       or created with accept() from such an object, the protocol(),
162       sockdomain() and socktype() methods may return undef.
163

SEE ALSO

165       Socket, IO::Handle, IO::Socket::INET, IO::Socket::UNIX
166

AUTHOR

168       Graham Barr.  atmark() by Lincoln Stein.  Currently maintained by the
169       Perl Porters.  Please report all bugs to <perlbug@perl.org>.
170
172       Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights
173       reserved.  This program is free software; you can redistribute it
174       and/or modify it under the same terms as Perl itself.
175
176       The atmark() implementation: Copyright 2001, Lincoln Stein
177       <lstein@cshl.org>.  This module is distributed under the same terms as
178       Perl itself.  Feel free to use, modify and redistribute it as long as
179       you retain the correct attribution.
180
181
182
183perl v5.28.2                      2018-11-01                   IO::Socket(3pm)
Impressum