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!


In reply to IO::Socket to me. . . or why won't you stay? by Hero Zzyzzx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.