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

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?

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

    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

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

        Perhaps you should go find the source code of the connected() method.

        sub connected { @_ == 1 or croak 'usage: $sock->connected()'; my($sock) = @_; getpeername($sock); }

        - tye        

        Hello again beanscake,

        Since you are running on MacOS and you have by default grep(1) - Linux man page and all sweet linux network tools nc(1) - Linux man page, why do not implement your own way of checking active tcp/udp ports based on the server port.

        Sample of code with output:

        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $serverPort = 12345; my @ActivePorts = `netstat -a | grep $serverPort`; # Strip white spaces @ActivePorts = map { s/^\s+|\s+$//g; $_; } @ActivePorts; print Dumper \@ActivePorts; __END__ $VAR1 = [ 'udp 0 0 localhost:12345 *:*' ];

        I am running a UDP server localy at the moment and I can see the active port that listens to my connections.

        So based on the output you can either terminate them or simply use them.

        Update: Even faster than nc is ss(8) - Linux man page that provides you also pid without sudo.

        Sample of code with output:

        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $serverPort = 12345; my @ActivePorts = `ss -lp | grep 12345`; # Strip white spaces @ActivePorts = map { s/^\s+|\s+$//g; $_; } @ActivePorts; print Dumper \@ActivePorts; __END__ $VAR1 = [ 'tcp UNCONN 0 0 127.0.0.1:ipproto-123 +45 *:* users:(("perl",8179,3))' ];

        Hope this helps.

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