Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

IO::Socket hanging during requests

by sch (Pilgrim)
on Oct 20, 2002 at 00:17 UTC ( [id://206591]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, hoping someone could cast an eye over this and let me know if you have any thoughts as to whats going on.

In the following piece of code %servers is a hash where the key is an ip address and the value is a port (I don't think it's particularly important to the problem at hand, but the servers are opennap servers).I'm trying to setup connections to these servers using IO::Socket and have been seeing something strange - when the code loops through the %servers hash, every couple of iterations it hangs for a minute or so. Can anyone suggest why this might be happening. The code is as follows:

foreach (keys(%servers)) { print "Connecting to $_ :: $servers{$_}: "; my $socket; $socket = IO::Socket::INET->new( PeerAddr => $_, PeerPort => $servers{$_}, Proto => "tcp", Reuse => 1, Type => SOCK_STREAM); if (! defined ($socket)) { print "$@\n"; next; } print "Connected\n"; close $socket; $socket->DESTROY; print "..\n"; }

When it runs I get something like:

Connecting to 24.164.137.84 :: 3456: IO::Socket::INET: connect: Connec +tion refused Connecting to 62.163.22.155 :: 4200: Connected .. (hangs here) Connecting to 80.117.254.113 :: 8888: IO::Socket::INET: connect: Conne +ction refused Connecting to 62.226.24.21 :: 8888: IO::Socket::INET: connect: Connect +ion refused (hangs here) Connecting to 80.134.24.185 :: 8888: IO::Socket::INET: connect: Connec +tion refused Connecting to 217.84.215.47 :: 7777: IO::Socket::INET: connect: Connec +tion refused Connecting to 80.133.122.25 :: 7777: IO::Socket::INET: connect: Connec +tion refused Connecting to 213.84.39.165 :: 4567: IO::Socket::INET: connect: Connec +tion refused Connecting to 24.156.72.65 :: 513: IO::Socket::INET: connect: Connecti +on refused (hangs here)

But today you took me walking, Through a land that we have lost,
While our children sit at websites, With no access to the cost

Replies are listed 'Best First'.
Re: IO::Socket hanging during requests
by fokat (Deacon) on Oct 20, 2002 at 01:29 UTC
    TCP involves protocol handshakes when you setup and tear down a connection. In the case where you're connected to the remote server, the delay might be related to the end points (ie, your machine and the remote server) negotiating the closing of the socket.

    Another possible source for the delay could be the buffering that happens automagically on STDOUT. You can get rid of it by saying something like $| = 1; near the beginning of your script. See if this makes the delays go away.

Re: IO::Socket hanging during requests
by sch (Pilgrim) on Oct 20, 2002 at 05:26 UTC

    After playing around some, I think I found out what's going on - the pauses are where there's no server at the other end, so I guess one of three things was happening:

    1. We get a connection so we don't get a hang
    2. The connection is refused, again we don't get a hang
    3. There's no server at the requested address, so it sits waiting for a response, doesn't get one and eventually times out

    But today you took me walking, Through a land that we have lost,
    While our children sit at websites, With no access to the cost

      If there is no server at the other end, wont you get a connection refused message?

      In my client socket code, I always eval the calls to the socket so I can timeout if there is any problems..

        I'm not a 100% certain here, but I thought you only got a connection refused if there was a server at the other end to refuse it.

        But today you took me walking, Through a land that we have lost,
        While our children sit at websites, With no access to the cost

Re: IO::Socket hanging during requests
by sch (Pilgrim) on Oct 20, 2002 at 00:18 UTC

    BTW, when I copied and pasted the code section it had tabs - any idea why they've disappeared in the original node?

    But today you took me walking, Through a land that we have lost,
    While our children sit at websites, With no access to the cost

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://206591]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-24 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found