Government Security
Network Security Resources

Jump to content

Photo

Perl Chat

- - - - - server perl
  • Please log in to reply
No replies to this topic

#1 netcomm

netcomm

    Specialist

  • Members
  • 121 posts

Posted 06 August 2010 - 02:27 PM

Here is a basic chat **server**.
The discription is wrong, sorry.

any suggestions would be great.

#!/usr/bin/perl

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

$max_clients = 10;
$port = 1234;

$new_client = IO::Socket::INET->new(Proto=>"tcp", LocalPort=>$port, Listen=>$max_clients, Reuse=>1);
$sel = IO::Select->new($new_client);

while (@ready = $sel->can_read) {

    foreach $client (@ready) {
    if ($client == $new_client) {
     $add = $client->accept;
     $count = $sel->count;
     print $add "--- $count chatter(s) online ---\n";
     print $add "--- enter \"quit\" to quit ---\n";

     $sel->add($add);    

    } else {
   
     $message = <$client>;
  
     chop($message);
     chop($message);    
# if user wants to quit
     if ($message eq "quit") {
# remove him from selection
        $sel->remove($client);

        close $client;

     } else {
# send the message to all chatters
        foreach ($sel->can_write) {
         print $_ "[".$_->peerhost."] ".$message."\n"
        }
      }
    }
  }
}






Also tagged with one or more of these keywords: server, perl