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'};
107 croak 'connect: Bad address'
108 if(@_ == 2 && !defined $_[1]);
110 $blocking = $sock->blocking(0)
113 unless(connect($sock, $addr)) {
114 if($timeout && ($! == &IO::EINPROGRESS)) {
117 my $sel = new IO::Select $sock;
122 unless($sel->can_write($timeout) && defined($sock->peername)) {
124 croak "connect: timeout";
133 if($sock && $blocking);
140 @_ == 2 or croak 'usage: $sock->bind(NAME)';
144 return bind($sock, $addr) ? $sock
149 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
150 my($sock,$queue) = @_;
152 unless $queue && $queue > 0;
154 return listen($sock, $queue) ? $sock
159 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
161 my $pkg = shift || $sock;
162 my $timeout = ${*$sock}{'io_socket_timeout'};
163 my $new = $pkg->new(Timeout => $timeout);
170 my $sel = new IO::Select $sock;
172 croak "accept: timeout"
173 unless $sel->can_read($timeout);
175 $peer = accept($new,$sock) || undef;
178 return wantarray ? defined $peer ? ($new, $peer)
180 : defined $peer ? $new
185 @_ == 1 or croak 'usage: $sock->sockname()';
190 @_ == 1 or croak 'usage: $sock->peername()';
193 || ${*$sock}{'io_socket_peername'}
198 @_ == 1 or croak 'usage: $sock->connected()';
204 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
206 my $flags = $_[2] || 0;
207 my $peer = $_[3] || $sock->peername;
209 croak 'send: Cannot determine peer address'
212 my $r = defined(getpeername($sock))
213 ? send($sock, $_[1], $flags)
214 : send($sock, $_[1], $flags, $peer);
216 # remember who we send to, if it was sucessful
217 ${*$sock}{'io_socket_peername'} = $peer
218 if(@_ == 4 && defined $r);
224 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
227 my $flags = $_[3] || 0;
229 # remember who we recv'd from
230 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
234 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
235 my($sock, $how) = @_;
236 shutdown($sock, $how);
240 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME)';
241 setsockopt($_[0],$_[1],$_[2],$_[3]);
244 my $intsize = length(pack("i",0));
247 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
248 my $r = getsockopt($_[0],$_[1],$_[2]);
251 if(defined $r && length($r) == $intsize);
257 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
258 : $sock->setsockopt(SOL_SOCKET,@_);
262 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
264 my $r = ${*$sock}{'io_socket_timeout'} || undef;
266 ${*$sock}{'io_socket_timeout'} = 0 + $val
273 @_ == 1 or croak 'usage: $sock->sockdomain()';
275 ${*$sock}{'io_socket_domain'};
279 @_ == 1 or croak 'usage: $sock->socktype()';
281 ${*$sock}{'io_socket_type'}
285 @_ == 1 or croak 'usage: $sock->protocol()';
287 ${*$sock}{'io_socket_protocol'};
296 IO::Socket - Object interface to socket communications
304 C<IO::Socket> provides an object interface to creating and using sockets. It
305 is built upon the L<IO::Handle> interface and inherits all the methods defined
308 C<IO::Socket> only defines methods for those operations which are common to all
309 types of socket. Operations which are specified to a socket in a particular
310 domain have methods defined in sub classes of C<IO::Socket>
312 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
320 Creates an C<IO::Socket>, which is a reference to a
321 newly created symbol (see the C<Symbol> package). C<new>
322 optionally takes arguments, these arguments are in key-value pairs.
323 C<new> only looks for one key C<Domain> which tells new which domain
324 the socket will be in. All other arguments will be passed to the
325 configuration method of the package for that domain, See below.
327 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
329 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
330 by default. This was not the case with earlier releases.
332 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
338 See L<perlfunc> for complete descriptions of each of the following
339 supported C<IO::Socket> methods, which are just front ends for the
340 corresponding built-in functions:
349 peername (getpeername)
350 sockname (getsockname)
353 Some methods take slightly different arguments to those defined in L<perlfunc>
354 in attempt to make the interface more flexible. These are
360 perform the system call C<accept> on the socket and return a new object. The
361 new object will be created in the same class as the listen socket, unless
362 C<PKG> is specified. This object can be used to communicate with the client
363 that was trying to connect. In a scalar context the new socket is returned,
364 or undef upon failure. In an array context a two-element array is returned
365 containing the new socket and the peer address, the list will
366 be empty upon failure.
368 Additional methods that are provided are
372 Set or get the timeout value associated with this socket. If called without
373 any arguments then the current setting is returned. If called with an argument
374 the current setting is changed and the previous value returned.
376 =item sockopt(OPT [, VAL])
378 Unified method to both set and get options in the SOL_SOCKET level. If called
379 with one argument then getsockopt is called, otherwise setsockopt is called.
383 Returns the numerical number for the socket domain type. For example, for
384 a AF_INET socket the value of &AF_INET will be returned.
388 Returns the numerical number for the socket type. For example, for
389 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
393 Returns the numerical number for the protocol being used on the socket, if
394 known. If the protocol is unknown, as with an AF_UNIX socket, zero
399 If the socket is in a connected state the the peer address is returned.
400 If the socket is not in a connected state then undef will be returned.
406 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
410 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
414 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
415 This program is free software; you can redistribute it and/or
416 modify it under the same terms as Perl itself.