[英]How to create a Unix Domain socket?
我正在嘗試學習如何使用套接字在同一台計算機上的進程之間進行通信。 我從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
文檔,並確定要傳遞的正確參數是Local
而不是LocalAddr
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.