Hero Zzyzzx has asked for the wisdom of the Perl Monks concerning the following question:

Monks
I'm trying to get this to work and It's driving me nuts. What I'm writing is a script to automate a set of whois lookups. My idea was to originally open a socket to whois.networksolutions.com and then reuse this socket for every lookup, reconnecting if needed.

However, it appears that I need to reconnect for every request via the socket. Am I fundamentally screwing something up about how sockets work, or is there a problem with my code?

INF is just a list of domains separated with newlines

#!/usr/bin/perl -wT # This is an excerpt, though it should work OK. use strict; use IO::Socket; use vars qw($socket); $socket = whoisconnect('whois.networksolutions.com',5); my $fileRead = "test.txt"; open(INF,$fileRead) or die("Could someone tell me where $fileRead is? +$! \n"); while (<INF>){ my $domain= $_; chomp($domain); if (!$socket->connected()){ # Test connection print "Not Connected, attempting to reconnect. . .\n"; $socket = whoisconnect('whois.networksolutions.com',5); } # If I uncomment the line below, this works beautifully. # $socket = whoisconnect('whois.networksolutions.com',5); $socket->send("$domain\n") or die ("Can't send data $!"); my @answer= <$socket>; foreach(@answer){ print $_; } } close(INF); sub whoisconnect{ my ($domain,$timeout)=@_; my $socket = IO::Socket::INET->new(PeerAddr => $domain, PeerPort => 43, Proto =>'tcp', Type => SOCK_STREAM, Timeout => $timeout, Reuse=>1 ) or die("Couldn't connect: $!\n"); return $socket; }

Now, this only works on the first domain sent from INF, and the connection test I'm trying doesn't return undef, rendering my connection test worthless.

If I put in a fresh connection (by uncommenting the indicated line) this works for every line of INF. What's going on? I was hoping to reuse a socket as a way to save a little speed. Is this a delusion?

Thanks!

Replies are listed 'Best First'.
Re: IO::Socket to me. . . or why won't you stay?
by tadman (Prior) on Aug 01, 2001 at 18:43 UTC
    As far as I know, most WHOIS servers hang up on you after each query, so you will have to reconnect explicitly. You can recycle the socket object by forcing it to reconnect, which should save you some overhead. Right now you're creating a whole new socket object each time.

    I'm not sure why the connected() function isn't operating correctly, but it might not be a good indicator anyway. Assume you're going to get ditched.

      Forgive me, but how would you force it to reconnect without creating a new socket? If it's true that a whois server hangs up after each query, then reconnecting without creating a new socket seems like a good compromise.

      Thanks, tadman.

        It seems that I'm making assumptions about the functionality of IO::Socket::INET that just aren't true. For example, you can't really reconnect unless you jump through a bunch of hoops. The $socket->connect() method, for example, is no more robust than the internal one. You need to resolve your addresses first using gethostbyaddr, which is inconvenient, to say the least. So it can be done, but the cost is too high to make it practical.

        Maybe this will be fixed in an upcoming release.

        In the interim, recreating the socket doesn't seem like such a big deal after all. It will certainly work.