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.
15 use vars qw(@ISA $VERSION);
20 require IO::Socket::INET;
21 require IO::Socket::UNIX;
23 @ISA = qw(IO::Handle);
30 Exporter::export 'Socket', $callpkg, @_;
35 my $sock = $class->SUPER::new();
39 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
41 return scalar(%arg) ? $sock->configure(\%arg)
54 my $domain = delete $arg->{Domain};
56 croak 'IO::Socket: Cannot configure a generic socket'
57 unless defined $domain;
59 croak "IO::Socket: Unsupported socket domain"
60 unless defined $domain2pkg[$domain];
62 croak "IO::Socket: Cannot configure socket in domain '$domain'"
63 unless ref($sock) eq "IO::Socket";
65 bless($sock, $domain2pkg[$domain]);
66 $sock->configure($arg);
70 @_ == 4 or croak 'usage: $sock->socket(DOMAIN, TYPE, PROTOCOL)';
71 my($sock,$domain,$type,$protocol) = @_;
73 socket($sock,$domain,$type,$protocol) or
76 ${*$sock}{'io_socket_domain'} = $domain;
77 ${*$sock}{'io_socket_type'} = $type;
78 ${*$sock}{'io_socket_proto'} = $protocol;
84 @_ == 4 || croak 'usage: IO::Socket->socketpair(DOMAIN, TYPE, PROTOCOL)';
85 my($class,$domain,$type,$protocol) = @_;
86 my $sock1 = $class->new();
87 my $sock2 = $class->new();
89 socketpair($sock1,$sock2,$domain,$type,$protocol) or
92 ${*$sock1}{'io_socket_type'} = ${*$sock2}{'io_socket_type'} = $type;
93 ${*$sock1}{'io_socket_proto'} = ${*$sock2}{'io_socket_proto'} = $protocol;
99 @_ == 2 or croak 'usage: $sock->connect(NAME)';
102 my $timeout = ${*$sock}{'io_socket_timeout'};
105 $blocking = $sock->blocking(0) if $timeout;
108 croak 'connect: Bad address'
109 if(@_ == 2 && !defined $_[1]);
111 unless(connect($sock, $addr)) {
112 if($timeout && ($! == &IO::EINPROGRESS)) {
115 my $sel = new IO::Select $sock;
117 unless($sel->can_write($timeout) && defined($sock->peername)) {
118 croak "connect: timeout";
127 my $ret = $@ ? undef : $sock;
129 $sock->blocking($blocking) if $timeout;
135 @_ == 2 or croak 'usage: $sock->bind(NAME)';
139 return bind($sock, $addr) ? $sock
144 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
145 my($sock,$queue) = @_;
147 unless $queue && $queue > 0;
149 return listen($sock, $queue) ? $sock
154 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
156 my $pkg = shift || $sock;
157 my $timeout = ${*$sock}{'io_socket_timeout'};
158 my $new = $pkg->new(Timeout => $timeout);
165 my $sel = new IO::Select $sock;
167 croak "accept: timeout"
168 unless $sel->can_read($timeout);
170 $peer = accept($new,$sock) || undef;
175 return wantarray ? defined $peer ? ($new, $peer)
177 : defined $peer ? $new
182 @_ == 1 or croak 'usage: $sock->sockname()';
187 @_ == 1 or croak 'usage: $sock->peername()';
190 || ${*$sock}{'io_socket_peername'}
195 @_ == 1 or croak 'usage: $sock->connected()';
201 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
203 my $flags = $_[2] || 0;
204 my $peer = $_[3] || $sock->peername;
206 croak 'send: Cannot determine peer address'
209 my $r = defined(getpeername($sock))
210 ? send($sock, $_[1], $flags)
211 : send($sock, $_[1], $flags, $peer);
213 # remember who we send to, if it was sucessful
214 ${*$sock}{'io_socket_peername'} = $peer
215 if(@_ == 4 && defined $r);
221 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
224 my $flags = $_[3] || 0;
226 # remember who we recv'd from
227 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
231 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
232 my($sock, $how) = @_;
233 shutdown($sock, $how);
237 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
238 setsockopt($_[0],$_[1],$_[2],$_[3]);
241 my $intsize = length(pack("i",0));
244 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
245 my $r = getsockopt($_[0],$_[1],$_[2]);
248 if(defined $r && length($r) == $intsize);
254 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
255 : $sock->setsockopt(SOL_SOCKET,@_);
259 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
261 my $r = ${*$sock}{'io_socket_timeout'} || undef;
263 ${*$sock}{'io_socket_timeout'} = 0 + $val
270 @_ == 1 or croak 'usage: $sock->sockdomain()';
272 ${*$sock}{'io_socket_domain'};
276 @_ == 1 or croak 'usage: $sock->socktype()';
278 ${*$sock}{'io_socket_type'}
282 @_ == 1 or croak 'usage: $sock->protocol()';
284 ${*$sock}{'io_socket_proto'};
293 IO::Socket - Object interface to socket communications
301 C<IO::Socket> provides an object interface to creating and using sockets. It
302 is built upon the L<IO::Handle> interface and inherits all the methods defined
305 C<IO::Socket> only defines methods for those operations which are common to all
306 types of socket. Operations which are specified to a socket in a particular
307 domain have methods defined in sub classes of C<IO::Socket>
309 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
317 Creates an C<IO::Socket>, which is a reference to a
318 newly created symbol (see the C<Symbol> package). C<new>
319 optionally takes arguments, these arguments are in key-value pairs.
320 C<new> only looks for one key C<Domain> which tells new which domain
321 the socket will be in. All other arguments will be passed to the
322 configuration method of the package for that domain, See below.
324 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
326 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
327 by default. This was not the case with earlier releases.
329 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
335 See L<perlfunc> for complete descriptions of each of the following
336 supported C<IO::Socket> methods, which are just front ends for the
337 corresponding built-in functions:
346 peername (getpeername)
347 sockname (getsockname)
350 Some methods take slightly different arguments to those defined in L<perlfunc>
351 in attempt to make the interface more flexible. These are
357 perform the system call C<accept> on the socket and return a new object. The
358 new object will be created in the same class as the listen socket, unless
359 C<PKG> is specified. This object can be used to communicate with the client
360 that was trying to connect. In a scalar context the new socket is returned,
361 or undef upon failure. In an array context a two-element array is returned
362 containing the new socket and the peer address; the list will
363 be empty upon failure.
365 =item socketpair(DOMAIN, TYPE, PROTOCOL)
367 Call C<socketpair> and return a list of two sockets created, or an
368 empty list on failure.
372 Additional methods that are provided are:
378 Set or get the timeout value associated with this socket. If called without
379 any arguments then the current setting is returned. If called with an argument
380 the current setting is changed and the previous value returned.
382 =item sockopt(OPT [, VAL])
384 Unified method to both set and get options in the SOL_SOCKET level. If called
385 with one argument then getsockopt is called, otherwise setsockopt is called.
389 Returns the numerical number for the socket domain type. For example, for
390 a AF_INET socket the value of &AF_INET will be returned.
394 Returns the numerical number for the socket type. For example, for
395 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
399 Returns the numerical number for the protocol being used on the socket, if
400 known. If the protocol is unknown, as with an AF_UNIX socket, zero
405 If the socket is in a connected state the the peer address is returned.
406 If the socket is not in a connected state then undef will be returned.
412 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
416 Graham Barr. Currently maintained by the Perl Porters. Please report all
417 bugs to <perl5-porters@perl.org>.
421 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
422 This program is free software; you can redistribute it and/or
423 modify it under the same terms as Perl itself.