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

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

Replies are listed 'Best First'.
Re^5: How can I test TCP socket status in Perl? (voodoo)
by tye (Sage) on Jul 27, 2015 at 03:13 UTC
    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        

Re^5: How can I test TCP socket status in Perl?
by thanos1983 (Parson) on Jul 27, 2015 at 00:27 UTC

    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!

      Hi thanos1983, ss doesn't exist on my Mac OSX, and no Macport available ... FYI

      The way forward always starts with a minimal test.

        Hello 1nickt

        Nice to know, but this is really strange. There is no way to install it? Well maybe in future it will be added.

        Thanks again, for your time and effort to inform me.

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