4 unless(grep /blib/, @INC) {
14 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
15 $reason = 'Socket extension unavailable';
17 elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
18 $reason = 'IO extension unavailable';
20 elsif (! $Config{'d_fork'}) {
24 print "1..0 # Skip: $reason\n";
29 my $has_perlio = $] >= 5.008 && find PerlIO::Layer 'perlio';
35 $SIG{ALRM} = sub { die; };
41 $listen = IO::Socket::INET->new(Listen => 2,
43 # some systems seem to need as much as 10,
44 # so be generous with the timeout
50 # Check if can fork with dynamic extensions (bug in CRT):
52 system "$^X -I../lib -MOpcode -e 'defined fork or die' > /dev/null 2>&1") {
53 print "ok $_ # skipped: broken fork\n" for 2..5;
57 $port = $listen->sockport;
61 $sock = $listen->accept() or die "accept failed: $!";
65 print $sock->getline();
75 } elsif(defined $pid) {
77 $sock = IO::Socket::INET->new(PeerPort => $port,
79 PeerAddr => 'localhost'
81 || IO::Socket::INET->new(PeerPort => $port,
83 PeerAddr => '127.0.0.1'
85 or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
91 print $sock->getline();
100 # Test various other ways to create INET sockets that should
102 $listen = IO::Socket::INET->new(Listen => '', Timeout => 15) or die "$!";
103 $port = $listen->sockport;
108 last SERVER_LOOP unless $sock = $listen->accept;
110 last SERVER_LOOP if /^quit/;
117 } elsif (defined $pid) {
118 # child, try various ways to connect
119 $sock = IO::Socket::INET->new("localhost:$port")
120 || IO::Socket::INET->new("127.0.0.1:$port");
122 print "not " unless $sock->connected;
124 $sock->print("ok 7\n");
127 $sock->print("ok 9\n");
128 $sock->print("done\n");
139 # some machines seem to suffer from a race condition here
142 $sock = IO::Socket::INET->new("127.0.0.1:$port");
144 $sock->print("ok 10\n");
145 $sock->print("done\n");
153 # some machines seem to suffer from a race condition here
156 $sock = IO::Socket->new(Domain => AF_INET,
157 PeerAddr => "localhost:$port")
158 || IO::Socket->new(Domain => AF_INET,
159 PeerAddr => "127.0.0.1:$port");
161 $sock->print("ok 11\n");
162 $sock->print("quit\n");
173 # Then test UDP sockets
174 $server = IO::Socket->new(Domain => AF_INET,
176 LocalAddr => 'localhost')
177 || IO::Socket->new(Domain => AF_INET,
179 LocalAddr => '127.0.0.1');
180 $port = $server->sockport;
184 $server->recv($buf, 100);
186 } elsif (defined($pid)) {
188 $sock = IO::Socket::INET->new(Proto => 'udp',
189 PeerAddr => "localhost:$port")
190 || IO::Socket::INET->new(Proto => 'udp',
191 PeerAddr => "127.0.0.1:$port");
192 $sock->send("ok 12\n");
194 $sock->send("ok 12\n"); # send another one to be sure
200 print "not " unless $server->blocking;
203 if ( $^O eq 'qnx' ) {
204 # QNX4 library bug: Can set non-blocking on socket, but
205 # cannot return that status.
206 print "ok 14 # skipped on QNX4\n";
208 $server->blocking(0);
209 print "not " if $server->blocking;
214 ### Set up some data to be transfered between the server and
215 ### the client. We'll use own source code ...
218 if( !open( SRC, "< $0")) {
219 print "not ok 15 - $!\n";
229 my $listen = IO::Socket::INET->new( Listen => 2, Proto => 'tcp', Timeout => 15) ||
232 die if( !defined( $listen));
233 my $serverport = $listen->sockport;
234 my $server_pid = fork();
237 ### TEST 17 Client/Server establishment
242 ### Get data from the server using a single stream
244 $sock = IO::Socket::INET->new("localhost:$serverport")
245 || IO::Socket::INET->new("127.0.0.1:$serverport");
248 $sock->print("send\n");
255 $sock->print("done\n");
258 print "not " if( @array != @data);
265 ### Get data from the server using a stream, which is
266 ### interrupted by eof calls.
267 ### On perl-5.7.0@7673 this failed in a SOCKS environment, because eof
268 ### did an getc followed by an ungetc in order to check for the streams
269 ### end. getc(3) got replaced by the SOCKS funktion, which ended up in
270 ### a recv(2) call on the socket, while ungetc(3) put back a character
271 ### to an IO buffer, which never again was read.
273 ### TESTS 19,20,21,22
274 ### Try to ping-pong some Unicode.
276 $sock = IO::Socket::INET->new("localhost:$serverport")
277 || IO::Socket::INET->new("127.0.0.1:$serverport");
280 print binmode($sock, ":utf8") ? "ok 19\n" : "not ok 19\n";
282 print "ok 19 - Skip: no perlio\n";
288 $sock->print("ping \x{100}\n");
289 chomp(my $pong = scalar <$sock>);
290 print $pong =~ /^pong (.+)$/ && $1 eq "\x{100}" ?
291 "ok 20\n" : "not ok 20\n";
293 $sock->print("ord \x{100}\n");
294 chomp(my $ord = scalar <$sock>);
295 print $ord == 0x100 ?
296 "ok 21\n" : "not ok 21\n";
298 $sock->print("chr 0x100\n");
299 chomp(my $chr = scalar <$sock>);
300 print $chr eq "\x{100}" ?
301 "ok 22\n" : "not ok 22\n";
303 print "ok $_ - Skip: no perlio\n" for 20..22;
306 $sock->print("send\n");
309 while( !eof( $sock ) ){
316 $sock->print("done\n");
319 print "not " if( @array != @data);
328 $sock = IO::Socket::INET->new("localhost:$serverport")
329 || IO::Socket::INET->new("127.0.0.1:$serverport");
332 $sock->print("done\n");
335 print "not " if( 1 != kill 0, $server_pid);
341 } elsif (defined($server_pid)) {
345 SERVER_LOOP: while (1) {
346 last SERVER_LOOP unless $sock = $listen->accept;
347 # Do not print ok/not ok for this binmode() since there's
348 # a race condition with our client, just die if we fail.
349 if ($has_perlio) { binmode($sock, ":utf8") or die }
351 last SERVER_LOOP if /^quit/;
354 print $sock "pong $1\n";
358 print $sock ord($1), "\n";
362 print $sock chr(hex($1)), "\n";
384 # test Blocking option in constructor
386 $sock = IO::Socket::INET->new(Blocking => 0)
390 if ( $^O eq 'qnx' ) {
391 print "ok 26 # skipped on QNX4\n";
392 # QNX4 library bug: Can set non-blocking on socket, but
393 # cannot return that status.
395 my $status = $sock->blocking;
396 print "not " unless defined $status && !$status;