Unix Domain Sockets in Perl
I know this has been done dozens of times before, but it's been a real headache for me lately. A full example of how to use IO::Socket::UNIX is not really available anywhere on the web. All of the socket info for perl seems to assume you've been doing socket programming in C for years, and theres very limited info on doing Unix Domain Sockets. it all seems to be about inet sockets. which, of course, relates a lot to Unix Sockets, but there are some differences.
okay, here is my working example. Its meant to be run twice, the first time it's the server, the second time it's the client.
#!/usr/bin/perl
use strict; $|++;
use IO::Socket;
my $socketfile = $ENV{HOME} . "/.sockettest";
if ( -S $socketfile ) {
# client!
my $client = IO::Socket::UNIX->new(Peer => $socketfile,
Type => SOCK_STREAM ) or die $!;
my $string = "this is some sent garbage.\n";
print $client $string;
$client->flush;
$client->close;
exit;
} else {
# server!
unlink $socketfile;
my $data;
my $server = IO::Socket::UNIX->new(Local => $socketfile,
Type => SOCK_STREAM,
Listen => 32 ) or die $!;
$server->autoflush(1);
while ( my $connection = $server->accept() ) {
my $data= <$connection>;
print $data, $/;
sleep 5;
}
}

