[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] IO::Socket::APPLETALK



Hi all.

I wrote a subclass for IO::Socket that I think you all might find
interesting.  It's very simple, and allows for easy access to AppleTalk
sockets in MacPerl, via a mechanism that you may already be used to.  You
can also use the raw Socket module for connections, but I think most people
will find IO::Socket to be easier.

Just add the text below to :lib:IO:Socket.pm (just before the text: "=head1
SEE ALSO\n\nL<Socket>, L<IO::Handle>") and run the script below that on two
machines.

Please let me know if it works or not, and any comments you have.

--
Chris Nandor               pudge@pobox.com           http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==                    MacPerl: Power and Ease                     ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#


##
## AF_APPLETALK
##
package IO::Socket::APPLETALK;

use strict;
use vars qw(@ISA $VERSION);
use Socket;
use Carp;
use Exporter;

@ISA = qw(IO::Socket);

IO::Socket::APPLETALK->register_domain( AF_APPLETALK );

=head2 IO::Socket::APPLETALK

C<IO::Socket::APPLETALK> provides a constructor to create an AF_APPLETALK
domain socket and some related methods. The constructor can take the
following options

    Object      Name of service
    Type        Type of service (defaults to Object)
    Zone        AppleTalk Zone (defaults to *)
    Listen      Create a listen socket

=head2 METHODS

=over 4

=item None yet

=back

=cut

sub configure {
    my($fh,$arg) = @_;
    my($addr,$obj,$type,$zone);

    $obj = $arg->{Object};
    $type = $arg->{Type} || $obj;
    $zone = $arg->{Zone} || '*';
    $addr = pack_sockaddr_atlk_sym($obj,$type,$zone) or return undef;

    $fh->socket(AF_APPLETALK, SOCK_STREAM, 0) or return undef;

    if(exists $arg->{Listen}) {
        $fh->bind($addr) or return undef;
        $fh->listen($arg->{Listen} || 5) or return undef;
    } else {
        $fh->connect($addr) or return undef;
    }

    $fh;
}

__END__

# Test script:

#!perl -w
use strict;
use IO::Socket;
my($answer,$client,$server,$sock);

$answer = MacPerl::Answer(
  'Who are you?','Client','Server'
);

if ($answer) {

  $sock = IO::Socket::APPLETALK->new(
    Object  => 'MyAppleTalkSocket'
  ) || die('Cannot start client');

  print while (<$sock>);

} else {

  $server = IO::Socket::APPLETALK->new(
    Object  => 'MyAppleTalkSocket',
    Listen  => 1,
  ) || die('Cannot start server');

  printf("[Server at %s accepting clients]\n",
    join '.', sockaddr_atlk(getsockname($server))
  );

  while ($client = $server->accept()) {
    print $client "Connect!\n";
    print "Connect!\n";
    close($client);
  }

}



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch