# Fortune-Server

use strict;
use warnings;
use IO::Socket;

use constant MYPORT => 2000;
my $sock = '';
my $client = '';
my $data = 'fortunes';       # Sprueche-Datei

$sock = new IO::Socket::INET(LocalPort => MYPORT,
		             Reuse     => 1,
		             Listen    => 5)
    or die "can't create local socket: $@\n";

$SIG{'CHLD'} = sub { wait(); };    # Zombies verhindern

print "Accepting connections on Port ", MYPORT, "...\n";
while ($client = $sock->accept())
  {
  # Verbindung ist aufgebaut
  print "Accepted connection from ",
        $client->peerhost(), ":", $client->peerport(), "\n";

  # Jetzt kommt der Spruch
  my $sav = $/; $/ = "\n%\n";
  my $cookie;
  srand($$);
  open(KEKS,"<",$data) || die "Keine Kekse\n";
  rand($.) < 1 && ($cookie = $_) while <KEKS>;
  $cookie =~ s/%$//;
  close(KEKS);
  $/ = $sav;
  print $client "\n$cookie\n";
  $client->close;
  }