views:

62

answers:

1

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";
+1  A: 

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.

Eric Strom
Probably want to remove the single-quotes from `$host` and `$port` :)
friedo
Thanks, good catch, I looked right past them...
Eric Strom