Fixes related to working local $.
[p5sagit/p5-mst-13.2.git] / t / lib / io_udp.t
1 #!./perl
2
3 BEGIN {
4     unless(grep /blib/, @INC) {
5         chdir 't' if -d 't';
6         unshift @INC, '../lib' if -d '../lib';
7     }
8 }
9
10 use Config;
11
12 BEGIN {
13     if(-d "lib" && -f "TEST") {
14         my $reason;
15
16         if ($Config{'extensions'} !~ /\bSocket\b/) {
17           $reason = 'Socket was not built';
18         }
19         elsif ($Config{'extensions'} !~ /\bIO\b/) {
20           $reason = 'IO was not built';
21         }
22         elsif ($^O eq 'os2') {
23           $reason = "blocks on OS/2, not debugged yet";
24         }
25         elsif ($^O eq 'apollo') {
26           $reason = "unknown *FIXME*";
27         }
28         undef $reason if $^O eq 'VMS' and $Config{d_socket};
29         if ($reason) {
30             print "1..0 # Skip: $reason\n";
31             exit 0;
32         }
33     }
34 }
35
36 sub compare_addr {
37     my $a = shift;
38     my $b = shift;
39     my @a = unpack_sockaddr_in($a);
40     my @b = unpack_sockaddr_in($b);
41     "$a[0]$a[1]" eq "$b[0]$b[1]";
42 }
43
44 $| = 1;
45 print "1..7\n";
46
47 use Socket;
48 use IO::Socket qw(AF_INET SOCK_DGRAM INADDR_ANY);
49
50     # This can fail if localhost is undefined or the
51     # special 'loopback' address 127.0.0.1 is not configured
52     # on your system. (/etc/rc.config.d/netconfig on HP-UX.)
53     # As a shortcut (not recommended) you could change 'localhost'
54     # here to be the name of this machine eg 'myhost.mycompany.com'.
55
56 $udpa = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
57     or die "$! (maybe your system does not have the 'localhost' address defined)";
58
59 print "ok 1\n";
60
61 $udpb = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
62     or die "$! (maybe your system does not have the 'localhost' address defined)";
63
64 print "ok 2\n";
65
66 $udpa->send("ok 4\n",0,$udpb->sockname);
67
68 print "not " unless compare_addr($udpa->peername,$udpb->sockname);
69 print "ok 3\n";
70
71 my $where = $udpb->recv($buf="",5);
72 print $buf;
73
74 my @xtra = ();
75
76 unless(compare_addr($where,$udpa->sockname)) {
77     print "not ";
78     @xtra = (0,$udpa->sockname);
79 }
80 print "ok 5\n";
81
82 $udpb->send("ok 6\n",@xtra);
83 $udpa->recv($buf="",5);
84 print $buf;
85
86 print "not " if $udpa->connected;
87 print "ok 7\n";