#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Test::More tests => 1; use IO::Socket; my $expected_closed_port = 111; my $dead_socket = IO::Socket::INET->new(PeerAddr => 'localhost', PeerPort => $expected_closed_port, Proto => "udp"); if($dead_socket) { # It could be a UDP connection, and there's no such concept as UP or DOWN, so we need to try sending an # empty datagram, and check for an error $dead_socket->send(""); my $foo; setsockopt( $dead_socket, SOL_SOCKET, SO_RCVTIMEO, pack('L!L!', +10, 0) ); my $return_value = recv($dead_socket, $foo, 1, 0); if(defined($return_value)) { ok(0, "I shouldn't be able to connect to port $expected_closed_port on localhost"); } else { ok(1, "I shouldn't be able to connect to port $expected_closed_port on localhost"); } }