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 our(@ISA, $VERSION, @EXPORT_OK);
21 require IO::Socket::INET;
22 require IO::Socket::UNIX if ($^O ne 'epoc' && $^O ne 'symbian');
24 @ISA = qw(IO::Handle);
28 @EXPORT_OK = qw(sockatmark);
32 if (@_ && $_[0] eq 'sockatmark') { # not very extensible but for now, fast
33 Exporter::export_to_level('IO::Socket', 1, $pkg, 'sockatmark');
36 Exporter::export 'Socket', $callpkg, @_;
42 my $sock = $class->SUPER::new();
46 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
48 return scalar(%arg) ? $sock->configure(\%arg)
61 my $domain = delete $arg->{Domain};
63 croak 'IO::Socket: Cannot configure a generic socket'
64 unless defined $domain;
66 croak "IO::Socket: Unsupported socket domain"
67 unless defined $domain2pkg[$domain];
69 croak "IO::Socket: Cannot configure socket in domain '$domain'"
70 unless ref($sock) eq "IO::Socket";
72 bless($sock, $domain2pkg[$domain]);
73 $sock->configure($arg);
77 @_ == 4 or croak 'usage: $sock->socket(DOMAIN, TYPE, PROTOCOL)';
78 my($sock,$domain,$type,$protocol) = @_;
80 socket($sock,$domain,$type,$protocol) or
83 ${*$sock}{'io_socket_domain'} = $domain;
84 ${*$sock}{'io_socket_type'} = $type;
85 ${*$sock}{'io_socket_proto'} = $protocol;
91 @_ == 4 || croak 'usage: IO::Socket->socketpair(DOMAIN, TYPE, PROTOCOL)';
92 my($class,$domain,$type,$protocol) = @_;
93 my $sock1 = $class->new();
94 my $sock2 = $class->new();
96 socketpair($sock1,$sock2,$domain,$type,$protocol) or
99 ${*$sock1}{'io_socket_type'} = ${*$sock2}{'io_socket_type'} = $type;
100 ${*$sock1}{'io_socket_proto'} = ${*$sock2}{'io_socket_proto'} = $protocol;
106 @_ == 2 or croak 'usage: $sock->connect(NAME)';
109 my $timeout = ${*$sock}{'io_socket_timeout'};
113 $blocking = $sock->blocking(0) if $timeout;
114 if (!connect($sock, $addr)) {
115 if (defined $timeout && ($!{EINPROGRESS} || $!{EWOULDBLOCK})) {
118 my $sel = new IO::Select $sock;
120 if (!$sel->can_write($timeout)) {
121 $err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
122 $@ = "connect: timeout";
124 elsif (!connect($sock,$addr) &&
125 not ($!{EISCONN} || ($! == 10022 && $^O eq 'MSWin32'))
127 # Some systems refuse to re-connect() to
128 # an already open socket and set errno to EISCONN.
129 # Windows sets errno to WSAEINVAL (10022)
134 elsif ($blocking || !($!{EINPROGRESS} || $!{EWOULDBLOCK})) {
140 $sock->blocking(1) if $blocking;
144 $err ? undef : $sock;
151 return $sock->SUPER::blocking(@_)
154 # Windows handles blocking differently
156 # http://groups.google.co.uk/group/perl.perl5.porters/browse_thread/
157 # thread/b4e2b1d88280ddff/630b667a66e3509f?#630b667a66e3509f
158 # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
159 # winsock/winsock/ioctlsocket_2.asp
161 # 0x8004667e is FIONBIO
162 # By default all sockets are blocking
164 return !${*$sock}{io_sock_nonblocking}
169 ${*$sock}{io_sock_nonblocking} = $block ? "0" : "1";
171 return ioctl($sock, 0x8004667e, \${*$sock}{io_sock_nonblocking});
176 @_ == 1 or croak 'usage: $sock->close()';
178 ${*$sock}{'io_socket_peername'} = undef;
179 $sock->SUPER::close();
183 @_ == 2 or croak 'usage: $sock->bind(NAME)';
187 return bind($sock, $addr) ? $sock
192 @_ >= 1 && @_ <= 2 or croak 'usage: $sock->listen([QUEUE])';
193 my($sock,$queue) = @_;
195 unless $queue && $queue > 0;
197 return listen($sock, $queue) ? $sock
202 @_ == 1 || @_ == 2 or croak 'usage $sock->accept([PKG])';
204 my $pkg = shift || $sock;
205 my $timeout = ${*$sock}{'io_socket_timeout'};
206 my $new = $pkg->new(Timeout => $timeout);
209 if(defined $timeout) {
212 my $sel = new IO::Select $sock;
214 unless ($sel->can_read($timeout)) {
215 $@ = 'accept: timeout';
216 $! = (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
221 $peer = accept($new,$sock)
224 return wantarray ? ($new, $peer)
229 @_ == 1 or croak 'usage: $sock->sockname()';
234 @_ == 1 or croak 'usage: $sock->peername()';
236 ${*$sock}{'io_socket_peername'} ||= getpeername($sock);
240 @_ == 1 or croak 'usage: $sock->connected()';
246 @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
248 my $flags = $_[2] || 0;
249 my $peer = $_[3] || $sock->peername;
251 croak 'send: Cannot determine peer address'
254 my $r = defined(getpeername($sock))
255 ? send($sock, $_[1], $flags)
256 : send($sock, $_[1], $flags, $peer);
258 # remember who we send to, if it was successful
259 ${*$sock}{'io_socket_peername'} = $peer
260 if(@_ == 4 && defined $r);
266 @_ == 3 || @_ == 4 or croak 'usage: $sock->recv(BUF, LEN [, FLAGS])';
269 my $flags = $_[3] || 0;
271 # remember who we recv'd from
272 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
276 @_ == 2 or croak 'usage: $sock->shutdown(HOW)';
277 my($sock, $how) = @_;
278 ${*$sock}{'io_socket_peername'} = undef;
279 shutdown($sock, $how);
283 @_ == 4 or croak '$sock->setsockopt(LEVEL, OPTNAME, OPTVAL)';
284 setsockopt($_[0],$_[1],$_[2],$_[3]);
287 my $intsize = length(pack("i",0));
290 @_ == 3 or croak '$sock->getsockopt(LEVEL, OPTNAME)';
291 my $r = getsockopt($_[0],$_[1],$_[2]);
294 if(defined $r && length($r) == $intsize);
300 @_ == 1 ? $sock->getsockopt(SOL_SOCKET,@_)
301 : $sock->setsockopt(SOL_SOCKET,@_);
305 @_ == 1 or croak 'usage: $sock->atmark()';
311 @_ == 1 || @_ == 2 or croak 'usage: $sock->timeout([VALUE])';
313 my $r = ${*$sock}{'io_socket_timeout'};
315 ${*$sock}{'io_socket_timeout'} = defined $val ? 0 + $val : $val
322 @_ == 1 or croak 'usage: $sock->sockdomain()';
324 ${*$sock}{'io_socket_domain'};
328 @_ == 1 or croak 'usage: $sock->socktype()';
330 ${*$sock}{'io_socket_type'}
334 @_ == 1 or croak 'usage: $sock->protocol()';
336 ${*$sock}{'io_socket_proto'};
345 IO::Socket - Object interface to socket communications
353 C<IO::Socket> provides an object interface to creating and using sockets. It
354 is built upon the L<IO::Handle> interface and inherits all the methods defined
357 C<IO::Socket> only defines methods for those operations which are common to all
358 types of socket. Operations which are specified to a socket in a particular
359 domain have methods defined in sub classes of C<IO::Socket>
361 C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
369 Creates an C<IO::Socket>, which is a reference to a
370 newly created symbol (see the C<Symbol> package). C<new>
371 optionally takes arguments, these arguments are in key-value pairs.
372 C<new> only looks for one key C<Domain> which tells new which domain
373 the socket will be in. All other arguments will be passed to the
374 configuration method of the package for that domain, See below.
376 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
378 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
379 by default. This was not the case with earlier releases.
381 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
387 See L<perlfunc> for complete descriptions of each of the following
388 supported C<IO::Socket> methods, which are just front ends for the
389 corresponding built-in functions:
398 peername (getpeername)
399 sockname (getsockname)
402 Some methods take slightly different arguments to those defined in L<perlfunc>
403 in attempt to make the interface more flexible. These are
409 perform the system call C<accept> on the socket and return a new
410 object. The new object will be created in the same class as the listen
411 socket, unless C<PKG> is specified. This object can be used to
412 communicate with the client that was trying to connect.
414 In a scalar context the new socket is returned, or undef upon
415 failure. In a list context a two-element array is returned containing
416 the new socket and the peer address; the list will be empty upon
419 The timeout in the [PKG] can be specified as zero to effect a "poll",
420 but you shouldn't do that because a new IO::Select object will be
421 created behind the scenes just to do the single poll. This is
422 horrendously inefficient. Use rather true select() with a zero
423 timeout on the handle, or non-blocking IO.
425 =item socketpair(DOMAIN, TYPE, PROTOCOL)
427 Call C<socketpair> and return a list of two sockets created, or an
428 empty list on failure.
432 Additional methods that are provided are:
438 True if the socket is currently positioned at the urgent data mark,
443 my $sock = IO::Socket::INET->new('some_server');
444 $sock->read($data, 1024) until $sock->atmark;
446 Note: this is a reasonably new addition to the family of socket
447 functions, so all systems may not support this yet. If it is
448 unsupported by the system, an attempt to use this method will
451 The atmark() functionality is also exportable as sockatmark() function:
453 use IO::Socket 'sockatmark';
455 This allows for a more traditional use of sockatmark() as a procedural
456 socket function. If your system does not support sockatmark(), the
457 C<use> declaration will fail at compile time.
461 If the socket is in a connected state the peer address is returned.
462 If the socket is not in a connected state then undef will be returned.
466 Returns the numerical number for the protocol being used on the socket, if
467 known. If the protocol is unknown, as with an AF_UNIX socket, zero
472 Returns the numerical number for the socket domain type. For example, for
473 an AF_INET socket the value of &AF_INET will be returned.
475 =item sockopt(OPT [, VAL])
477 Unified method to both set and get options in the SOL_SOCKET level. If called
478 with one argument then getsockopt is called, otherwise setsockopt is called.
482 Returns the numerical number for the socket type. For example, for
483 a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
487 Set or get the timeout value associated with this socket. If called without
488 any arguments then the current setting is returned. If called with an argument
489 the current setting is changed and the previous value returned.
495 L<Socket>, L<IO::Handle>, L<IO::Socket::INET>, L<IO::Socket::UNIX>
499 Graham Barr. atmark() by Lincoln Stein. Currently maintained by the
500 Perl Porters. Please report all bugs to <perl5-porters@perl.org>.
504 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
505 This program is free software; you can redistribute it and/or
506 modify it under the same terms as Perl itself.
508 The atmark() implementation: Copyright 2001, Lincoln Stein <lstein@cshl.org>.
509 This module is distributed under the same terms as Perl itself.
510 Feel free to use, modify and redistribute it as long as you retain
511 the correct attribution.