1#!./perl
2
3BEGIN {
4    require($ENV{PERL_CORE} ? '../../t/test.pl' : './t/test.pl');
5
6    use Config;
7    my $reason;
8    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
9      $reason = 'Socket was not built';
10    }
11    elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
12      $reason = 'IO was not built';
13    }
14    undef $reason if $^O eq 'VMS' and $Config{d_socket};
15    skip_all($reason) if $reason;
16}
17
18sub compare_addr {
19    no utf8;
20    my $a = shift;
21    my $b = shift;
22    if (length($a) != length $b) {
23	my $min = (length($a) < length $b) ? length($a) : length $b;
24	if ($min and substr($a, 0, $min) eq substr($b, 0, $min)) {
25	    printf "# Apparently: %d bytes junk at the end of %s\n# %s\n",
26		abs(length($a) - length ($b)),
27		$_[length($a) < length ($b) ? 1 : 0],
28		"consider decreasing bufsize of recfrom.";
29	    substr($a, $min) = "";
30	    substr($b, $min) = "";
31	}
32	return 0;
33    }
34    my @a = unpack_sockaddr_in($a);
35    my @b = unpack_sockaddr_in($b);
36    "$a[0]$a[1]" eq "$b[0]$b[1]";
37}
38
39plan(7);
40watchdog(15);
41
42use Socket;
43use IO::Socket qw(AF_INET SOCK_DGRAM INADDR_ANY);
44
45$udpa = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
46     || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
47    or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
48ok(1);
49
50$udpb = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
51     || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
52    or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
53ok(1);
54
55$udpa->send('BORK', 0, $udpb->sockname);
56
57ok(compare_addr($udpa->peername,$udpb->sockname, 'peername', 'sockname'));
58
59my $where = $udpb->recv($buf="", 4);
60is($buf, 'BORK');
61
62my @xtra = ();
63
64if (! ok(compare_addr($where,$udpa->sockname, 'recv name', 'sockname'))) {
65    @xtra = (0, $udpa->sockname);
66}
67
68$udpb->send('FOObar', @xtra);
69$udpa->recv($buf="", 6);
70is($buf, 'FOObar');
71
72ok(! $udpa->connected);
73
74exit(0);
75
76# EOF
77