我正在尝试学习如何使用套接字在同一台计算机上的进程之间进行通信。我从 Perl Cookbook 中的这个简单测试程序开始:
use feature qw(say);
use strict;
use warnings;
use IO::Socket;
my $socket_file = 'mysocket';
if ( -e $socket_file ) {
unlink $socket_file or die "Could not delete socket file '$socket_file': $!";
}
my $sock = IO::Socket::UNIX->new(
LocalAddr => $socket_file,
Type => SOCK_STREAM,
Listen => 5, # listen to max 5 connections
) or die "Could not create socket: '$@'";
say "Created socket successfully..";
sleep 1;
close $sock;
say "Closed socket..";
exit;
输出为:
Could not create socket: '' at ./test.pl line 10.
所以问题是:为什么IO::Socket::UNIX->new()
失败,为什么失败时不设置$@
?
最佳答案
它没有设置$@
,因为IO::Socket::UNIX
在任何地方都不使用eval
。
如果您打印 $!
而不是 $@
,您可能会看到:
Invalid argument at xxx.pl line 10.
此时您可以查阅 IO::Socket::UNIX
docs并查看要传递的正确参数是 Local
而不是 LocalAddr
。
关于perl - 如何创建 Unix 域套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39379254/