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

please what is better way to determine if a socket is still active or closed here is what i have been able to come up with

use warnings; use strict; use IO::Socket::INET; my $sock; sub Connect_Server { $sock = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 80, Proto => 'tcp', Timeout => 8 ) or die("Connect failed: $@\n"); } sub AmStillConnected { return unless defined $sock; return unless $sock->connected(); return 1; } sub test_status { if (&AmStillConnected()){ print "am connected\n"; }elsif(!&AmStillConnected()){ print "not connected\n"; } } &Connect_Server(); &test_status();

Replies are listed 'Best First'.
Re: How can I test TCP socket status in Perl?
by thanos1983 (Parson) on Jul 26, 2015 at 22:59 UTC

    Hello beanscake,

    I think you can find more here: How to find out whether socket is still connected?.

    But it would be good to provide us with more information, such as:

    Sample of current output.

    Operating system (Windows, Linux, Mac).

    etc. anything you think relevant that can affect your question.

    Update:

    I executed your code and I got the following error:Connect failed: IO::Socket::INET: connect: Connection refused

    I think you need to start with some basics first. Read first this sort tutorial Perl, Sockets and TCP/IP Networking that I used the sample code provided under:

    Server:

    #!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '7070', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);

    Client:

    #!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; my $sock = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => '7070', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; print $sock "Hello there!\n"; close($sock);

    Output on Server:

    Hello there!

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Interesting thread thanos1983. The issue of whether getpeername is a reliable way to test the connection is not resolved though. The connected() method in IO::Socket simply returns the result of getpeername, and the doc for that says:
      Returns the packed sockaddr address of the other end of the SOCKET connection.
      So I suppose the question is, does the function getpeername actually test the connection with a send/receive, or does it just format information already received by the intial connection?

        Hello tangent,

        That is true, well there is only one way to find out Experimentation :D.

        Well beanscake says that on his OS it works fine, but I am getting and error when I experiment with his code. So I assume that this is only part of the code.

        Please beanscake provide us with an update on your question with full text code (complete Server and Client), so we can experiment more. I assume that the code provided is not complete since my test is failing and you are saying that on your OS is not.

        Seeking for Perl wisdom...on the process of learning...not there...yet!

      Thank you in advance, i want AmStillConnected() to return undef if not connected. here is output when not connected. 'Can't call method "connected" on an undefined value at Untitled 9.pl line 27.' but works when i first connect and test the status for this reason it's incomplete.. am running this on Mac, thank you again

        If $sock is not defined you are not connected, so you should test for that as well:
        sub AmStillConnected { return unless defined $sock; return unless $sock->connected; return 1; }