1# -*- perl -*-
2#
3#   $Id: single.t,v 1.2 1999/08/12 14:28:59 joe Exp $
4#
5
6require 5.004;
7use strict;
8
9use IO::Socket ();
10use Config ();
11use Net::Daemon::Test ();
12
13my $numTests = 5;
14
15
16my($handle, $port);
17if (@ARGV) {
18    $port = shift @ARGV;
19} else {
20    ($handle, $port) =
21	Net::Daemon::Test->Child($numTests,
22					      $^X, '-Iblib/lib', '-Iblib/arch',
23					      't/server', '--mode=single',
24					      '--timeout', 60);
25}
26
27print "Making first connection to port $port...\n";
28my $fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
29			       'PeerPort' => $port);
30printf("%s 1\n", $fh ? "ok" : "not ok");
31printf("%s 2\n", $fh->close() ? "ok" : "not ok");
32print "Making second connection to port $port...\n";
33$fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
34			    'PeerPort' => $port);
35printf("%s 3\n", $fh ? "ok" : "not ok");
36my($ok) = $fh ? 1 : 0;
37for (my $i = 0;  $ok  &&  $i < 20;  $i++) {
38    print "Writing number: $i\n";
39    if (!$fh->print("$i\n")  ||  !$fh->flush()) { $ok = 0; last; }
40    print "Written.\n";
41    my($line) = $fh->getline();
42    print "line = ", (defined($line) ? $line : "undef"), "\n";
43    if (!defined($line)) { $ok = 0;  last; }
44    if ($line !~ /(\d+)/  ||  $1 != $i*2) { $ok = 0;  last; }
45}
46printf("%s 4\n", $ok ? "ok" : "not ok");
47printf("%s 5\n", $fh->close() ? "ok" : "not ok");
48
49END {
50    if ($handle) { $handle->Terminate() }
51    if (-f "ndtest.prt") { unlink "ndtest.prt" }
52}
53