1BEGIN {
2  if ($ENV{PERL_CORE}) {
3    unless ($ENV{PERL_TEST_Net_Ping}) {
4      print "1..0 # Skip: network dependent test\n";
5        exit;
6    }
7  }
8  unless (eval "require Socket") {
9    print "1..0 \# Skip: no Socket\n";
10    exit;
11  }
12  if (my $port = getservbyname('echo', 'tcp')) {
13    socket(*ECHO, &Socket::PF_INET(), &Socket::SOCK_STREAM(), (getprotobyname 'tcp')[2]);
14    unless (connect(*ECHO, scalar &Socket::sockaddr_in($port, &Socket::inet_aton("localhost")))) {
15      print "1..0 \# Skip: loopback tcp echo service is off ($!)\n";
16      exit;
17    }
18    close (*ECHO);
19  } else {
20    print "1..0 \# Skip: no echo port\n";
21    exit;
22  }
23}
24
25# Test of stream protocol using loopback interface.
26#
27# NOTE:
28#   The echo service must be enabled on localhost
29#   to really test the stream protocol ping.  See
30#   the end of this document on how to enable it.
31
32use Test;
33use Net::Ping;
34plan tests => 22;
35
36my $p = new Net::Ping "stream";
37
38# new() worked?
39ok !!$p;
40
41# Attempt to connect to the echo port
42ok ($p -> ping("localhost"));
43
44# Try several pings while it is connected
45for (1..20) {
46  select (undef,undef,undef,0.1);
47  ok $p -> ping("localhost");
48}
49
50__END__
51
52A simple xinetd configuration to enable the echo service can easily be made.
53Just create the following file before restarting xinetd:
54
55/etc/xinetd.d/echo:
56
57# description: An echo server.
58service echo
59{
60        type            = INTERNAL
61        id              = echo-stream
62        socket_type     = stream
63        protocol        = tcp
64        user            = root
65        wait            = no
66        disable         = no
67}
68
69
70Or if you are using inetd, before restarting, add
71this line to your /etc/inetd.conf:
72
73echo   stream  tcp     nowait  root    internal
74