我正在尝试制作一个 perl 程序,它允许用户输入主机和外部主机的端口号以使用 IO::Socket 连接。它允许我运行程序并输入主机和端口,但它从不连接并说“无法连接到 c:\users\USER\Documents\code\perl\sql.pl 第 18 行第 2 行的 [host] 。” 下面显示的这段代码我做错了什么?而且,我怎样才能在我的主机上进行输入验证,它可以是主机名或 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";