如何指定在 Perl 的 IO::Socket::INET 中使用哪个端口?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您修复丢失的逗号,格兰特·麦克莱恩的答案是有效的,但这里的“有效”可能与您的期望有关。
运行此命令会产生:
这并不是因为代码不起作用,而是按预期工作(在我的例子中)。也就是说,预计与本地主机端口 8001 的连接将失败,而另一端没有任何监听。这说明了错误报告的有用性:
现在运行会产生:
如果我在端口 8001 上运行 netcat 侦听,我会得到不同的结果:
Grant McLean's answer works, if you fix the missing comma, but "works" here may be relative to what you are expecting.
Running this yields:
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:
Which running now yields:
If I run netcat listening on port 8001, I get a different result:
根据 文档,您应该执行以下操作:
According to the documentation, you should be doing something like: