Saturday, March 21, 2009

send: Cannot determine peer address

I came across this error while doing some work with Unix domain sockets and Perl's IO::Socket::UNIX module. I was able to create the socket successfully and have the client connect to it. The client could successfully send data to the server, but when the server tried to respond I got the error "send: Cannot determine peer address". My original server code was:


use strict;
use warnings;
use IO::Socket::UNIX;
my $server = IO::Socket::UNIX->new(
Local => '/tmp/path/to/socket',
Type => SOCK_STREAM,
Listen => 5,
) or die $@;


The client code that produced the error was


use strict;
use warnings;
use IO::Socket::UNIX;
my $socket = IO::Socket::UNIX->new(
Peer => '/tmp/path/to/socket',
Type => SOCK_STREAM,
) or die $@;
print $socket "command away\n";


It turns out that my client was exiting before the server could respond. By the time the server responded, the client had disappeared and no "peer address" was available. By adding my $response = <$socket> to the end of the script, the client waited for the server's response and everything worked great.