7 IO::Socket - supply object methods for sockets
15 C<IO::Socket> provides an object interface to creating and using sockets. It
16 is built upon the L<IO::Handle> interface and inherits all the methods defined
19 C<IO::Socket> only defines methods for those operations which are common to all
20 types of socket. Operations which are specified to a socket in a particular
21 domain have methods defined in sub classes of C<IO::Socket>
23 See L<perlfunc> for complete descriptions of each of the following
24 supported C<IO::Seekable> methods, which are just front ends for the
25 corresponding built-in functions:
34 peername (getpeername)
35 sockname (getsockname)
37 Some methods take slightly different arguments to those defined in L<perlfunc>
38 in attempt to make the interface more flexible. These are
42 perform the system call C<accept> on the socket and return a new object. The
43 new object will be created in the same class as the listen socket, unless
44 C<PKG> is specified. This object can be used to communicate with the client
45 that was trying to connect. In a scalar context the new socket is returned,
46 or undef upon failure. In an array context a two-element array is returned
47 containing the new socket and the peer address, the list will
48 be empty upon failure.
50 Additional methods that are provided are
54 Set or get the timeout value associated with this socket. If called without
55 any arguments then the current setting is returned. If called with an argument
56 the current setting is changed and the previous value returned.
58 =item sockopt(OPT [, VAL])
60 Unified method to both set and get options in the SOL_SOCKET level. If called
61 with one argument then getsockopt is called, otherwise setsockopt is called
73 use vars qw(@ISA @EXPORT_OK $VERSION);
76 @ISA = qw(IO::Handle);
78 # This one will turn 1.2 => 1.02 and 1.2.3 => 1.0203 and so on ...
80 $VERSION = do{my @r=(q$Revision: 1.9 $=~/(\d+)/g);sprintf "%d."."%02d"x$#r,@r};
85 Exporter::export 'Socket', $callpkg, @_;
90 my $fh = $class->SUPER::new();
92 ${*$fh}{'io_socket_timeout'} = delete $arg{Timeout};
94 return scalar(%arg) ? $fh->configure(\%arg)
99 croak 'IO::Socket: Cannot configure a generic socket';
103 @_ == 4 or croak 'usage: $fh->socket(DOMAIN, TYPE, PROTOCOL)';
104 my($fh,$domain,$type,$protocol) = @_;
106 socket($fh,$domain,$type,$protocol) or
109 ${*$fh}{'io_socket_type'} = $type;
114 @_ == 4 || croak 'usage: IO::Socket->pair(DOMAIN, TYPE, PROTOCOL)';
115 my($class,$domain,$type,$protocol) = @_;
116 my $fh1 = $class->new();
117 my $fh2 = $class->new();
119 socketpair($fh1,$fh1,$domain,$type,$protocol) or
122 ${*$fh1}{'io_socket_type'} = ${*$fh2}{'io_socket_type'} = $type;
128 @_ == 2 || @_ == 3 or croak 'usage: $fh->connect(NAME) or $fh->connect(PORT, ADDR)';
130 my $addr = @_ == 1 ? shift : sockaddr_in(@_);
131 my $timeout = ${*$fh}{'io_socket_timeout'};
132 local($SIG{ALRM}) = $timeout ? sub { undef $fh; }
133 : $SIG{ALRM} || 'DEFAULT';
136 croak 'connect: Bad address'
137 if(@_ == 2 && !defined $_[1]);
140 defined $Config{d_alarm} && defined alarm($timeout) or
144 my $ok = connect($fh, $addr);
149 croak "connect: timeout"
152 undef $fh unless $ok;
159 @_ == 2 || @_ == 3 or croak 'usage: $fh->bind(NAME) or $fh->bind(PORT, ADDR)';
161 my $addr = @_ == 1 ? shift : sockaddr_in(@_);
163 return bind($fh, $addr) ? $fh
168 @_ >= 1 && @_ <= 2 or croak 'usage: $fh->listen([QUEUE])';
171 unless $queue && $queue > 0;
173 return listen($fh, $queue) ? $fh
178 @_ == 1 || @_ == 2 or croak 'usage $fh->accept([PKG])';
180 my $pkg = shift || $fh;
181 my $timeout = ${*$fh}{'io_socket_timeout'};
182 my $new = $pkg->new(Timeout => $timeout);
188 vec($fdset, $fh->fileno,1) = 1;
189 croak "accept: timeout"
190 unless select($fdset,undef,undef,$timeout);
192 $peer = accept($new,$fh);
195 return wantarray ? defined $peer ? ($new, $peer)
197 : defined $peer ? $new
202 @_ == 1 or croak 'usage: $fh->sockname()';
207 @_ == 1 or croak 'usage: $fh->peername()';
210 || ${*$fh}{'io_socket_peername'}
215 @_ >= 2 && @_ <= 4 or croak 'usage: $fh->send(BUF, [FLAGS, [TO]])';
217 my $flags = $_[2] || 0;
218 my $peer = $_[3] || $fh->peername;
220 croak 'send: Cannot determine peer address'
223 my $r = send($fh, $_[1], $flags, $peer);
225 # remember who we send to, if it was sucessful
226 ${*$fh}{'io_socket_peername'} = $peer
227 if(@_ == 4 && defined $r);
233 @_ == 3 || @_ == 4 or croak 'usage: $fh->recv(BUF, LEN [, FLAGS])';
236 my $flags = $_[3] || 0;
238 # remember who we recv'd from
239 ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
244 @_ == 4 or croak '$fh->setsockopt(LEVEL, OPTNAME)';
245 setsockopt($_[0],$_[1],$_[2],$_[3]);
248 my $intsize = length(pack("i",0));
251 @_ == 3 or croak '$fh->getsockopt(LEVEL, OPTNAME)';
252 my $r = getsockopt($_[0],$_[1],$_[2]);
255 if(defined $r && length($r) == $intsize);
261 @_ == 1 ? $fh->getsockopt(SOL_SOCKET,@_)
262 : $fh->setsockopt(SOL_SOCKET,@_);
266 @_ == 1 || @_ == 2 or croak 'usage: $fh->timeout([VALUE])';
268 my $r = ${*$fh}{'io_socket_timeout'} || undef;
270 ${*$fh}{'io_socket_timeout'} = 0 + $val
277 @_ == 1 or croak '$fh->socktype()';
278 ${*{$_[0]}}{'io_socket_type'} || undef;
289 package IO::Socket::INET;
292 use vars qw(@ISA $VERSION);
297 @ISA = qw(IO::Socket);
299 my %socket_type = ( tcp => SOCK_STREAM,
303 =head2 IO::Socket::INET
305 C<IO::Socket::INET> provides a constructor to create an AF_INET domain socket
306 and some related methods. The constructor can take the following options
308 PeerAddr Remote host address
309 PeerPort Remote port or service
310 LocalPort Local host bind port
311 LocalAddr Local host bind address
312 Proto Protocol name (eg tcp udp etc)
313 Type Socket type (SOCK_STREAM etc)
314 Listen Queue size for listen
315 Timeout Timeout value for various operations
317 If Listen is defined then a listen socket is created, else if the socket
318 type, which is derived from the protocol, is SOCK_STREAM then a connect
321 Only one of C<Type> or C<Proto> needs to be specified, one will be assumed
328 Return the address part of the sockaddr structure for the socket
332 Return the port number that the socket is using on the local host
336 Return the address part of the sockaddr structure for the socket in a
337 text form xx.xx.xx.xx
339 =item peeraddr(), peerport(), peerhost()
341 Same as for the sock* functions, but returns the data about the peer
342 host instead of the local host.
348 my($addr,$port,$proto) = @_;
353 if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,);
356 @proto = $proto =~ m,\D, ? getprotobyname($proto)
357 : getprotobynumber($proto);
359 $proto = $proto[2] || undef;
363 $port =~ s,\((\d+)\)$,,;
365 my $defport = $1 || undef;
366 my $pnum = ($port =~ m,^(\d+)$,)[0];
368 @serv= getservbyname($port, $proto[0] || "")
371 $port = $pnum || $serv[2] || $defport || undef;
373 $proto = (getprotobyname($serv[3]))[2] || undef
377 return ($addr || undef,
385 my($lport,$rport,$laddr,$raddr,$proto,$type);
388 ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
392 $laddr = defined $laddr ? inet_aton($laddr)
395 unless(exists $arg->{Listen}) {
396 ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
401 croak 'IO::Socket: Cannot determine protocol'
404 my $pname = (getprotobynumber($proto))[0];
405 $type = $arg->{Type} || $socket_type{$pname};
407 $fh->socket(AF_INET, $type, $proto) or
410 $fh->bind($lport || 0, $laddr) or
413 if(exists $arg->{Listen}) {
414 $fh->listen($arg->{Listen} || 5) or
418 croak "IO::Socket: Cannot determine remote port"
419 unless($rport || $type == SOCK_DGRAM);
421 if($type == SOCK_STREAM || defined $raddr) {
422 croak "IO::Socket: Bad peer address"
423 unless defined $raddr;
425 $fh->connect($rport,inet_aton($raddr)) or
434 @_ == 1 or croak 'usage: $fh->sockaddr()';
436 (sockaddr_in($fh->sockname))[1];
440 @_ == 1 or croak 'usage: $fh->sockport()';
442 (sockaddr_in($fh->sockname))[0];
446 @_ == 1 or croak 'usage: $fh->sockhost()';
448 inet_ntoa($fh->sockaddr);
452 @_ == 1 or croak 'usage: $fh->peeraddr()';
454 (sockaddr_in($fh->peername))[1];
458 @_ == 1 or croak 'usage: $fh->peerport()';
460 (sockaddr_in($fh->peername))[0];
464 @_ == 1 or croak 'usage: $fh->peerhost()';
466 inet_ntoa($fh->peeraddr);
473 package IO::Socket::UNIX;
476 use vars qw(@ISA $VERSION);
481 @ISA = qw(IO::Socket);
483 =head2 IO::Socket::UNIX
485 C<IO::Socket::UNIX> provides a constructor to create an AF_UNIX domain socket
486 and some related methods. The constructor can take the following options
488 Type Type of socket (eg SOCK_STREAM or SOCK_DGRAM)
489 Local Path to local fifo
490 Peer Path to peer fifo
491 Listen Create a listen socket
497 Returns the pathname to the fifo at the local end
501 Returns the pathanme to the fifo at the peer end
509 my $type = $arg->{Type} || SOCK_STREAM;
511 $fh->socket(AF_UNIX, $type, 0) or
514 if(exists $arg->{Local}) {
515 my $addr = sockaddr_un($arg->{Local});
519 if(exists $arg->{Listen}) {
520 $fh->listen($arg->{Listen} || 5) or
523 elsif(exists $arg->{Peer}) {
524 my $addr = sockaddr_un($arg->{Peer});
525 $fh->connect($addr) or
533 @_ == 1 or croak 'usage: $fh->hostpath()';
534 (sockaddr_un($_[0]->hostname))[0];
538 @_ == 1 or croak 'usage: $fh->peerpath()';
539 (sockaddr_un($_[0]->peername))[0];
544 Graham Barr <Graham.Barr@tiuk.ti.com>
550 The VERSION is derived from the revision turning each number after the
551 first dot into a 2 digit number so
553 Revision 1.8 => VERSION 1.08
554 Revision 1.2.3 => VERSION 1.0203
558 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
559 software; you can redistribute it and/or modify it under the same terms
564 1; # Keep require happy