User input in Perl with IO::Socket
- by David
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";