Muskovitz has asked for the wisdom of the Perl Monks concerning the following question:

I want an idea on how to make online chatting network in perl, just like a messenger where if there's an online people you can chat him/her. I know how to create a chat network in perl but everyone can chat in an single network like a public chat room, But what i was trying to say is you can only chat people who are online and only both of you can see your messages. I have a limited idea on how to make this things work do i need to create a server for each client entered my program? modules i use is Gtk2 IO::Socket::INET threads Thanks in advance.

Replies are listed 'Best First'.
Re: Creating an online chat in perl
by karlgoethebier (Abbot) on Apr 02, 2015 at 14:40 UTC
    ...chatting...

    I remember vaguely that there are some basic but very instructive examples in Network Programming with Perl by Lincoln Stein.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Creating an online chat in perl
by trippledubs (Deacon) on Apr 02, 2015 at 19:21 UTC
    #!/usr/bin/env perl use strict; use warnings; use IO::Socket::INET qw(CRLF); use IO::Select; my $socket = IO::Socket::INET->new( Listen => 5, LocalPort => 7777, Reuse => 1, ) or die $!; my $reader = IO::Select->new($socket); my @clients; while (1) { for my $ready ($reader->can_read()) { if ($ready == $socket) { my $client = $socket->accept(); $reader->add($client); push @clients,$client; } else { my $buffer; sysread($ready,$buffer,1024); for my $client (@clients) { if ($client != $ready) { # Dont echo to client whose chatt +ing syswrite($client,$buffer); } } } } }
    So I came up with this, it is pretty much all based on the same reference that karl mentioned to Network Programming by Stein. If you already have a group chat then it should be easy to adapt. instead of pushing your clients to an array as done here it would go into maybe a two dimensional array where each element is a client that talks to the other element and no one else. Or a hash with the key as the login name. You did not say who you want to talk to who. Maybe they enter a password and then that is the key to the hash that contains some structure that leads to the socket you want to write to. If that makes sense.
      Thanks trippledubs, i think using IO::Select is better than using threads and also i need a couple of more days to study the network programming with perl by stein to sustain my knowledge in perl network programming.