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.
20 require IO::Socket::INET;
21 require IO::Socket::UNIX if ($^O ne 'epoc');
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;
172 croak "$@" if $@ and $sock;
174 return wantarray ? defined $peer ? ($new, $peer)
176 : defined $peer ? $new
181 @_ == 1 or croak 'usage: $sock->sockname()';
186 @_ == 1 or croak 'usage: $sock->peername()';
189 || ${*$sock}{'io_socket_peername'}
194 @_ == 1 or croak 'usage: $sock->connected()';
200 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
202 my $flags = $_[2] || 0;
203 my $peer = $_[3] || $sock->peername;
205 croak 'send: Cannot determine peer address'
208 my $r = defined(getpeername($sock))
209 ? send($sock, $_[1], $flags)
210 : send($sock, $_[1], $flags, $peer);
212 # remember who we send to, if it was sucessful
213 ${*$sock}{'io_socket_peername'} = $peer
214 if(@_ == 4 && defined $r);
220 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
223 my $flags = $_[3] || 0;
225 # remember who we recv'd from
226 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
230 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
231 my($sock, $how) = @_;
232 shutdown($sock, $how);
236 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
237 setsockopt($_[0],$_[1],$_[2],$_[3]);
240 my $intsize = length(pack("i",0));
243 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
244 my $r = getsockopt($_[0],$_[1],$_[2]);
247 if(defined $r && length($r) == $intsize);
253 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
254 : $sock->setsockopt(SOL_SOCKET,@_);
258 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
260 my $r = ${*$sock}{'io_socket_timeout'} || undef;
262 ${*$sock}{'io_socket_timeout'} = 0 + $val
269 @_ == 1 or croak 'usage: $sock->sockdomain()';
271 ${*$sock}{'io_socket_domain'};
275 @_ == 1 or croak 'usage: $sock->socktype()';
277 ${*$sock}{'io_socket_type'}
281 @_ == 1 or croak 'usage: $sock->protocol()';
283 ${*$sock}{'io_socket_proto'};
292 IO::Socket - Object interface to socket communications
300 C<IO::Socket> provides an object interface to creating and using sockets. It
301 is built upon the L<IO::Handle> interface and inherits all the methods defined
304 C<IO::Socket> only defines methods for those operations which are common to all
305 types of socket. Operations which are specified to a socket in a particular
306 domain have methods defined in sub classes of C<IO::Socket>
308 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
316 Creates an C<IO::Socket>, which is a reference to a
317 newly created symbol (see the C<Symbol> package). C<new>
318 optionally takes arguments, these arguments are in key-value pairs.
319 C<new> only looks for one key C<Domain> which tells new which domain
320 the socket will be in. All other arguments will be passed to the
321 configuration method of the package for that domain, See below.
323 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
325 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
326 by default. This was not the case with earlier releases.
328 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
334 See L<perlfunc> for complete descriptions of each of the following
335 supported C<IO::Socket> methods, which are just front ends for the
336 corresponding built-in functions:
345 peername (getpeername)
346 sockname (getsockname)
349 Some methods take slightly different arguments to those defined in L<perlfunc>
350 in attempt to make the interface more flexible. These are
356 perform the system call C<accept> on the socket and return a new object. The
357 new object will be created in the same class as the listen socket, unless
358 C<PKG> is specified. This object can be used to communicate with the client
359 that was trying to connect. In a scalar context the new socket is returned,
360 or undef upon failure. In an array context a two-element array is returned
361 containing the new socket and the peer address; the list will
362 be empty upon failure.
364 =item socketpair(DOMAIN, TYPE, PROTOCOL)
366 Call C<socketpair> and return a list of two sockets created, or an
367 empty list on failure.
371 Additional methods that are provided are:
377 Set or get the timeout value associated with this socket. If called without
378 any arguments then the current setting is returned. If called with an argument
379 the current setting is changed and the previous value returned.
381 =item sockopt(OPT [, VAL])
383 Unified method to both set and get options in the SOL_SOCKET level. If called
384 with one argument then getsockopt is called, otherwise setsockopt is called.
388 Returns the numerical number for the socket domain type. For example, for
389 a AF_INET socket the value of &AF_INET will be returned.
393 Returns the numerical number for the socket type. For example, for
394 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
398 Returns the numerical number for the protocol being used on the socket, if
399 known. If the protocol is unknown, as with an AF_UNIX socket, zero
404 If the socket is in a connected state the the peer address is returned.
405 If the socket is not in a connected state then undef will be returned.
411 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
415 Graham Barr. Currently maintained by the Perl Porters. Please report all
416 bugs to <perl5-porters@perl.org>.
420 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
421 This program is free software; you can redistribute it and/or
422 modify it under the same terms as Perl itself.