Perl 中使用 IO::Socket 的用户输入

发布于 2024-08-31 06:04:13 字数 955 浏览 11 评论 0原文

我正在尝试制作一个 perl 程序,它允许用户输入主机和外部主机的端口号以使用 IO::Socket 连接。它允许我运行程序并输入主机和端口,但它永远不会连接并显示“无法连接到位于 c:\users\USER\Documents\code\perl\sql.pl 第 18 行、第 2 行的 [主机] ”。下面显示的这段代码我做错了什么?另外,我如何在主机上进行输入验证(可以是主机名或 IP 地址)?非常感谢!下面的代码

use IO::Socket
print "Host to connect to: ";
chomp ($host = <STDIN>);
print "Port to connect with: ";
chomp ($port = <STDIN>);
while(($port > 65535) || ($port <= 0)){
    print "Port to connect with [Port > 0 < 65535] : ";
    chomp ($port = <STDIN>);
    }
print "\nConnecting to host $host on port $port\n";
$socket = new IO::Socket::INET (
                                  LocalHost => '$host',
                                  LocalPort => '$port',
                                  Proto => 'tcp',
                                  Listen => 5,
                                  Reuse => 1
                               );
die "Could not connect to  $host";

I am trying to make a perl program which allows a user to input the host and the port number of a foreign host to connect to using IO::Socket. It allows me to run the program and input a host and a port but it never connects and says "Could not connect to [host] at c:\users\USER\Documents\code\perl\sql.pl line 18, line 2." What am i doing wrong with this code shown below? And also, how can i have input validation on my host, which can either be a host name or an ip address? Thanks a bunch! Code Below

use IO::Socket
print "Host to connect to: ";
chomp ($host = <STDIN>);
print "Port to connect with: ";
chomp ($port = <STDIN>);
while(($port > 65535) || ($port <= 0)){
    print "Port to connect with [Port > 0 < 65535] : ";
    chomp ($port = <STDIN>);
    }
print "\nConnecting to host $host on port $port\n";
$socket = new IO::Socket::INET (
                                  LocalHost => '$host',
                                  LocalPort => '$port',
                                  Proto => 'tcp',
                                  Listen => 5,
                                  Reuse => 1
                               );
die "Could not connect to  $host";

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

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

发布评论

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

评论(1

夜空下最亮的亮点 2024-09-07 06:04:14

您对 IO::Socket::INET 的设置正在创建一个侦听端口(也称为服务器)。要创建客户端,请尝试:

$socket = IO::Socket::INET->new(
              PeerAddr => $host,
              PeerPort => $port,
              Proto => 'tcp'
          );

以下是 IO::Socket:: 的完整文档INET

The settings you have for IO::Socket::INET are creating a listening port (aka a server). To create a client try:

$socket = IO::Socket::INET->new(
              PeerAddr => $host,
              PeerPort => $port,
              Proto => 'tcp'
          );

Here is the full documentation for IO::Socket::INET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文