#!/usr/bin/perl -w
use IO::Socket::UNIX;
use strict;
my $sockfile = '/tmp/mysocket';
my $r;
unlink($sockfile);
my $usock = new IO::Socket::UNIX(
Type => SOCK_STREAM,
Local => $sockfile,
Listen => 1)
|| die "ERROR: $!\n";
while (1) {
if (($r) = $usock->accept) {
my $out = do_something($r);
$r->printflush($out);
# sleep 1; # this makes it work
$r->close;
}
}
sub do_something {
my ($fh) = @_;
my ($l) = <$fh>;
return uc($l);
}
####
#!/usr/bin/perl -w
use IO::Socket::UNIX;
my ($sockfile) = shift
|| die "$0: Socket address required\n";
my $r;
my $usock = new IO::Socket::UNIX (Type => SOCK_STREAM,
Peer => $sockfile)
|| die "ERROR: $!\n";
# read stdin and write to socket
while(<>) {
print $usock $_;
}
# shut down writing side (server receives EOF)
$usock->shutdown(1);
$sock->blocking(1); # this does not help
# read socket (blocking) and write to stdout
while(<$usock>) {
print $_;
}
# shut down reading side
$usock->shutdown(0);
##
##
my $linger = pack('ii', 1, 120); # BSD struct
$usock->sockopt(SO_LINGER, $linger);