TCP Client/Server
################  Single-Threaded Client/Server Demo  #################
#!/usr/bin/perl -w

#  I am the same client as in the TCP Client/Server handout!

use IO::Socket;

$socket = IO::Socket::INET->new(PeerAddr => "challenger.atc.fhda.edu",
                                PeerPort => 3682,
                                Proto    => 'tcp',
                                Type     => SOCK_STREAM)
                      or
die "Cannot open socket!\n";

$SIG{PIPE} = 'handler';    #  SIGPIPE if server end of socket is gone when                                 #  we try to write to it.

#  Trap terminal signals so that we can send a quit packet to the server.
$SIG{INT}  = 'handler';  
$SIG{QUIT} = 'handler'; 

while (print("Enter line: "), ($outbuf = <STDIN>) !~ /^\s*quit\s*$/i)
{
     print $socket $outbuf;
     $inbuf = <$socket>;
     chomp $inbuf;
     print "Got \"$inbuf\" from server!\n\n";
}

#  Send that quit packet!!
print $socket "quit\n";   #  Send QUIT packet to server.
close($socket);



sub handler
{
    my ($signo) = shift;

    if ($signo eq "INT" || $signo eq "QUIT")
    {
#  Send the quit packet on a terminal interrupt!
         print $socket "quit\n"; 
         close($socket);
         exit(1);
    }
    else  #  SIGPIPE!!
    {
         print "Server died\n";
         exit(2);
    }
}




#!/usr/bin/perl -w

#################  Single-Threaded Server with Select  ##################

use IO::Socket;      
use IO::Select;

$passive = IO::Socket::INET->new(LocalPort => 3682,
                                 Proto     => 'tcp',
                                 Type      => SOCK_STREAM,
                                 Reuse     => 1,
                                 Listen    => 10)
             or
die "Could not open socket!\n";

$sockset = new IO::Select();   #  Make a new passive socket.
$sockset->add($passive);       #  Add it to a socket set.

while(1)
{
      ($ready) = IO::Select->select($sockset, undef, undef);
      foreach $sock (@$ready)     #  Which sockets in $sockset have input?
      {
          if ($sock == $passive)  #  Passive socket has new client waiting.
          {
                $active = $passive->accept();  #  Create active socket
                $sockset->add($active);        #  for new client.
          }
          else
          {
                $inline = <$sock>;
                if ($inline eq "quit\n") {
                     $sockset->remove($sock);  #  Remove client socket!!
                     print "Server removed a client from socket set!\n";
                }
                else {
                     print $sock $inline;
                }
          }
      }      
}





########################  Output Session With Server  ####################

$ sels.pl&
[1]     966387

$ selc.pl
Enter line: hello
Got "hello" from server!

Enter line: [2] + Stopped (SIGTSTP) selc.pl   #  Stop this client!!

$ selc.pl   #  Start another!!
Enter line: what
Got "what" from server!

Enter line: when
Got "when" from server!

Enter line: quit   #  Proof that server is multi-client!!

$ Server removed a client from socket set!  #  Server removed active
                                            #  client socket from $sockset
                                            #  after client "quit".

$ fg     #  Bring old client back to life!!
selc.pl
hello
Got "hello" from server!   #  Yes, server still knows it's there!!

Enter line: when
Got "when" from server!

Enter line: who
Got "who" from server!

Enter line: quit
Server removed a client from socket set!  #  Server removes socket from
                                          #  $sockset.
$ fg   #  Lets get rid of server. 
sels.pl   #  CTRL-C the server.