如何指定在 Perl 的 IO::Socket::INET 中使用哪个端口?

发布于 2024-08-31 08:26:02 字数 530 浏览 12 评论 0原文

我正在使用 IO::Socket::INET 创建进程间通信在我的程序中。我需要在 TCP 客户端中使用特定的端口号。我按照 Perl 文档中的示例进行操作,但它不起作用。这是我的代码:

旧代码(工作):

tx_socket = new IO::Socket::INET->new('127.0.0.1:8001') || die "Can't connect to 127.0.0.1:8001 : $!\n"; 

新代码(不工作):

tx_socket = new IO::Socket::INET->new('127.0.0.1:8001', LocalPort=>9000 ) || die "Can't connect to 127.0.0.1:8001 : $!\n"; 

有谁知道出了什么问题?

I am using IO::Socket::INET to create inter-process communication in my program. I need to use a specific port number in my TCP client. I was following the example in Perl doc, but it doesn't work. Here is my code:

old code(working):

tx_socket = new IO::Socket::INET->new('127.0.0.1:8001') || die "Can't connect to 127.0.0.1:8001 : $!\n"; 

new code(not working):

tx_socket = new IO::Socket::INET->new('127.0.0.1:8001', LocalPort=>9000 ) || die "Can't connect to 127.0.0.1:8001 : $!\n"; 

Does anyone know what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-09-07 08:26:02

如果您修复丢失的逗号,格兰特·麦克莱恩的答案是有效的,但这里的“有效”可能与您的期望有关。

use IO::Socket::INET;
$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
);
die("No socket!\n") unless $sock;
print "Socket good!\n";

运行此命令会产生:

No socket!

这并不是因为代码不起作用,而是按预期工作(在我的例子中)。也就是说,预计与本地主机端口 8001 的连接将失败,而另一端没有任何监听。这说明了错误报告的有用性:

use IO::Socket::INET;
$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
) or die("$!\n");
die("No socket!\n") unless $sock;
print "Socket good!\n";

现在运行会产生:

Connection refused

如果我在端口 8001 上运行 netcat 侦听,我会得到不同的结果:

Socket good!

Grant McLean's answer works, if you fix the missing comma, but "works" here may be relative to what you are expecting.

use IO::Socket::INET;
$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
);
die("No socket!\n") unless $sock;
print "Socket good!\n";

Running this yields:

No socket!

Which isn't because the code doesn't work, it's working as expected (in my case). That is, it's expected that a connection to a localhost port 8001 will fail with nothing listening on the other side. This illustrates the usefulness of error reporting:

use IO::Socket::INET;
$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
) or die("$!\n");
die("No socket!\n") unless $sock;
print "Socket good!\n";

Which running now yields:

Connection refused

If I run netcat listening on port 8001, I get a different result:

Socket good!
汐鸠 2024-09-07 08:26:02

根据 文档,您应该执行以下操作:

$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
) or die "Connect error: $!";

According to the documentation, you should be doing something like:

$sock = IO::Socket::INET->new(
    PeerAddr  => '127.0.0.1',
    PeerPort  => 8001,
    LocalPort => 9000,
    Proto     => 'tcp'
) or die "Connect error: $!";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文