4 unless(grep /blib/, @INC) {
13 my $can_fork = $Config{d_fork} ||
14 (($^O eq 'MSWin32' || $^O eq 'NetWare') and
15 $Config{useithreads} and
16 $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/
19 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
20 $reason = 'Socket extension unavailable';
22 elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
23 $reason = 'IO extension unavailable';
29 print "1..0 # Skip: $reason\n";
34 my $has_perlio = $] >= 5.008 && find PerlIO::Layer 'perlio';
40 $SIG{ALRM} = sub { die; };
46 $listen = IO::Socket::INET->new(Listen => 2,
48 # some systems seem to need as much as 10,
49 # so be generous with the timeout
55 # Check if can fork with dynamic extensions (bug in CRT):
57 system "$^X -I../lib -MOpcode -e 'defined fork or die' > /dev/null 2>&1") {
58 print "ok $_ # skipped: broken fork\n" for 2..5;
62 $port = $listen->sockport;
66 $sock = $listen->accept() or die "accept failed: $!";
70 print $sock->getline();
80 } elsif(defined $pid) {
82 $sock = IO::Socket::INET->new(PeerPort => $port,
84 PeerAddr => 'localhost'
86 || IO::Socket::INET->new(PeerPort => $port,
88 PeerAddr => '127.0.0.1'
90 or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
96 print $sock->getline();
105 # Test various other ways to create INET sockets that should
107 $listen = IO::Socket::INET->new(Listen => '', Timeout => 15) or die "$!";
108 $port = $listen->sockport;
113 last SERVER_LOOP unless $sock = $listen->accept;
115 last SERVER_LOOP if /^quit/;
122 } elsif (defined $pid) {
123 # child, try various ways to connect
124 $sock = IO::Socket::INET->new("localhost:$port")
125 || IO::Socket::INET->new("127.0.0.1:$port");
127 print "not " unless $sock->connected;
129 $sock->print("ok 7\n");
132 $sock->print("ok 9\n");
133 $sock->print("done\n");
144 # some machines seem to suffer from a race condition here
147 $sock = IO::Socket::INET->new("127.0.0.1:$port");
149 $sock->print("ok 10\n");
150 $sock->print("done\n");
158 # some machines seem to suffer from a race condition here
161 $sock = IO::Socket->new(Domain => AF_INET,
162 PeerAddr => "localhost:$port")
163 || IO::Socket->new(Domain => AF_INET,
164 PeerAddr => "127.0.0.1:$port");
166 $sock->print("ok 11\n");
167 $sock->print("quit\n");
178 # Then test UDP sockets
179 $server = IO::Socket->new(Domain => AF_INET,
181 LocalAddr => 'localhost')
182 || IO::Socket->new(Domain => AF_INET,
184 LocalAddr => '127.0.0.1');
185 $port = $server->sockport;
189 $server->recv($buf, 100);
191 } elsif (defined($pid)) {
193 $sock = IO::Socket::INET->new(Proto => 'udp',
194 PeerAddr => "localhost:$port")
195 || IO::Socket::INET->new(Proto => 'udp',
196 PeerAddr => "127.0.0.1:$port");
197 $sock->send("ok 12\n");
199 $sock->send("ok 12\n"); # send another one to be sure
205 print "not " unless $server->blocking;
208 if ( $^O eq 'qnx' ) {
209 # QNX4 library bug: Can set non-blocking on socket, but
210 # cannot return that status.
211 print "ok 14 # skipped on QNX4\n";
213 $server->blocking(0);
214 print "not " if $server->blocking;
219 ### Set up some data to be transfered between the server and
220 ### the client. We'll use own source code ...
223 if( !open( SRC, "< $0")) {
224 print "not ok 15 - $!\n";
234 my $listen = IO::Socket::INET->new( Listen => 2, Proto => 'tcp', Timeout => 15) ||
237 die if( !defined( $listen));
238 my $serverport = $listen->sockport;
239 my $server_pid = fork();
242 ### TEST 17 Client/Server establishment
247 ### Get data from the server using a single stream
249 $sock = IO::Socket::INET->new("localhost:$serverport")
250 || IO::Socket::INET->new("127.0.0.1:$serverport");
253 $sock->print("send\n");
260 $sock->print("done\n");
263 print "not " if( @array != @data);
270 ### Get data from the server using a stream, which is
271 ### interrupted by eof calls.
272 ### On perl-5.7.0@7673 this failed in a SOCKS environment, because eof
273 ### did an getc followed by an ungetc in order to check for the streams
274 ### end. getc(3) got replaced by the SOCKS funktion, which ended up in
275 ### a recv(2) call on the socket, while ungetc(3) put back a character
276 ### to an IO buffer, which never again was read.
278 ### TESTS 19,20,21,22
279 ### Try to ping-pong some Unicode.
281 $sock = IO::Socket::INET->new("localhost:$serverport")
282 || IO::Socket::INET->new("127.0.0.1:$serverport");
285 print binmode($sock, ":utf8") ? "ok 19\n" : "not ok 19\n";
287 print "ok 19 - Skip: no perlio\n";
293 $sock->print("ping \x{100}\n");
294 chomp(my $pong = scalar <$sock>);
295 print $pong =~ /^pong (.+)$/ && $1 eq "\x{100}" ?
296 "ok 20\n" : "not ok 20\n";
298 $sock->print("ord \x{100}\n");
299 chomp(my $ord = scalar <$sock>);
300 print $ord == 0x100 ?
301 "ok 21\n" : "not ok 21\n";
303 $sock->print("chr 0x100\n");
304 chomp(my $chr = scalar <$sock>);
305 print $chr eq "\x{100}" ?
306 "ok 22\n" : "not ok 22\n";
308 print "ok $_ - Skip: no perlio\n" for 20..22;
311 $sock->print("send\n");
314 while( !eof( $sock ) ){
321 $sock->print("done\n");
324 print "not " if( @array != @data);
333 $sock = IO::Socket::INET->new("localhost:$serverport")
334 || IO::Socket::INET->new("127.0.0.1:$serverport");
337 $sock->print("done\n");
340 print "not " if( 1 != kill 0, $server_pid);
346 } elsif (defined($server_pid)) {
350 SERVER_LOOP: while (1) {
351 last SERVER_LOOP unless $sock = $listen->accept;
352 # Do not print ok/not ok for this binmode() since there's
353 # a race condition with our client, just die if we fail.
354 if ($has_perlio) { binmode($sock, ":utf8") or die }
356 last SERVER_LOOP if /^quit/;
359 print $sock "pong $1\n";
363 print $sock ord($1), "\n";
367 print $sock chr(hex($1)), "\n";
389 # test Blocking option in constructor
391 $sock = IO::Socket::INET->new(Blocking => 0)
395 if ( $^O eq 'qnx' ) {
396 print "ok 26 # skipped on QNX4\n";
397 # QNX4 library bug: Can set non-blocking on socket, but
398 # cannot return that status.
400 my $status = $sock->blocking;
401 print "not " unless defined $status && !$status;