Perl 中使用 IO::Socket 的用户输入
我正在尝试制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对 IO::Socket::INET 的设置正在创建一个侦听端口(也称为服务器)。要创建客户端,请尝试:
以下是 IO::Socket:: 的完整文档INET。
The settings you have for
IO::Socket::INET
are creating a listening port (aka a server). To create a client try:Here is the full documentation for IO::Socket::INET.