in reply to How can I test TCP socket status in Perl?

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!

Replies are listed 'Best First'.
Re^2: How can I test TCP socket status in Perl?
by tangent (Parson) on Jul 26, 2015 at 23:34 UTC
    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 thanos1983, i just picked up 127.0.0.1 and port 80 as an example in my question, my intention is to find out better way to test if an established connective is still active or not before sending data or to avoid opening another instance of same connection, if i can be able to call

        return (0) unless ($sock->connected()); return (0) unless (getpeername($sock));

        unfortunately i cant call to them without socket or is there a better way

Re^2: How can I test TCP socket status in Perl?
by beanscake (Acolyte) on Jul 26, 2015 at 23:20 UTC

    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; }
        thank you this worked work me.