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->pair(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;
173 return wantarray ? defined $peer ? ($new, $peer)
175 : defined $peer ? $new
180 @_ == 1 or croak 'usage: $sock->sockname()';
185 @_ == 1 or croak 'usage: $sock->peername()';
188 || ${*$sock}{'io_socket_peername'}
193 @_ == 1 or croak 'usage: $sock->connected()';
199 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
201 my $flags = $_[2] || 0;
202 my $peer = $_[3] || $sock->peername;
204 croak 'send: Cannot determine peer address'
207 my $r = defined(getpeername($sock))
208 ? send($sock, $_[1], $flags)
209 : send($sock, $_[1], $flags, $peer);
211 # remember who we send to, if it was sucessful
212 ${*$sock}{'io_socket_peername'} = $peer
213 if(@_ == 4 && defined $r);
219 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
222 my $flags = $_[3] || 0;
224 # remember who we recv'd from
225 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
229 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
230 my($sock, $how) = @_;
231 shutdown($sock, $how);
235 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
236 setsockopt($_[0],$_[1],$_[2],$_[3]);
239 my $intsize = length(pack("i",0));
242 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
243 my $r = getsockopt($_[0],$_[1],$_[2]);
246 if(defined $r && length($r) == $intsize);
252 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
253 : $sock->setsockopt(SOL_SOCKET,@_);
257 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
259 my $r = ${*$sock}{'io_socket_timeout'} || undef;
261 ${*$sock}{'io_socket_timeout'} = 0 + $val
268 @_ == 1 or croak 'usage: $sock->sockdomain()';
270 ${*$sock}{'io_socket_domain'};
274 @_ == 1 or croak 'usage: $sock->socktype()';
276 ${*$sock}{'io_socket_type'}
280 @_ == 1 or croak 'usage: $sock->protocol()';
282 ${*$sock}{'io_socket_protocol'};
291 IO::Socket - Object interface to socket communications
299 C<IO::Socket> provides an object interface to creating and using sockets. It
300 is built upon the L<IO::Handle> interface and inherits all the methods defined
303 C<IO::Socket> only defines methods for those operations which are common to all
304 types of socket. Operations which are specified to a socket in a particular
305 domain have methods defined in sub classes of C<IO::Socket>
307 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
315 Creates an C<IO::Socket>, which is a reference to a
316 newly created symbol (see the C<Symbol> package). C<new>
317 optionally takes arguments, these arguments are in key-value pairs.
318 C<new> only looks for one key C<Domain> which tells new which domain
319 the socket will be in. All other arguments will be passed to the
320 configuration method of the package for that domain, See below.
322 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
324 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
325 by default. This was not the case with earlier releases.
327 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
333 See L<perlfunc> for complete descriptions of each of the following
334 supported C<IO::Socket> methods, which are just front ends for the
335 corresponding built-in functions:
344 peername (getpeername)
345 sockname (getsockname)
348 Some methods take slightly different arguments to those defined in L<perlfunc>
349 in attempt to make the interface more flexible. These are
355 perform the system call C<accept> on the socket and return a new object. The
356 new object will be created in the same class as the listen socket, unless
357 C<PKG> is specified. This object can be used to communicate with the client
358 that was trying to connect. In a scalar context the new socket is returned,
359 or undef upon failure. In an array context a two-element array is returned
360 containing the new socket and the peer address, the list will
361 be empty upon failure.
363 Additional methods that are provided are
367 Set or get the timeout value associated with this socket. If called without
368 any arguments then the current setting is returned. If called with an argument
369 the current setting is changed and the previous value returned.
371 =item sockopt(OPT [, VAL])
373 Unified method to both set and get options in the SOL_SOCKET level. If called
374 with one argument then getsockopt is called, otherwise setsockopt is called.
378 Returns the numerical number for the socket domain type. For example, for
379 a AF_INET socket the value of &AF_INET will be returned.
383 Returns the numerical number for the socket type. For example, for
384 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
388 Returns the numerical number for the protocol being used on the socket, if
389 known. If the protocol is unknown, as with an AF_UNIX socket, zero
394 If the socket is in a connected state the the peer address is returned.
395 If the socket is not in a connected state then undef will be returned.
401 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
405 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
409 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
410 This program is free software; you can redistribute it and/or
411 modify it under the same terms as Perl itself.