All pastes #282457 Raw Edit

Miscellany

public text v1 · immutable
#282457 ·published 2006-12-17 16:30 UTC
rendered paste body
use IO::Socket;
use IO::Select;
my $listener = 
  IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
die "Can't create socket for listening: $!" unless $listener;
print "Listening for connections on port 8008\n";

my $readable = IO::Select->new;     # Create a new IO::Select object
$readable->add($listener);          # Add the listener to it
my %names;           #Holds all the names, connected to their sockets
                     #Connection => Name
my %names2;          #Name => Connection number
#my %connects;       #
my $count;           #Used to display how many people have identified
my %connects;        #Holds the connection number of the current user
                     #Connection => Connection number
my $connect;         #Temp var for every loop. Tells what number the connection is
my $connect_count = 0;   #A counter to assign the connection number
my @msgs;            #Holds messages

while(1) {
    # Get a list of sockets that are ready to talk to us.
    my ($ready) = IO::Select->select($readable, undef, undef, undef);
    foreach my $s (@$ready) {
        # Is it a new connection?
        if($s == $listener) {
            # Accept the connection and add it to our readable list.
            my $new_sock = $listener->accept;
            $readable->add($new_sock) if $new_sock;
            print $new_sock "Hello, Enter your name\r\n";
        } else {  # It's an established connection
            $connect = $connects{$s};
            if (defined $msgs[$connect]) {
                print $s "$msgs[$connect]";
            }
            my $buf = <$s>;   # Try to read a line
            # Was there anyone on the other end?
            if( defined $buf ) {
                #if they havent yet entered their name, the first thing they enter will be their name
                if (!defined $names{$s}) {
                    #chomp $buf;
                    $buf =~ /^(\w+)/;
                    #print "$1\n";
                    $names{$s} = "$1";
                    print $s "Name accepted\r\n";
                    $count = keys %names;
                    print $s "There are $count people connected\r\n";
                    @name = values %names;
                    print $s "@name\r\n";
                    $connect_count++;
                    $connects{$s} = $connect_count;
                    $names2{$buf} = $connect_count;
                    #%connects  = reverse %names
                } elsif ($buf =~ /goodbye/i) {
                    #if they think the server is not good enough for them they can leave
                    #and this is where they do it
                    print $s "See you later!\n";
                    #$names{$s} = undef;
                    delete $names{$s};
                    #%connects  = reverse %names
                    $readable->remove($s);
                    $s->close;
                } else {
                    $connect = $connects{$s};
                    if ($buf =~ /^(\w+):(.+)/) {
                        print "Names2 of $1: $names2{Peter}\n";
                        print %names2;
                        print "\n";
                        #if (defined $connects{$1}) {
                            $q = $names2{$1};
                            print "$q\n";
                            $msgs[$q] = "$names{$s}: $2\r\n";
                            #print $s "$msgs[$q]";
                        #}
                    } else {
                        print $s "$names{$s}: There are $b people\r\n";
                    }
                    #print $s "You said: $buf\n";
                }
            } else { # The client disconnected.
                $readable->remove($s);
                $s->close;
                print STDERR "Client Connection closed\n";
                $count--;       
                delete $names{$s};
                #%connects  = reverse %names
            }
        }
    }
}