3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
21 require IO::Socket::INET;
22 require IO::Socket::UNIX if ($^O ne 'epoc');
24 @ISA = qw(IO::Handle);
31 Exporter::export 'Socket', $callpkg, @_;
36 my $sock = $class->SUPER::new();
40 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
42 return scalar(%arg) ? $sock->configure(\%arg)
55 my $domain = delete $arg->{Domain};
57 croak 'IO::Socket: Cannot configure a generic socket'
58 unless defined $domain;
60 croak "IO::Socket: Unsupported socket domain"
61 unless defined $domain2pkg[$domain];
63 croak "IO::Socket: Cannot configure socket in domain '$domain'"
64 unless ref($sock) eq "IO::Socket";
66 bless($sock, $domain2pkg[$domain]);
67 $sock->configure($arg);
71 @_ == 4 or croak 'usage: $sock->socket(DOMAIN, TYPE, PROTOCOL)';
72 my($sock,$domain,$type,$protocol) = @_;
74 socket($sock,$domain,$type,$protocol) or
77 ${*$sock}{'io_socket_domain'} = $domain;
78 ${*$sock}{'io_socket_type'} = $type;
79 ${*$sock}{'io_socket_proto'} = $protocol;
85 @_ == 4 || croak 'usage: IO::Socket->socketpair(DOMAIN, TYPE, PROTOCOL)';
86 my($class,$domain,$type,$protocol) = @_;
87 my $sock1 = $class->new();
88 my $sock2 = $class->new();
90 socketpair($sock1,$sock2,$domain,$type,$protocol) or
93 ${*$sock1}{'io_socket_type'} = ${*$sock2}{'io_socket_type'} = $type;
94 ${*$sock1}{'io_socket_proto'} = ${*$sock2}{'io_socket_proto'} = $protocol;
100 @_ == 2 or croak 'usage: $sock->connect(NAME)';
103 my $timeout = ${*$sock}{'io_socket_timeout'};
106 $blocking = $sock->blocking(0) if $timeout;
108 if (!connect($sock, $addr)) {
109 if ($timeout && exists &IO::EINPROGRESS && ($! == &IO::EINPROGRESS)) {
112 my $sel = new IO::Select $sock;
114 if (!$sel->can_write($timeout)) {
115 $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
116 $@ = "connect: timeout";
118 elsif(!connect($sock,$addr) && not $!{EISCONN}) {
119 # Some systems (e.g. Digital UNIX/Tru64) fail to
120 # re-connect() to an already open socket and set
121 # errno to EISCONN (Socket is already connected)
122 # for such an attempt.
133 $sock->blocking(1) if $blocking;
137 $err ? undef : $sock;
141 @_ == 2 or croak 'usage: $sock->bind(NAME)';
145 return bind($sock, $addr) ? $sock
150 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
151 my($sock,$queue) = @_;
153 unless $queue && $queue > 0;
155 return listen($sock, $queue) ? $sock
160 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
162 my $pkg = shift || $sock;
163 my $timeout = ${*$sock}{'io_socket_timeout'};
164 my $new = $pkg->new(Timeout => $timeout);
170 my $sel = new IO::Select $sock;
172 unless ($sel->can_read($timeout)) {
173 $@ = 'accept: timeout';
174 $! = (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
179 $peer = accept($new,$sock)
182 return wantarray ? ($new, $peer)
187 @_ == 1 or croak 'usage: $sock->sockname()';
192 @_ == 1 or croak 'usage: $sock->peername()';
195 || ${*$sock}{'io_socket_peername'}
200 @_ == 1 or croak 'usage: $sock->connected()';
206 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
208 my $flags = $_[2] || 0;
209 my $peer = $_[3] || $sock->peername;
211 croak 'send: Cannot determine peer address'
214 my $r = defined(getpeername($sock))
215 ? send($sock, $_[1], $flags)
216 : send($sock, $_[1], $flags, $peer);
218 # remember who we send to, if it was sucessful
219 ${*$sock}{'io_socket_peername'} = $peer
220 if(@_ == 4 && defined $r);
226 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
229 my $flags = $_[3] || 0;
231 # remember who we recv'd from
232 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
236 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
237 my($sock, $how) = @_;
238 shutdown($sock, $how);
242 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
243 setsockopt($_[0],$_[1],$_[2],$_[3]);
246 my $intsize = length(pack("i",0));
249 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
250 my $r = getsockopt($_[0],$_[1],$_[2]);
253 if(defined $r && length($r) == $intsize);
259 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
260 : $sock->setsockopt(SOL_SOCKET,@_);
264 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
266 my $r = ${*$sock}{'io_socket_timeout'} || undef;
268 ${*$sock}{'io_socket_timeout'} = 0 + $val
275 @_ == 1 or croak 'usage: $sock->sockdomain()';
277 ${*$sock}{'io_socket_domain'};
281 @_ == 1 or croak 'usage: $sock->socktype()';
283 ${*$sock}{'io_socket_type'}
287 @_ == 1 or croak 'usage: $sock->protocol()';
289 ${*$sock}{'io_socket_proto'};
298 IO::Socket - Object interface to socket communications
306 C<IO::Socket> provides an object interface to creating and using sockets. It
307 is built upon the L<IO::Handle> interface and inherits all the methods defined
310 C<IO::Socket> only defines methods for those operations which are common to all
311 types of socket. Operations which are specified to a socket in a particular
312 domain have methods defined in sub classes of C<IO::Socket>
314 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
322 Creates an C<IO::Socket>, which is a reference to a
323 newly created symbol (see the C<Symbol> package). C<new>
324 optionally takes arguments, these arguments are in key-value pairs.
325 C<new> only looks for one key C<Domain> which tells new which domain
326 the socket will be in. All other arguments will be passed to the
327 configuration method of the package for that domain, See below.
329 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
331 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
332 by default. This was not the case with earlier releases.
334 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
340 See L<perlfunc> for complete descriptions of each of the following
341 supported C<IO::Socket> methods, which are just front ends for the
342 corresponding built-in functions:
351 peername (getpeername)
352 sockname (getsockname)
355 Some methods take slightly different arguments to those defined in L<perlfunc>
356 in attempt to make the interface more flexible. These are
362 perform the system call C<accept> on the socket and return a new object. The
363 new object will be created in the same class as the listen socket, unless
364 C<PKG> is specified. This object can be used to communicate with the client
365 that was trying to connect. In a scalar context the new socket is returned,
366 or undef upon failure. In an array context a two-element array is returned
367 containing the new socket and the peer address; the list will
368 be empty upon failure.
370 =item socketpair(DOMAIN, TYPE, PROTOCOL)
372 Call C<socketpair> and return a list of two sockets created, or an
373 empty list on failure.
377 Additional methods that are provided are:
383 Set or get the timeout value associated with this socket. If called without
384 any arguments then the current setting is returned. If called with an argument
385 the current setting is changed and the previous value returned.
387 =item sockopt(OPT [, VAL])
389 Unified method to both set and get options in the SOL_SOCKET level. If called
390 with one argument then getsockopt is called, otherwise setsockopt is called.
394 Returns the numerical number for the socket domain type. For example, for
395 a AF_INET socket the value of &AF_INET will be returned.
399 Returns the numerical number for the socket type. For example, for
400 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
404 Returns the numerical number for the protocol being used on the socket, if
405 known. If the protocol is unknown, as with an AF_UNIX socket, zero
410 If the socket is in a connected state the the peer address is returned.
411 If the socket is not in a connected state then undef will be returned.
417 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
421 Graham Barr. Currently maintained by the Perl Porters. Please report all
422 bugs to <perl5-porters@perl.org>.
426 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
427 This program is free software; you can redistribute it and/or
428 modify it under the same terms as Perl itself.