#!/usr/bin/perl-thread -w

use IO::Socket;
use strict;
use Config;

$SIG{INT} = \&exit_prog;

print "welcome to kenix - perl version - by sam\n";

# socket creation
print "--- connection in process ...\n";
my $server = new IO::Socket::INET->new(
				       PeerPort => 15005,
				       PeerAddr => '127.0.0.1',
				       Type => SOCK_STREAM,
				       Proto => 'tcp')
    or die "can't connect to server : $@\n";
print "--- connected\n";

# select stuffs
my $rin = '';
my $win = '';
my $ein = '';

vec($rin, fileno(STDIN), 1) = 1;
vec($rin, fileno($server), 1) = 1;

while (select(my $rout = $rin, my $wout = $win, my $eout = $ein, undef) > 0) {
    if (vec($rout, fileno(STDIN), 1) == 1) {
	&write_sub;
    }
    if (vec($rout, fileno($server), 1) == 1) {
	&listen_sub;
    }
}

# subs
sub listen_sub {
    my $reponse=<$server>;
    if ($reponse =~ /^\/QUIT/i) {
	print "--- over\n";
	close($server);
	exit;
    }
    print ":$reponse";
}

sub write_sub {
    my $ligne = <STDIN>;
    print $server "$ligne";
}

# ctrl+c handler
sub exit_prog {
    print $server "/quit\n";
    close($server);
    exit;
}
# writed by sam <sam@nova-mag.org>


