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 | |
by Hero Zzyzzx (Curate) on Aug 01, 2001 at 18:48 UTC | |
by tadman (Prior) on Aug 01, 2001 at 19:05 UTC | |
by Hero Zzyzzx (Curate) on Aug 01, 2001 at 19:12 UTC | |
by tadman (Prior) on Aug 01, 2001 at 19:16 UTC |