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